Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 28th, 2012  |  syntax: Python  |  size: 0.65 KB  |  views: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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