forked from elvanja/codility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
period.rb
63 lines (53 loc) · 1.42 KB
/
period.rb
1
2
3
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
def period(string)
string_period = nil
(1..(string.size / 2)).each do |period|
valid = true
(0..(string.size - period - 1)).each do |k|
valid = false and break if string[k] != string[k + period]
end
string_period = period if valid && period <= (string_period || period)
break if string_period && string_period == 1
end
string_period || -1
end
def solution(n)
period(n.to_s(2))
end
require 'rspec'
describe "#period calculates string period" do
it "when single character" do
period("1").should == -1
end
it "with two characters" do
period("00").should == 1
period("11").should == 1
period("10").should == -1
period("01").should == -1
end
it "with three characters" do
period("000").should == 1
period("111").should == 1
period("101").should == -1
period("010").should == -1
period("011").should == -1
end
it "with large one that has a period" do
period("11101110011110111001111011100111").should == 10
end
it "with given examples" do
period("1110111011").should == 4
period("1100110").should == -1
end
end
describe "#solution calculates binary period of a number" do
it "with given examples" do
solution(955).should == 4
solution(102).should == -1
end
it "with large number that has a period" do
solution(4_001_079_015).should == 10
end
it "with max number" do
solution(1_000_000_000).should == -1
end
end