-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem112.py
More file actions
58 lines (50 loc) · 1.23 KB
/
problem112.py
File metadata and controls
58 lines (50 loc) · 1.23 KB
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
# -*- encoding: utf-8 -*-
# project euler problem 112
# http://projecteuler.net/problem=112
import itertools
def isBouncy(n):
st = str(n)
length = len(st)
prev_state = None
current_state = None
if length <= 2:
return False
# Initialize state
# increase == 1
# decrease == 2
# equal == 3
if st[0] < st[1]:
prev_state = current_state = 1
elif st[0] > st[1]:
prev_state = current_state = 2
else:
prev_state = current_state = 3
i = 1
while i < length - 1:
if st[i] < st[i+1]:
current_state = 1
if prev_state == 2:
return True
else:
prev_state = current_state
elif st[i] > st[i+1]:
current_state = 2
if prev_state == 1:
return True
else:
prev_state = current_state
else:
current_state = 3
if prev_state != 3:
pass
else:
prev_state = current_state
i += 1
return False
count = 0
for n in itertools.count(101):
if isBouncy(n):
count += 1
if 100*count == 99*n:
print n
break