magician 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/magician/array.rb +5 -7
- data/lib/magician/integer.rb +14 -14
- data/lib/magician/numeric.rb +3 -1
- data/lib/magician/shortcuts.rb +3 -0
- data/magician.gemspec +2 -2
- data/spec/array_spec.rb +1 -1
- metadata +3 -3
data/lib/magician/array.rb
CHANGED
@@ -57,13 +57,12 @@ class Array
|
|
57
57
|
#
|
58
58
|
# @return [Numeric] the median of the numbers in the array
|
59
59
|
def median
|
60
|
-
|
61
|
-
return nil if
|
62
|
-
sorted = nums.sort
|
60
|
+
sorted = numerics.sort
|
61
|
+
return nil if sorted.empty?
|
63
62
|
if sorted.length.odd?
|
64
|
-
|
63
|
+
sorted[sorted.length/2]
|
65
64
|
else
|
66
|
-
(
|
65
|
+
(sorted[sorted.length/2-1] + sorted[sorted.length/2]) / 2.0
|
67
66
|
end
|
68
67
|
end
|
69
68
|
|
@@ -87,8 +86,7 @@ class Array
|
|
87
86
|
# @return [Hash] a hash table of the occurrences of each item from the original
|
88
87
|
# array
|
89
88
|
def occurences
|
90
|
-
occurences =
|
91
|
-
each { |item| occurences[item] = 0 }
|
89
|
+
occurences = Hash.new(0)
|
92
90
|
each { |item| occurences[item] += 1 }
|
93
91
|
occurences
|
94
92
|
end
|
data/lib/magician/integer.rb
CHANGED
@@ -11,21 +11,13 @@ class Integer
|
|
11
11
|
return nil if self == 0
|
12
12
|
return [1] if abs == 1
|
13
13
|
#first half
|
14
|
-
factors = []
|
15
|
-
|
16
|
-
if abs
|
17
|
-
|
18
|
-
factors << i
|
19
|
-
else break
|
20
|
-
end
|
14
|
+
factors = [1]
|
15
|
+
2.upto((abs/2).to_i) do |i|
|
16
|
+
if abs%i == 0
|
17
|
+
factors << i
|
21
18
|
end
|
22
19
|
end
|
23
|
-
|
24
|
-
factors_old = factors.dup
|
25
|
-
until factors_old.length == 0
|
26
|
-
factors << abs/factors_old.pop
|
27
|
-
end
|
28
|
-
factors
|
20
|
+
factors << abs
|
29
21
|
end
|
30
22
|
|
31
23
|
# Gets the factorial of the integer, which is equivalent to the product of all
|
@@ -46,10 +38,18 @@ class Integer
|
|
46
38
|
# @return [Boolean] true if the integer is prime
|
47
39
|
def prime?
|
48
40
|
return false if self <= 1
|
49
|
-
|
41
|
+
(2..Math.sqrt(self)).each do |i|
|
50
42
|
return false if self % i == 0
|
51
43
|
end
|
52
44
|
true
|
53
45
|
end
|
54
46
|
|
47
|
+
# Returns true if the number is pandigital. That is the number contains the
|
48
|
+
# each of the digits 1 through 9 exactly once.
|
49
|
+
#
|
50
|
+
# @return [Boolean] true if pandigital
|
51
|
+
def pandigital?
|
52
|
+
to_s.split(//).sort!.join.to_i == 123456789
|
53
|
+
end
|
54
|
+
|
55
55
|
end
|
data/lib/magician/numeric.rb
CHANGED
data/lib/magician/shortcuts.rb
CHANGED
data/magician.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "magician"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Nicolas McCurdy"]
|
12
|
-
s.date = "2012-12-
|
12
|
+
s.date = "2012-12-10"
|
13
13
|
s.description = "A suite of handy methods for doing calculations in irb."
|
14
14
|
s.email = "thenickperson@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/array_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magician
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -154,7 +154,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
154
154
|
version: '0'
|
155
155
|
segments:
|
156
156
|
- 0
|
157
|
-
hash: -
|
157
|
+
hash: -1827626556693680297
|
158
158
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
159
|
none: false
|
160
160
|
requirements:
|