oh_my_method 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/oh_my_method/integer.rb +13 -0
- data/lib/oh_my_method/version.rb +1 -1
- data/spec/oh_my_method/integer_spec.rb +21 -3
- data/spec/oh_my_method/string_spec.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6a3349cf27f8f96303f09d2e650b9b44b309751
|
4
|
+
data.tar.gz: 731c8d9472584bd7670c57e9d6f403c2133b0e0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b72e363107442079ae5d25aa36d247569d09af35f479607f9318560fa8a5bfb775da1d1a136d92a64031a4fdda6832fe921795dac69402a5daaab32cda0e007
|
7
|
+
data.tar.gz: 701bd55d3e947527f290e2beffdc80f5d85f8af938fd395a736a495f96d20fcf82eb67ac599250f38cc39c04bf93c353d2dd2e0201303f67e6a3a52c8d53673d
|
data/lib/oh_my_method/integer.rb
CHANGED
@@ -6,4 +6,17 @@ class Integer
|
|
6
6
|
def minus?
|
7
7
|
self < 0
|
8
8
|
end
|
9
|
+
|
10
|
+
def combination(k)
|
11
|
+
self.factorial/(k.factorial*(self-k).factorial)
|
12
|
+
end
|
13
|
+
|
14
|
+
def permutation(k)
|
15
|
+
self.factorial/(self-k).factorial
|
16
|
+
end
|
17
|
+
|
18
|
+
def factorial
|
19
|
+
return 1 if self.zero?
|
20
|
+
(1..self).inject(:*)
|
21
|
+
end
|
9
22
|
end
|
data/lib/oh_my_method/version.rb
CHANGED
@@ -1,17 +1,35 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "Integer" do
|
4
|
-
it "plus?" do
|
3
|
+
describe "Integer Class" do
|
4
|
+
it "#plus?" do
|
5
5
|
expect(-1.plus?).to eq false
|
6
6
|
expect(0.plus?).to eq false
|
7
7
|
expect(1.plus?).to eq true
|
8
8
|
expect((1<<64).plus?).to eq true
|
9
9
|
end
|
10
10
|
|
11
|
-
it "minus?" do
|
11
|
+
it "#minus?" do
|
12
12
|
expect(-1.minus?).to eq true
|
13
13
|
expect(0.minus?).to eq false
|
14
14
|
expect(1.minus?).to eq false
|
15
15
|
expect((1<<64).minus?).to eq false
|
16
16
|
end
|
17
|
+
|
18
|
+
it "#factorial" do
|
19
|
+
expect(0.factorial).to eq 1
|
20
|
+
expect(3.factorial).to eq 6
|
21
|
+
expect(10.factorial).to eq 3628800
|
22
|
+
end
|
23
|
+
|
24
|
+
it "#combination" do
|
25
|
+
expect(4.combination(1)).to eq 4
|
26
|
+
expect(4.combination(0)).to eq 1
|
27
|
+
expect(10.combination(5)).to eq 252
|
28
|
+
end
|
29
|
+
|
30
|
+
it "#permutation" do
|
31
|
+
expect(3.permutation(0)).to eq 1
|
32
|
+
expect(3.permutation(2)).to eq 6
|
33
|
+
expect(20.permutation(5)).to eq 1860480
|
34
|
+
end
|
17
35
|
end
|
@@ -1,21 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "String Class" do
|
4
|
-
it "palindrome?" do
|
4
|
+
it "#palindrome?" do
|
5
5
|
expect("hello".palindrome?).to eq false
|
6
6
|
expect("heeeh".palindrome?).to eq true
|
7
7
|
expect("Heeeh".palindrome?).to eq false
|
8
8
|
expect("ABCBA".palindrome?).to eq true
|
9
9
|
end
|
10
10
|
|
11
|
-
it "middle_with?" do
|
11
|
+
it "#middle_with?" do
|
12
12
|
expect("hello".middle_with?("ell")).to eq true
|
13
13
|
expect("hello".middle_with?("ello")).to eq false
|
14
14
|
expect("hello".middle_with?("hel")).to eq false
|
15
15
|
expect("ABABA".middle_with?("BAB")).to eq true
|
16
16
|
end
|
17
17
|
|
18
|
-
it "sum_digit" do
|
18
|
+
it "#sum_digit" do
|
19
19
|
expect("11111".sum_digit).to eq 5
|
20
20
|
expect("12321".sum_digit).to eq 9
|
21
21
|
expect("123456789".sum_digit).to eq 45
|