magician 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +6 -8
- data/CHANGELOG.md +33 -5
- data/Gemfile +7 -7
- data/LICENSE.txt +1 -1
- data/README.md +15 -9
- data/VERSION +1 -1
- data/lib/magician/array.rb +133 -92
- data/lib/magician/integer.rb +44 -50
- data/lib/magician/math.rb +167 -116
- data/lib/magician/numeric.rb +38 -26
- data/lib/magician/random.rb +31 -0
- data/lib/magician/shortcuts.rb +5 -2
- data/lib/magician/string.rb +7 -7
- data/magician.gemspec +20 -18
- data/spec/array_spec.rb +87 -70
- data/spec/integer_spec.rb +44 -30
- data/spec/math_spec.rb +90 -72
- data/spec/numeric_spec.rb +36 -18
- data/spec/random_spec.rb +21 -0
- data/spec/shortcuts_spec.rb +7 -5
- data/spec/spec_helper.rb +2 -0
- data/spec/string_spec.rb +10 -9
- metadata +20 -18
data/spec/array_spec.rb
CHANGED
@@ -1,75 +1,92 @@
|
|
1
|
-
require File.
|
1
|
+
require "#{File.dirname __FILE__}/spec_helper"
|
2
2
|
|
3
3
|
describe Array do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
5
|
+
it 'should return all of its Numerics in order' do
|
6
|
+
[1, 2.0, -3].numerics.should == [1, 2.0, -3]
|
7
|
+
['string'].numerics.should == []
|
8
|
+
[].numerics.should == []
|
9
|
+
[1, 'two', 3, 'four'].numerics.should == [1, 3]
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should calculate its sum' do
|
13
|
+
[].sum.should == 0
|
14
|
+
[1].sum.should == 1
|
15
|
+
[1, 2, 3, 4].sum.should == 10
|
16
|
+
[-4, 0, 5].sum.should == 1
|
17
|
+
|
18
|
+
expect { ['string'].sum }.to raise_error RuntimeError
|
19
|
+
expect { ['string', 1].sum }.to raise_error RuntimeError
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should calculate its product' do
|
23
|
+
[].product.should == 1
|
24
|
+
[1].product.should == 1
|
25
|
+
[5, 7, 2].product.should == 70
|
26
|
+
|
27
|
+
expect { ['string'].product }.to raise_error RuntimeError
|
28
|
+
expect { ['string', 1].product }.to raise_error RuntimeError
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should calculate its middle' do
|
32
|
+
[].middle.should == nil
|
33
|
+
[4].middle.should == 4.0
|
34
|
+
[2, 1, 5, 4, 3].middle.should == 5.0
|
35
|
+
[1, 2, 3, 4].middle.should == 2.5
|
36
|
+
[1, 2, 4, 3].middle.should == 3.0
|
37
|
+
|
38
|
+
expect { ['string', 4].middle }.to raise_error RuntimeError
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should calculate its range' do
|
42
|
+
[].range.should == nil
|
43
|
+
[4].range.should == 0
|
44
|
+
[5, 1, 10].range.should == 9
|
45
|
+
|
46
|
+
expect { ['string'].range }.to raise_error RuntimeError
|
47
|
+
expect { ['string', 4].range }.to raise_error RuntimeError
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should calculate its mean' do
|
51
|
+
[].mean.should == nil
|
52
|
+
[4].mean.should == 4
|
53
|
+
[-3, 0, 6].mean.should == 1
|
54
|
+
[1, 2, 3, 4, 5].mean.should == 3
|
55
|
+
|
56
|
+
expect { ['string'].mean }.to raise_error RuntimeError
|
57
|
+
expect { ['string', 4].mean }.to raise_error RuntimeError
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should calculate its median' do
|
61
|
+
[].median.should == nil
|
62
|
+
[4].median.should == 4.0
|
63
|
+
[2, 1, 5, 4, 3].median.should == 3.0
|
64
|
+
[1, 2, 3, 4].median.should == 2.5
|
65
|
+
|
66
|
+
expect { ['string', 4].median }.to raise_error RuntimeError
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should calculate its mode' do
|
70
|
+
[].mode.should == nil
|
71
|
+
['string'].mode.should == ['string']
|
72
|
+
[4].mode.should == [4]
|
73
|
+
['string', 4].mode.should == ['string',4]
|
74
|
+
|
75
|
+
[1, 2, 1, 3, 1, 4].mode.should == [1]
|
76
|
+
[1, 1, 1, 2, 2, 2, 3].mode.should == [1, 2]
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should calculate a hash holding numbers of occurrences of its items' do
|
80
|
+
[].occurences.should == {}
|
81
|
+
['string'].occurences.should == { 'string'=>1 }
|
82
|
+
[4].occurences.should == { 4=>1 }
|
83
|
+
['string', 4].occurences.should == { 'string'=>1, 4=>1 }
|
84
|
+
|
85
|
+
[1, 2, 2, 5].occurences.should == { 1=>1, 2=>2, 5=>1 }
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should let Array#average be used as an alias to Array#mean' do
|
89
|
+
[4].average.should == 4.0
|
90
|
+
end
|
74
91
|
|
75
92
|
end
|
data/spec/integer_spec.rb
CHANGED
@@ -1,35 +1,49 @@
|
|
1
|
-
require File.
|
1
|
+
require "#{File.dirname __FILE__}/spec_helper"
|
2
2
|
|
3
3
|
describe Integer do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
5
|
+
it 'should calculate its factors' do
|
6
|
+
1.factors.should == [1]
|
7
|
+
6.factors.should == [1, 2, 3, 6]
|
8
|
+
7.factors.should == [1, 7]
|
9
|
+
|
10
|
+
-1.factors.should == [1]
|
11
|
+
-6.factors.should == [1, 2, 3, 6]
|
12
|
+
-7.factors.should == [1, 7]
|
13
|
+
|
14
|
+
expect { 0.factors }.to raise_error ArgumentError
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should calculate its factorial' do
|
18
|
+
0.factorial.should == 1
|
19
|
+
1.factorial.should == 1
|
20
|
+
-1.factorial.should == nil
|
21
|
+
5.factorial.should == 120
|
22
|
+
10.factorial.should == 3_628_800
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should determine if it is prime' do
|
26
|
+
0.prime?.should be_false
|
27
|
+
1.prime?.should be_false
|
28
|
+
2.prime?.should be_true
|
29
|
+
5.prime?.should be_true
|
30
|
+
6.prime?.should be_false
|
31
|
+
|
32
|
+
-1.prime?.should be_false
|
33
|
+
-2.prime?.should be_false
|
34
|
+
-5.prime?.should be_false
|
35
|
+
-6.prime?.should be_false
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should determine if it is pandigital' do
|
39
|
+
123456789.pandigital?.should be_true
|
40
|
+
987654321.pandigital?.should be_true
|
41
|
+
192837465.pandigital?.should be_true
|
42
|
+
|
43
|
+
12345.pandigital?.should be_false
|
44
|
+
1234567890.pandigital?.should be_false
|
45
|
+
1234567899.pandigital?.should be_false
|
46
|
+
112233445566778899.pandigital?.should be_false
|
47
|
+
end
|
34
48
|
|
35
49
|
end
|
data/spec/math_spec.rb
CHANGED
@@ -1,77 +1,95 @@
|
|
1
|
-
require File.
|
1
|
+
require "#{File.dirname __FILE__}/spec_helper"
|
2
2
|
|
3
3
|
describe Math do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
5
|
+
it 'should solve quadratic formulas' do
|
6
|
+
Math.quadratic(1, 2, 1).should == [-1.0, -1.0]
|
7
|
+
Math.quadratic(1, 1, 0).should == [-1.0, 0.0]
|
8
|
+
Math.quadratic(1, 0, 0).should == [0.0, 0.0]
|
9
|
+
|
10
|
+
expect { Math.quadratic(0, 1, 2) }.to raise_error ArgumentError
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should calculate permutations of n and k' do
|
14
|
+
Math.permutations(10, 5).should == 30_240
|
15
|
+
Math.permutations(5, 5).should == 120
|
16
|
+
Math.permutations(5, 0).should == 1
|
17
|
+
Math.permutations(0, 0).should == 1
|
18
|
+
|
19
|
+
expect { Math.permutations(0, 5) }.to raise_error ArgumentError
|
20
|
+
expect { Math.permutations(5, 10) }.to raise_error ArgumentError
|
21
|
+
expect { Math.permutations(-5, 5) }.to raise_error ArgumentError
|
22
|
+
expect { Math.permutations(5, -5) }.to raise_error ArgumentError
|
23
|
+
expect { Math.permutations(-5, -5) }.to raise_error ArgumentError
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should calculate combinations of n and k' do
|
27
|
+
Math.combinations(10, 5).should == 252
|
28
|
+
Math.combinations(5, 5).should == 1
|
29
|
+
Math.combinations(5, 0).should == 1
|
30
|
+
Math.combinations(0, 0).should == 1
|
31
|
+
|
32
|
+
expect { Math.combinations(5, 10) }.to raise_error ArgumentError
|
33
|
+
expect { Math.combinations(0, 5) }.to raise_error ArgumentError
|
34
|
+
expect { Math.combinations(-5, 5) }.to raise_error ArgumentError
|
35
|
+
expect { Math.combinations(5, -5) }.to raise_error ArgumentError
|
36
|
+
expect { Math.combinations(-5, -5) }.to raise_error ArgumentError
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should calculate the number of steps to finish the Collatz conjecture' do
|
40
|
+
Math.collatz(1).should == 0
|
41
|
+
Math.collatz(2).should == 1
|
42
|
+
Math.collatz(7).should == 16
|
43
|
+
Math.collatz(100).should == 25
|
44
|
+
|
45
|
+
expect { Math.collatz(-1) }.to raise_error ArgumentError
|
46
|
+
expect { Math.collatz(0) }.to raise_error ArgumentError
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should calculate the lengths of hypotenuses' do
|
50
|
+
Math.hypotenuse(0, 0).should == 0
|
51
|
+
Math.hypotenuse(Math.sqrt(5), 2).should == 3
|
52
|
+
Math.hypotenuse(1, 1).should == Math.sqrt(2)
|
53
|
+
|
54
|
+
expect { Math.hypotenuse(5, -5) }.to raise_error ArgumentError
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should determine if given numbers form Pythagorean triplets' do
|
58
|
+
Math.triplet?(3, 4, 5).should be_true
|
59
|
+
Math.triplet?(5, 12, 13).should be_true
|
60
|
+
Math.triplet?(7, 24, 25).should be_true
|
61
|
+
Math.triplet?(8, 15, 17).should be_true
|
62
|
+
Math.triplet?(4, 3, 5).should be_true
|
63
|
+
|
64
|
+
Math.triplet?(5, 4, 3).should be_false
|
65
|
+
Math.triplet?(0, 0, 0).should be_false
|
66
|
+
Math.triplet?(Math.sqrt(5), 2, 3).should be_false
|
67
|
+
Math.triplet?(1, 1, Math.sqrt(2)).should be_false
|
68
|
+
Math.triplet?(-1, -1, -1).should be_false
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should calculate series of Fibonacci numbers of specified lengths' do
|
72
|
+
Math.fibs(0).should == []
|
73
|
+
Math.fibs(1).should == [1]
|
74
|
+
Math.fibs(2).should == [1, 1]
|
75
|
+
Math.fibs(5).should == [1, 1, 2, 3, 5]
|
76
|
+
Math.fibs(10).should == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
77
|
+
|
78
|
+
Math.fibs(10, [5, 10]).should == [5, 10, 15, 25, 40, 65, 105, 170, 275, 445]
|
79
|
+
Math.fibs(1, [1, 2, 3]).should == [1]
|
80
|
+
|
81
|
+
expect { Math.fibs(-1) }.to raise_error ArgumentError
|
82
|
+
expect { Math.fibs(10, []) }.to raise_error ArgumentError
|
83
|
+
expect { Math.fibs(10, [9001]) }.to raise_error ArgumentError
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'should find all prime numbers up to different integers' do
|
87
|
+
Math.primes(0).should == []
|
88
|
+
Math.primes(1).should == []
|
89
|
+
Math.primes(2).should == [2]
|
90
|
+
Math.primes(5).should == [2,3,5]
|
91
|
+
Math.primes(10).should == [2,3,5,7]
|
92
|
+
Math.primes(100).should == [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
|
93
|
+
end
|
76
94
|
|
77
95
|
end
|
data/spec/numeric_spec.rb
CHANGED
@@ -1,24 +1,42 @@
|
|
1
|
-
require File.
|
1
|
+
require "#{File.dirname __FILE__}/spec_helper"
|
2
2
|
|
3
3
|
describe Numeric do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
5
|
+
it 'should determine if it is divisible by another number' do
|
6
|
+
0.divisible?(5).should be_true
|
7
|
+
1.divisible?(6).should be_false
|
8
|
+
-1.divisible?(1).should be_true
|
9
|
+
12.divisible?(6).should be_true
|
10
|
+
6.divisible?(5).should be_false
|
11
|
+
10.divisible?(0).should be_false
|
12
|
+
9.divisible?(1.5).should be_true
|
13
|
+
9.0.divisible?(1.5).should be_true
|
14
|
+
10.5.divisible?(1.5).should be_true
|
15
|
+
10.5.divisible?(1).should be_false
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
it 'should grab specific digits from different numbers' do
|
19
|
+
Math::PI.digits(0..-1).should == 3
|
20
|
+
12345.digits(0..2).should == 123
|
21
|
+
12345.digits(4).should == 5
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should convert angles to radians' do
|
25
|
+
0.to_radians.should == 0
|
26
|
+
90.to_radians.should == PI/2
|
27
|
+
180.to_radians.should == PI
|
28
|
+
270.to_radians.should == 3*PI/2
|
29
|
+
360.to_radians.should == 2*PI
|
30
|
+
-90.to_radians.should == -PI/2
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should convert angles to degrees' do
|
34
|
+
0.to_degrees.should == 0
|
35
|
+
(PI/2).to_degrees.should == 90
|
36
|
+
PI.to_degrees.should == 180
|
37
|
+
(3*PI/2).to_degrees.should == 270
|
38
|
+
(2*PI).to_degrees.should == 360
|
39
|
+
(-PI/2).to_degrees.should == -90
|
40
|
+
end
|
23
41
|
|
24
42
|
end
|