magician 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,75 +1,92 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require "#{File.dirname __FILE__}/spec_helper"
2
2
 
3
3
  describe Array do
4
4
 
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
- ['string'].sum.should == 0
15
- [1].sum.should == 1
16
- ['string', 1].sum.should == 1
17
- [1, 2, 3, 4].sum.should == 10
18
- [-4, 0, 5].sum.should == 1
19
- end
20
-
21
- it 'should calculate its product' do
22
- [].product.should == 1
23
- ['string'].product.should == 1
24
- [1].product.should == 1
25
- ['string', 1].product.should == 1
26
- [5, 7, 2].product.should == 70
27
- end
28
-
29
- it 'should calculate its range' do
30
- [].range.should == nil
31
- ['string'].range.should == nil
32
- [4].range.should == 0
33
- ['string', 4].range.should == 0
34
- [5, 1, 10].range.should == 9
35
- end
36
-
37
- it 'should calculate its mean' do
38
- [].mean.should == nil
39
- ['string'].mean.should == nil
40
- [4].mean.should == 4
41
- ['string', 4].mean.should == 4
42
- [-3, 0, 6].mean.should == 1
43
- [1, 2, 3, 4, 5].mean.should == 3
44
- end
45
-
46
- it 'should calculate its median' do
47
- [].median.should == nil
48
- [4].median.should == 4.0
49
- ['string', 4].median.should == 4.0
50
- [2, 1, 5, 4, 3].median.should == 3.0
51
- [1, 2, 3, 4].median.should == 2.5
52
- end
53
-
54
- it 'should calculate its mode' do
55
- [].mode.should == nil
56
- ['string'].mode.should == ['string']
57
- [4].mode.should == [4]
58
- ['string', 4].mode.should == ['string',4]
59
- [1, 2, 1, 3, 1, 4].mode.should == [1]
60
- [1, 1, 1, 2, 2, 2, 3].mode.should == [1, 2]
61
- end
62
-
63
- it 'should calculate a hash holding numbers of occurrences of its items' do
64
- [].occurences.should == {}
65
- ['string'].occurences.should == { 'string'=>1 }
66
- [4].occurences.should == { 4=>1 }
67
- ['string', 4].occurences.should == { 'string'=>1, 4=>1 }
68
- [1, 2, 2, 5].occurences.should == { 1=>1, 2=>2, 5=>1 }
69
- end
70
-
71
- it 'should let Array#average be used as an alias to Array#mean' do
72
- [4].average.should == 4.0
73
- end
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
@@ -1,35 +1,49 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require "#{File.dirname __FILE__}/spec_helper"
2
2
 
3
3
  describe Integer do
4
4
 
5
- it 'should calculate its factors' do
6
- 0.factors.should == nil
7
- 1.factors.should == [1]
8
- 6.factors.should == [1, 2, 3, 6]
9
- 7.factors.should == [1, 7]
10
- -1.factors.should == [1]
11
- -6.factors.should == [1, 2, 3, 6]
12
- -7.factors.should == [1, 7]
13
- end
14
-
15
- it 'should calculate its factorial' do
16
- 0.factorial.should == 1
17
- 1.factorial.should == 1
18
- -1.factorial.should == nil
19
- 5.factorial.should == 120
20
- 10.factorial.should == 3_628_800
21
- end
22
-
23
- it 'should determine if it is prime' do
24
- 0.prime?.should be_false
25
- 1.prime?.should be_false
26
- 2.prime?.should be_true
27
- 5.prime?.should be_true
28
- 6.prime?.should be_false
29
- -1.prime?.should be_false
30
- -2.prime?.should be_false
31
- -5.prime?.should be_false
32
- -6.prime?.should be_false
33
- end
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
@@ -1,77 +1,95 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require "#{File.dirname __FILE__}/spec_helper"
2
2
 
3
3
  describe Math do
4
4
 
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
- Math.quadratic(0, 1, 2).should == nil
10
- #Math.quadratic(1, 2, 3).should == 'change me'
11
- #Math.quadratic(-1, -2, -3).should == 'change me'
12
- #Math.quadratic(1, 1, 1).should == 'change me'
13
- end
14
-
15
- it 'should calculate permutations of n and k' do
16
- Math.permutations(10, 5).should == 30_240
17
- Math.permutations(5, 5).should == 120
18
- Math.permutations(5, 0).should == 1
19
- Math.permutations(0, 5).should == nil
20
- Math.permutations(0, 0).should == 1
21
- Math.permutations(5, 10).should == nil
22
- Math.permutations(-5, 5).should == nil
23
- Math.permutations(5, -5).should == nil
24
- Math.permutations(-5, -5).should == nil
25
- end
26
-
27
- it 'should calculate combinations of n and k' do
28
- Math.combinations(10, 5).should == 252
29
- Math.combinations(5, 10).should == nil
30
- Math.combinations(5, 5).should == 1
31
- Math.combinations(5, 0).should == 1
32
- Math.combinations(0, 5).should == nil
33
- Math.combinations(0, 0).should == 1
34
- Math.combinations(-5, 5).should == nil
35
- Math.combinations(5, -5).should == nil
36
- Math.combinations(-5, -5).should == nil
37
- end
38
-
39
- it 'should calculate the number of steps to finish the Collatz conjecture' do
40
- Math.collatz(-1).should == nil
41
- Math.collatz(0).should == nil
42
- Math.collatz(1).should == 0
43
- Math.collatz(2).should == 1
44
- Math.collatz(7).should == 16
45
- Math.collatz(100).should == 25
46
- end
47
-
48
- it 'should calculate the lengths of hypotenuses' do
49
- Math.hypotenuse(0, 0).should == 0
50
- Math.hypotenuse(Math.sqrt(5), 2).should == 3
51
- Math.hypotenuse(1, 1).should == Math.sqrt(2)
52
- Math.hypotenuse(5, -5).should be_nil
53
- end
54
-
55
- it 'should determine if given numbers form Pythagorean triplets' do
56
- Math.triplet?(3, 4, 5).should be_true
57
- Math.triplet?(5, 12, 13).should be_true
58
- Math.triplet?(7, 24, 25).should be_true
59
- Math.triplet?(8, 15, 17).should be_true
60
- Math.triplet?(4, 3, 5).should be_true
61
- Math.triplet?(5, 4, 3).should be_false
62
- Math.triplet?(0, 0, 0).should be_false
63
- Math.triplet?(Math.sqrt(5), 2, 3).should be_false
64
- Math.triplet?(1, 1, Math.sqrt(2)).should be_false
65
- Math.triplet?(-1, -1, -1).should be_false
66
- end
67
-
68
- it 'should calculate series of Fibonacci numbers of specified lengths' do
69
- Math.fibs(-1).should == nil
70
- Math.fibs(0).should == []
71
- Math.fibs(1).should == [1]
72
- Math.fibs(2).should == [1, 1]
73
- Math.fibs(5).should == [1, 1, 2, 3, 5]
74
- Math.fibs(10).should == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
75
- end
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
@@ -1,24 +1,42 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require "#{File.dirname __FILE__}/spec_helper"
2
2
 
3
3
  describe Numeric do
4
4
 
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
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
- 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
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