simplify

Annotated tests/shootout/fannkuch.py

201:95ea3c071ed9
2007-02-22 paulb Added tentative ListComp, ListCompFor, ListCompIf and Sliceobj support. Changed pop-up element generation so that in cases where no information is available, the pop-up elements are not generated. Added a test of list comprehensions. Added a slice built-in type.
paulb@200 1
# The Computer Language Shootout
paulb@200 2
# http://shootout.alioth.debian.org/
paulb@200 3
#
paulb@200 4
# Contributed by Sokolov Yura
paulb@200 5
paulb@200 6
from sys import argv
paulb@200 7
def fannkuch(n):
paulb@200 8
   count = range(1,n+1)
paulb@200 9
   maxFlipsCount, m, r, check = 0, n-1, n, 0
paulb@200 10
paulb@200 11
   perm1 = range(n)
paulb@200 12
   perm  = range(n)
paulb@200 13
   perm1_ins = perm1.insert
paulb@200 14
   perm1_pop = perm1.pop
paulb@200 15
   while True:
paulb@200 16
      if check < 30:
paulb@200 17
         print "".join(`i+1` for i in perm1)
paulb@200 18
         check += 1;
paulb@200 19
paulb@200 20
      while r != 1:
paulb@200 21
         count[r-1] = r
paulb@200 22
         r -= 1
paulb@200 23
paulb@200 24
      if perm1[0] != 0 and perm1[m] != m:
paulb@200 25
         perm[:]=perm1
paulb@200 26
         flipsCount = 0
paulb@200 27
         k = perm[0]
paulb@200 28
         while k:
paulb@200 29
            perm[:k+1] = perm[k::-1]
paulb@200 30
            flipsCount += 1
paulb@200 31
            k = perm[0]
paulb@200 32
paulb@200 33
         if flipsCount > maxFlipsCount:
paulb@200 34
            maxFlipsCount = flipsCount
paulb@200 35
            maxPerm = list(perm1)
paulb@200 36
paulb@200 37
      while True:
paulb@200 38
         if r == n: return maxFlipsCount
paulb@200 39
         perm1_ins(r,perm1_pop(0))
paulb@200 40
         count[r] -= 1
paulb@200 41
         if count[r] > 0: break
paulb@200 42
         r += 1
paulb@200 43
paulb@200 44
def main():
paulb@200 45
    n = int(argv and argv[1] or 1)
paulb@200 46
    print "Pfannkuchen(%d) = %d\n"%(n,fannkuch(n)),
paulb@200 47
paulb@200 48
if __name__=="__main__":
paulb@200 49
    main()