File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ The Computer Language Benchmarks Game
3+ http://benchmarksgame.alioth.debian.org/
4+
5+ Contributed by Sokolov Yura, modified by Tupteq.
6+ """
7+
8+ # import pyperf
9+
10+
11+ DEFAULT_ARG = 9
12+
13+
14+ def fannkuch (n ):
15+ count = list (range (1 , n + 1 ))
16+ max_flips = 0
17+ m = n - 1
18+ r = n
19+ perm1 = list (range (n ))
20+ perm = list (range (n ))
21+ perm1_ins = perm1 .insert
22+ perm1_pop = perm1 .pop
23+
24+ while 1 :
25+ while r != 1 :
26+ count [r - 1 ] = r
27+ r -= 1
28+
29+ if perm1 [0 ] != 0 and perm1 [m ] != m :
30+ perm = perm1 [:]
31+ flips_count = 0
32+ k = perm [0 ]
33+ while k :
34+ perm [:k + 1 ] = perm [k ::- 1 ]
35+ flips_count += 1
36+ k = perm [0 ]
37+
38+ if flips_count > max_flips :
39+ max_flips = flips_count
40+
41+ while r != n :
42+ perm1_ins (r , perm1_pop (0 ))
43+ count [r ] -= 1
44+ if count [r ] > 0 :
45+ break
46+ r += 1
47+ else :
48+ return max_flips
49+
50+
51+ if __name__ == "__main__" :
52+ #runner = pyperf.Runner()
53+ arg = DEFAULT_ARG
54+ #runner.bench_func('fannkuch', fannkuch, arg)
55+ fannkuch (arg )
You can’t perform that action at this time.
0 commit comments