-
Notifications
You must be signed in to change notification settings - Fork 0
/
projecteuler76.py
65 lines (58 loc) · 1.59 KB
/
projecteuler76.py
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
64
65
import gc
import cProfile
import io
import itertools
import pstats
import time
def profile(fnc):
"""A decorator that uses cProfile to profile a function"""
def inner(*args, **kwargs):
pr = cProfile.Profile()
pr.enable()
retval = fnc(*args, **kwargs)
pr.disable()
s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
return retval
return inner
def add_first_two_numbers(b):
return [b[0] + b[1]] + b[2:]
def all_pairs(a):
w = [sorted((x, a-x)) for x in range(a,1,-1)]
return w[1:]
def final(input_list):
for each in input_list:
print(1, each)
try:
input_list.append(add_first_two_numbers(each))
except IndexError:
pass
print('-'*8)
input_list = [sorted(x) for x in input_list if len(x) > 1]
input_list.sort()
input_list = list(k for k,_ in itertools.groupby(input_list))
return input_list
@profile
def start():
ti = time.time()
num = 10
stuff = [x for x in all_pairs(num)] + [[1 for x in range(num)]]
stuff = final(stuff,)
print(time.time()-ti)
interesting = [len(stuff)+1, len(stuff)]
print(len(stuff))
r = 0
while interesting[-2] != interesting[-1]:
r+=1
print(r)
stuff = final(stuff)
print(len(stuff))
interesting.append(len(stuff))
gc.collect()
print()
print(len(list(stuff)))
print(time.time()-ti)
start()