1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/shootout/fannkuch.py Sun Feb 18 01:39:11 2007 +0100
1.3 @@ -0,0 +1,49 @@
1.4 +# The Computer Language Shootout
1.5 +# http://shootout.alioth.debian.org/
1.6 +#
1.7 +# Contributed by Sokolov Yura
1.8 +
1.9 +from sys import argv
1.10 +def fannkuch(n):
1.11 + count = range(1,n+1)
1.12 + maxFlipsCount, m, r, check = 0, n-1, n, 0
1.13 +
1.14 + perm1 = range(n)
1.15 + perm = range(n)
1.16 + perm1_ins = perm1.insert
1.17 + perm1_pop = perm1.pop
1.18 + while True:
1.19 + if check < 30:
1.20 + print "".join(`i+1` for i in perm1)
1.21 + check += 1;
1.22 +
1.23 + while r != 1:
1.24 + count[r-1] = r
1.25 + r -= 1
1.26 +
1.27 + if perm1[0] != 0 and perm1[m] != m:
1.28 + perm[:]=perm1
1.29 + flipsCount = 0
1.30 + k = perm[0]
1.31 + while k:
1.32 + perm[:k+1] = perm[k::-1]
1.33 + flipsCount += 1
1.34 + k = perm[0]
1.35 +
1.36 + if flipsCount > maxFlipsCount:
1.37 + maxFlipsCount = flipsCount
1.38 + maxPerm = list(perm1)
1.39 +
1.40 + while True:
1.41 + if r == n: return maxFlipsCount
1.42 + perm1_ins(r,perm1_pop(0))
1.43 + count[r] -= 1
1.44 + if count[r] > 0: break
1.45 + r += 1
1.46 +
1.47 +def main():
1.48 + n = int(argv and argv[1] or 1)
1.49 + print "Pfannkuchen(%d) = %d\n"%(n,fannkuch(n)),
1.50 +
1.51 +if __name__=="__main__":
1.52 + main()