Собственно, рекурсивно составляет все возможные партии; выводит в консоль каждую 100 000 партию.
def print_game(path): global wins sum_w = sum(wins) % 100000 turns = 0 if not sum_w: cur_elem = 0 cur_cnt = 0 for e in path: turns += 1 if not sum_w: if e == cur_elem: cur_cnt += 1 else: if cur_elem != 0: if cur_cnt == 1: print "%d, " % cur_elem, else: print "%d {%d}, " % (cur_elem, cur_cnt), cur_elem = e cur_cnt = 1 if not sum_w: if cur_cnt == 1: print "%d, " % cur_elem, else: print "%d {%d}, " % (cur_elem, cur_cnt), global min_turns global min_turns_sequences if turns < min_turns: min_turns = turns min_turns_sequences = [path] elif turns == min_turns: min_turns_sequences.append(path) if turns % 2 == 0: wins[1] += 1 else: wins[0] += 1 if not sum_w: print "[%s player wins]" % ("Second" if turns % 2 == 0 else "First"), print "" squares = [a*a for a in range(1, int(math.sqrt(197)))] min_turns = 5 min_turns_sequences = [] wins = [0, 0] games() print "We have total %d games, %d ones where First Player wins and %d one where Second does" % (sum(wins), wins[0], wins[1]) print min_turns_sequences