#!/usr/bin/env python ''' A puzzle: fill in the blanks with counts of digits that appear in the sentence, including digits from the counts. In this sentence, the number of occurances of 0 is _, of 1 is _, of 2 is _, of 3 is_, of 4 is _, of 5 is _, of 6 is _, of 7 is _, of 8 is _, and of 9 is _. This program finds the counts of occurances of digits in a string (off the command line or chosen randomly) and iteratively attempts to come up with a solution to the puzzle sentence above Puzzle by Raphael Robinson, as mentioned in "Metamagical Themas", Douglas Hofstadter ''' import sys import random try: s = sys.argv[1] except IndexError: # No "seed" string on command line, compose one. s = str(random.randint(0, 1000000000)) previous_strings = set([]) last_string = s print s while s == last_string and s not in previous_strings: previous_strings.add(s) digits = [int(z) for z in s] counts = [0,0,0,0,0,0,0,0,0,0] for n in digits: counts[n] += 1 s = "" for digit, count in enumerate(counts): if count > 0: s = s + str(count) + str(digit) print s # Check to see if the string has correct self-reference if s == last_string: break last_string = s else: print "Cycle"