-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path112.rb
36 lines (33 loc) · 825 Bytes
/
112.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
require 'pp'
def bouncy(n)
digits = n.to_s.chars.map(&:to_i)
return false if digits.length == 1
if digits[0] < digits[1]
directionality = 'ascending'
elsif digits[0] == digits[1]
directionality = 'equal'
else
directionality = 'descending'
end
1.upto(digits.length-2) do |i|
if directionality == 'equal'
if digits[i] < digits[i+1]
directionality = 'ascending'
elsif digits[i] > digits[i+1]
directionality = 'descending'
end
else
return true if (directionality == 'ascending' && digits[i] > digits[i+1]) || (directionality == 'descending' && digits[i] < digits[i+1])
end
end
false
end
count = 0
bouncy = 0
bouncy_ratio = 0
while bouncy_ratio != 0.99
count += 1
bouncy += 1 if bouncy(count)
bouncy_ratio = bouncy.to_f/count
end
pp count