File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -82,6 +82,10 @@ def index(self, obj):
8282 def pop (self , n ):
8383 return self [...].removeAt ( n )
8484
85+ def insert (self , i , o ):
86+ if i < 0 : i = self .length + i
87+ self [...].insert (i ,o )
88+
8589
8690def tuple (a ):
8791 return list (a )
Original file line number Diff line number Diff line change 1+ # The Computer Language Benchmarks Game
2+ # http://shootout.alioth.debian.org/
3+ #
4+ # contributed by Sokolov Yura
5+ # modified by Tupteq
6+ # modified by hartsantler 2014
7+
8+ from time import time
9+
10+ DEFAULT_ARG = 9
11+
12+ def main ():
13+ if PYTHON == 'PYTHONJS' :
14+ pythonjs .configure ( direct_operator = '+' )
15+ pass
16+
17+ times = []
18+ for i in range (3 ):
19+ t0 = time ()
20+ #res = fannkuch(DEFAULT_ARG)
21+ res = fannkuch (8 )
22+ tk = time ()
23+ times .append (tk - t0 )
24+ avg = sum (times ) / len (times )
25+ print (avg )
26+
27+ def fannkuch (n ):
28+ count = range (1 , n + 1 )
29+ max_flips = 0
30+ m = n - 1
31+ r = n
32+ check = 0
33+ perm1 = range (n )
34+ perm = range (n )
35+ #perm1_ins = perm1.insert
36+ #perm1_pop = perm1.pop
37+ if PYTHON == 'PYTHON3' :
38+ count = list (count )
39+ perm1 = list (perm1 )
40+ perm = list (perm )
41+
42+ while True :
43+ if check < 30 :
44+ check += 1
45+
46+ while r != 1 :
47+ count [r - 1 ] = r
48+ r -= 1
49+
50+ if perm1 [0 ] != 0 and perm1 [m ] != m :
51+ perm = perm1 [:]
52+ flips_count = 0
53+ k = perm [0 ]
54+ while k :
55+ perm [:k + 1 ] = perm [k ::- 1 ]
56+ flips_count += 1
57+ k = perm [0 ]
58+
59+ if flips_count > max_flips :
60+ max_flips = flips_count
61+
62+ while r != n :
63+ #perm1_ins(r, perm1_pop(0))
64+ perm1 .insert (r , perm1 .pop (0 ))
65+ count [r ] -= 1
66+ if count [r ] > 0 :
67+ break
68+ r += 1
69+ else :
70+ return max_flips
71+
72+
You can’t perform that action at this time.
0 commit comments