magician 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- nums = numerics
61
- return nil if nums.empty?
62
- sorted = nums.sort
60
+ sorted = numerics.sort
61
+ return nil if sorted.empty?
63
62
  if sorted.length.odd?
64
- nums[nums.length/2]
63
+ sorted[sorted.length/2]
65
64
  else
66
- (nums[nums.length/2-1] + nums[nums.length/2]) / 2.0
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
@@ -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
- for i in 1..abs
16
- if abs % i == 0
17
- if i < abs/i
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
- #second half
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
- for i in 2..Math.sqrt(self)
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
@@ -22,8 +22,10 @@ class Numeric
22
22
  #
23
23
  # @return [Integer] substring of the number (using []), converted to an
24
24
  # Integer
25
+ #
26
+ # @deprecated because it's useless and wrong
25
27
  def digits selection
26
28
  to_s[selection].to_i
27
29
  end
28
30
 
29
- end
31
+ end
@@ -5,3 +5,6 @@ PI = Math::PI
5
5
 
6
6
  # Alias Math::E to E
7
7
  E = Math::E
8
+
9
+ # the Golden ratio
10
+ GOLDEN_RATIO = (1 + Math.sqrt(5))/2
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.0"
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-03"
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
@@ -47,7 +47,7 @@ describe Array do
47
47
  [].median.should == nil
48
48
  [4].median.should == 4.0
49
49
  ['string', 4].median.should == 4.0
50
- [2, 1, 3, 5, 4].median.should == 3.0
50
+ [2, 1, 5, 4, 3].median.should == 3.0
51
51
  [1, 2, 3, 4].median.should == 2.5
52
52
  end
53
53
 
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.0
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-03 00:00:00.000000000 Z
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: -956776375334094677
157
+ hash: -1827626556693680297
158
158
  required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  none: false
160
160
  requirements: