SHARE
TWEET

Untitled

a guest Feb 28th, 2012 19 Never
  1. import sys
  2.  
  3. def gen_games(i):
  4.     if i == 1:
  5.         yield 'A'
  6.         yield 'B'
  7.     else:
  8.         for game in gen_games(i-1):
  9.             yield 'A' + game
  10.             yield 'B' + game
  11.  
  12. def shorten(count, l):
  13.     for i in range(count, len(l)):
  14.         if l[:i].count('A') == count or l[:i].count('B') == count:
  15.             return l[:i]
  16.     return l
  17.  
  18. if __name__=='__main__':
  19.     if len(sys.argv) > 1:
  20.         length = int(sys.argv[1])
  21.         closed = set()
  22.         for game in gen_games(2*length - 1):
  23.             game = shorten(length, game)
  24.             if game not in closed:
  25.                 closed.add(game)
  26.                 print game
RAW Paste Data
Top