-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi.py
More file actions
62 lines (49 loc) · 1.64 KB
/
pi.py
File metadata and controls
62 lines (49 loc) · 1.64 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
59
60
61
62
#!/usr/bin/env python3
# solution for https://algospot.com/judge/problem/read/PI
import numpy as np
class PiFraction(object):
def __init__(self):
pass
def diff(self, _list, degree=1):
ans = [(_list[i+1]-_list[i]) for i in range(len(_list)-1)]
if degree == 1:
return ans
else :
return self.diff(ans)
def get_difficulty(self, subList):
if len(subList) == 0:
return 0
"""
if self.cache[int(''.join(subList))] :
return self.cache[int(''.join(subList))]
"""
if all(self.diff(subList, 1) == 0):
return 1
if all(self.diff(subList, 1) == 1) or all(self.diff(subList,1) == -1) :
return 2
if all(abs(self.diff(subList,1)) == abs(subList[1]-subList[0])) :
return 4
if all(self.diff(subList, 2) == 0):
return 5
return 10
def min_difficulty(self, _list):
"""
return minumum difficulty of the given list
make the sublist of which length is 3 ~ 5 and calculate get the
difficulty then recursively call the function
"""
if len(_list) <= 5:
return self.get_difficulty(_list)
else:
ans = float('inf')
for i in range(3, 6):
ans = min(self.min_difficulty(_list[:i])+self.min_difficulty(_list[i:]), ans)
return ans
def main():
testcases = int(input())
for i in range(testcases):
_list = list(int(x) for x in input())
fraction = PiFraction()
print(fraction.min_difficulty(_list))
if __name__ == '__main__':
main()