magician 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -6
- data/README.md +9 -4
- data/VERSION +1 -1
- data/lib/magician/array.rb +4 -4
- data/lib/magician/integer.rb +3 -3
- data/lib/magician/math.rb +10 -9
- data/magician.gemspec +77 -0
- data/spec/array_spec.rb +7 -7
- data/spec/integer_spec.rb +4 -4
- data/spec/math_spec.rb +7 -7
- data/spec/numeric_spec.rb +1 -1
- data/spec/shortcuts_spec.rb +1 -1
- data/spec/string_spec.rb +1 -1
- metadata +4 -3
data/Gemfile
CHANGED
@@ -5,10 +5,10 @@ source :rubygems
|
|
5
5
|
# Add dependencies to develop your gem here.
|
6
6
|
# Include everything needed to run rake, tests, features, etc.
|
7
7
|
group :development do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
gem 'rspec', '~> 2.12.0'
|
9
|
+
gem 'yard', '~> 0.7'
|
10
|
+
gem 'rdoc', '~> 3.12'
|
11
|
+
gem 'bundler'
|
12
|
+
gem 'jeweler', '~> 1.8.4'
|
13
|
+
gem 'simplecov'
|
14
14
|
end
|
data/README.md
CHANGED
@@ -4,13 +4,18 @@ A suite of handy methods for doing calculations in irb.
|
|
4
4
|
|
5
5
|
## Contributing to magician
|
6
6
|
|
7
|
-
- Check out the latest master to make sure the feature hasn't been implemented
|
8
|
-
|
7
|
+
- Check out the latest master to make sure the feature hasn't been implemented
|
8
|
+
or the bug hasn't been fixed yet.
|
9
|
+
- Check out the issue tracker to make sure someone already hasn't requested it
|
10
|
+
and/or contributed it.
|
9
11
|
- Fork the project.
|
10
12
|
- Start a feature/bugfix branch.
|
11
13
|
- Commit and push until you are happy with your contribution.
|
12
|
-
- Make sure to add tests for it. This is important so I don't break it in a
|
13
|
-
|
14
|
+
- Make sure to add tests for it. This is important so I don't break it in a
|
15
|
+
future version unintentionally.
|
16
|
+
- Please try not to mess with the Rakefile, version, or history. If you want to
|
17
|
+
have your own version, or is otherwise necessary, that is fine, but please
|
18
|
+
isolate to its own commit so I can cherry-pick around it.
|
14
19
|
|
15
20
|
## Copyright
|
16
21
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/magician/array.rb
CHANGED
@@ -55,7 +55,7 @@ class Array
|
|
55
55
|
# frequently). The mode of an empty array is nil.
|
56
56
|
#
|
57
57
|
# @return [Array] an array of all of the items in the array that occur the
|
58
|
-
# most
|
58
|
+
# most frequently (they must all have the same number of occurrences)
|
59
59
|
def mode
|
60
60
|
return nil if self.empty?
|
61
61
|
occ = occurences
|
@@ -63,12 +63,12 @@ class Array
|
|
63
63
|
occ.select { |key, value| value == max_occ }.keys
|
64
64
|
end
|
65
65
|
|
66
|
-
# Gets a hash table with the number of
|
66
|
+
# Gets a hash table with the number of occurrences of each item from the
|
67
67
|
# original array. The keys are the items from the original array, and the
|
68
|
-
# values are integers counting the number of
|
68
|
+
# values are integers counting the number of occurrences of the associated key
|
69
69
|
# values.
|
70
70
|
#
|
71
|
-
# @return [Hash] a hash table of the
|
71
|
+
# @return [Hash] a hash table of the occurrences of each item from the original
|
72
72
|
# array
|
73
73
|
def occurences
|
74
74
|
occurences = {}
|
data/lib/magician/integer.rb
CHANGED
@@ -3,7 +3,7 @@ class Integer
|
|
3
3
|
|
4
4
|
# Gets all of the factors of the current integer. If the current integer is
|
5
5
|
# negative, it will be treated as if it were positive (so the results will
|
6
|
-
# never contain negative integers).
|
6
|
+
# never contain negative integers). Returns nil if the integer is 0.
|
7
7
|
#
|
8
8
|
# @return [Array] an array of all of the factors of the current integer (in
|
9
9
|
# order, including 1 and the integer itself)
|
@@ -25,7 +25,7 @@ class Integer
|
|
25
25
|
until factors_old.length == 0
|
26
26
|
factors << self.abs/factors_old.pop
|
27
27
|
end
|
28
|
-
|
28
|
+
factors
|
29
29
|
end
|
30
30
|
|
31
31
|
# Gets the factorial of the integer, which is equivalent to the product of all
|
@@ -49,7 +49,7 @@ class Integer
|
|
49
49
|
for i in 2..Math.sqrt(self)
|
50
50
|
return false if self % i == 0
|
51
51
|
end
|
52
|
-
|
52
|
+
true
|
53
53
|
end
|
54
54
|
|
55
55
|
# Returns true if the integer is evenly divisible by n. If n is 0, it returns
|
data/lib/magician/math.rb
CHANGED
@@ -6,7 +6,7 @@ module Math
|
|
6
6
|
|
7
7
|
# Solves a quadratic formula of the form "ax^2+bx+c=0" for x, where a is not
|
8
8
|
# 0. It asks for the three coefficients of the function (a, b, and c), and
|
9
|
-
# returns the two possible values for x.
|
9
|
+
# returns the two possible values for x. Returns nil if a is 0.
|
10
10
|
#
|
11
11
|
# @param [Numeric] a the first coefficient (must not be 0)
|
12
12
|
# @param [Numeric] b the second coefficient
|
@@ -30,7 +30,7 @@ module Math
|
|
30
30
|
# @return [Integer] the number of permutations
|
31
31
|
def permutations(n, k)
|
32
32
|
return nil if n < 0 or k < 0 or n < k
|
33
|
-
|
33
|
+
n.factorial / (n-k).factorial
|
34
34
|
end
|
35
35
|
|
36
36
|
# The number of size k unordered subsets of a set of size n. Equivalent to
|
@@ -42,28 +42,29 @@ module Math
|
|
42
42
|
# @return [Integer] the number of combinations
|
43
43
|
def combinations(n, k)
|
44
44
|
return nil if n < 0 or k < 0 or n < k
|
45
|
-
|
45
|
+
n.factorial / (k.factorial * (n-k).factorial)
|
46
46
|
end
|
47
47
|
|
48
48
|
# Get the number of steps it takes to get from integer n to 1 using the
|
49
|
-
# Collatz
|
49
|
+
# Collatz conjecture (set http://en.wikipedia.org/wiki/Collatz_conjecture).
|
50
|
+
# Returns nil if n < 1.
|
50
51
|
#
|
51
|
-
# @param [Integer] n the number to put into the Collatz
|
52
|
+
# @param [Integer] n the number to put into the Collatz conjecture initially
|
52
53
|
# @param [Integer] depth the number of steps that have passed so far (should
|
53
54
|
# not be modified unless this is being cached carefully)
|
54
55
|
#
|
55
56
|
# @return [Integer] the number of steps it takes to get from integer n to 1
|
56
|
-
# using the Collatz
|
57
|
+
# using the Collatz conjecture (the depth)
|
57
58
|
def collatz(n, depth=0)
|
58
59
|
return nil if n < 1
|
59
60
|
if n == 1
|
60
|
-
|
61
|
+
depth
|
61
62
|
elsif n % 2 == 0
|
62
63
|
depth += 1
|
63
|
-
|
64
|
+
collatz(n/2, depth)
|
64
65
|
else
|
65
66
|
depth += 1
|
66
|
-
|
67
|
+
collatz(3*n + 1, depth)
|
67
68
|
end
|
68
69
|
end
|
69
70
|
|
data/magician.gemspec
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "magician"
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Nicolas McCurdy"]
|
12
|
+
s.date = "2012-12-01"
|
13
|
+
s.description = "A suite of handy methods for doing calculations in irb."
|
14
|
+
s.email = "thenickperson@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
".travis.yml",
|
23
|
+
"Gemfile",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.md",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/magician.rb",
|
29
|
+
"lib/magician/array.rb",
|
30
|
+
"lib/magician/integer.rb",
|
31
|
+
"lib/magician/math.rb",
|
32
|
+
"lib/magician/numeric.rb",
|
33
|
+
"lib/magician/shortcuts.rb",
|
34
|
+
"lib/magician/string.rb",
|
35
|
+
"magician.gemspec",
|
36
|
+
"spec/array_spec.rb",
|
37
|
+
"spec/integer_spec.rb",
|
38
|
+
"spec/math_spec.rb",
|
39
|
+
"spec/numeric_spec.rb",
|
40
|
+
"spec/shortcuts_spec.rb",
|
41
|
+
"spec/spec_helper.rb",
|
42
|
+
"spec/string_spec.rb"
|
43
|
+
]
|
44
|
+
s.homepage = "http://github.com/thenickperson/magician"
|
45
|
+
s.licenses = ["MIT"]
|
46
|
+
s.require_paths = ["lib"]
|
47
|
+
s.rubygems_version = "1.8.24"
|
48
|
+
s.summary = "A suite of handy methods for doing calculations in irb."
|
49
|
+
|
50
|
+
if s.respond_to? :specification_version then
|
51
|
+
s.specification_version = 3
|
52
|
+
|
53
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
54
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.12.0"])
|
55
|
+
s.add_development_dependency(%q<yard>, ["~> 0.7"])
|
56
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
57
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
58
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
59
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
62
|
+
s.add_dependency(%q<yard>, ["~> 0.7"])
|
63
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
64
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
65
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
66
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
67
|
+
end
|
68
|
+
else
|
69
|
+
s.add_dependency(%q<rspec>, ["~> 2.12.0"])
|
70
|
+
s.add_dependency(%q<yard>, ["~> 0.7"])
|
71
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
72
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
73
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
74
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
data/spec/array_spec.rb
CHANGED
@@ -2,47 +2,47 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe Array do
|
4
4
|
|
5
|
-
it 'should
|
5
|
+
it 'should calculate its sum' do
|
6
6
|
[].sum.should == 0
|
7
7
|
[1].sum.should == 1
|
8
8
|
[1, 2, 3, 4].sum.should == 10
|
9
9
|
[-4, 0, 5].sum.should == 1
|
10
10
|
end
|
11
11
|
|
12
|
-
it 'should
|
12
|
+
it 'should calculate its product' do
|
13
13
|
[].product.should == 1
|
14
14
|
[1].product.should == 1
|
15
15
|
[5, 7, 2].product.should == 70
|
16
16
|
end
|
17
17
|
|
18
|
-
it 'should
|
18
|
+
it 'should calculate its range' do
|
19
19
|
[].range.should == nil
|
20
20
|
[4].range.should == 0
|
21
21
|
[5, 1, 10].range.should == 9
|
22
22
|
end
|
23
23
|
|
24
|
-
it 'should
|
24
|
+
it 'should calculate its mean' do
|
25
25
|
[].mean.should == nil
|
26
26
|
[4].mean.should == 4
|
27
27
|
[-3, 0, 6].mean.should == 1
|
28
28
|
[1, 2, 3, 4, 5].mean.should == 3
|
29
29
|
end
|
30
30
|
|
31
|
-
it 'should
|
31
|
+
it 'should calculate its median' do
|
32
32
|
[].median.should == nil
|
33
33
|
[4].median.should == 4.0
|
34
34
|
[2, 1, 3, 5, 4].median.should == 3.0
|
35
35
|
[1, 2, 3, 4].median.should == 2.5
|
36
36
|
end
|
37
37
|
|
38
|
-
it 'should
|
38
|
+
it 'should calculate its mode' do
|
39
39
|
[].mode.should == nil
|
40
40
|
[4].mode.should == [4]
|
41
41
|
[1, 2, 1, 3, 1, 4].mode.should == [1]
|
42
42
|
[1, 1, 1, 2, 2, 2, 3].mode.should == [1, 2]
|
43
43
|
end
|
44
44
|
|
45
|
-
it 'should
|
45
|
+
it 'should calculate a hash holding numbers of occurrences of its items' do
|
46
46
|
[].occurences.should == {}
|
47
47
|
[4].occurences.should == { 4=>1 }
|
48
48
|
[1, 2, 2, 5].occurences.should == { 1=>1, 2=>2, 5=>1 }
|
data/spec/integer_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe Integer do
|
4
4
|
|
5
|
-
it 'should
|
5
|
+
it 'should calculate its factors' do
|
6
6
|
0.factors.should == nil
|
7
7
|
1.factors.should == [1]
|
8
8
|
6.factors.should == [1, 2, 3, 6]
|
@@ -12,7 +12,7 @@ describe Integer do
|
|
12
12
|
-7.factors.should == [1, 7]
|
13
13
|
end
|
14
14
|
|
15
|
-
it 'should
|
15
|
+
it 'should calculate its factorial' do
|
16
16
|
0.factorial.should == 1
|
17
17
|
1.factorial.should == 1
|
18
18
|
-1.factorial.should == nil
|
@@ -20,7 +20,7 @@ describe Integer do
|
|
20
20
|
10.factorial.should == 3_628_800
|
21
21
|
end
|
22
22
|
|
23
|
-
it 'should determine if
|
23
|
+
it 'should determine if it is prime' do
|
24
24
|
0.prime?.should be_false
|
25
25
|
1.prime?.should be_false
|
26
26
|
2.prime?.should be_true
|
@@ -32,7 +32,7 @@ describe Integer do
|
|
32
32
|
-6.prime?.should be_false
|
33
33
|
end
|
34
34
|
|
35
|
-
it 'should determine if
|
35
|
+
it 'should determine if it is divisible by another number' do
|
36
36
|
0.divisible?(5).should be_true
|
37
37
|
1.divisible?(6).should be_false
|
38
38
|
-1.divisible?(1).should be_true
|
data/spec/math_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe Math do
|
4
4
|
|
5
|
-
it 'should solve
|
5
|
+
it 'should solve quadratic formulas' do
|
6
6
|
Math.quadratic(1, 2, 1).should == [-1.0, -1.0]
|
7
7
|
Math.quadratic(1, 1, 0).should == [-1.0, 0.0]
|
8
8
|
Math.quadratic(1, 0, 0).should == [0.0, 0.0]
|
@@ -12,7 +12,7 @@ describe Math do
|
|
12
12
|
#Math.quadratic(1, 1, 1).should == 'change me'
|
13
13
|
end
|
14
14
|
|
15
|
-
it 'should
|
15
|
+
it 'should calculate permutations of n and k' do
|
16
16
|
Math.permutations(10, 5).should == 30_240
|
17
17
|
Math.permutations(5, 5).should == 120
|
18
18
|
Math.permutations(5, 0).should == 1
|
@@ -24,7 +24,7 @@ describe Math do
|
|
24
24
|
Math.permutations(-5, -5).should == nil
|
25
25
|
end
|
26
26
|
|
27
|
-
it 'should
|
27
|
+
it 'should calculate combinations of n and k' do
|
28
28
|
Math.combinations(10, 5).should == 252
|
29
29
|
Math.combinations(5, 10).should == nil
|
30
30
|
Math.combinations(5, 5).should == 1
|
@@ -36,7 +36,7 @@ describe Math do
|
|
36
36
|
Math.combinations(-5, -5).should == nil
|
37
37
|
end
|
38
38
|
|
39
|
-
it 'should
|
39
|
+
it 'should calculate the number of steps to finish the Collatz conjecture' do
|
40
40
|
Math.collatz(-1).should == nil
|
41
41
|
Math.collatz(0).should == nil
|
42
42
|
Math.collatz(1).should == 0
|
@@ -45,14 +45,14 @@ describe Math do
|
|
45
45
|
Math.collatz(100).should == 25
|
46
46
|
end
|
47
47
|
|
48
|
-
it 'should
|
48
|
+
it 'should calculate the lengths of hypotenuses' do
|
49
49
|
Math.hypotenuse(0, 0).should == 0
|
50
50
|
Math.hypotenuse(Math.sqrt(5), 2).should == 3
|
51
51
|
Math.hypotenuse(1, 1).should == Math.sqrt(2)
|
52
52
|
Math.hypotenuse(5, -5).should be_nil
|
53
53
|
end
|
54
54
|
|
55
|
-
it 'should determine if
|
55
|
+
it 'should determine if given numbers form Pythagorean triplets' do
|
56
56
|
Math.triplet?(3, 4, 5).should be_true
|
57
57
|
Math.triplet?(5, 12, 13).should be_true
|
58
58
|
Math.triplet?(7, 24, 25).should be_true
|
@@ -65,7 +65,7 @@ describe Math do
|
|
65
65
|
Math.triplet?(-1, -1, -1).should be_false
|
66
66
|
end
|
67
67
|
|
68
|
-
it 'should calculate
|
68
|
+
it 'should calculate series of Fibonacci numbers of specified lengths' do
|
69
69
|
Math.fibs(-1).should == nil
|
70
70
|
Math.fibs(0).should == []
|
71
71
|
Math.fibs(1).should == [1]
|
data/spec/numeric_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe Numeric do
|
4
4
|
|
5
|
-
it 'should
|
5
|
+
it 'should grab specific digits from different numbers' do
|
6
6
|
Math::PI.digits(0..-1).should == 3
|
7
7
|
12345.digits(0..2).should == 123
|
8
8
|
12345.digits(4).should == 5
|
data/spec/shortcuts_spec.rb
CHANGED
data/spec/string_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe String do
|
4
4
|
|
5
|
-
it 'should determine if
|
5
|
+
it 'should determine if it is a palindrome' do
|
6
6
|
''.palindrome?.should be_true
|
7
7
|
'a'.palindrome?.should be_true
|
8
8
|
'deed'.palindrome?.should be_true
|
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.1.
|
4
|
+
version: 0.1.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
|
+
date: 2012-12-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- lib/magician/numeric.rb
|
131
131
|
- lib/magician/shortcuts.rb
|
132
132
|
- lib/magician/string.rb
|
133
|
+
- magician.gemspec
|
133
134
|
- spec/array_spec.rb
|
134
135
|
- spec/integer_spec.rb
|
135
136
|
- spec/math_spec.rb
|
@@ -152,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
152
153
|
version: '0'
|
153
154
|
segments:
|
154
155
|
- 0
|
155
|
-
hash:
|
156
|
+
hash: -2635961885196991731
|
156
157
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
158
|
none: false
|
158
159
|
requirements:
|