Created
January 10, 2021 16:29
-
-
Save esouthren/2fe3d71169acceff7c0efcaff89c8816 to your computer and use it in GitHub Desktop.
Hofstadter's Sentences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# Metamagical Themas, Douglas R Hofstadter | |
# Self-Referential Sentences: a follow up | |
# | |
# The noted logician Raphael Robinson submitted a playful puzzle | |
# in the self documenting genre. Readers are asked to complete | |
# the following sentence: | |
# | |
# "In this sentence, the number of occurrences 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 _." | |
# | |
# Each blank is to be filled with a numeral of one or more digits, | |
# written in decimal notation. Robinson states that there are | |
# exactly two solutions. | |
# | |
# esouthren, Jan 2021 | |
# I found one solution; can you find the other? | |
# | |
origin = {0:1, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1} | |
def is_valid_sentence(input): | |
for key in input: | |
char_count = get_char_count(input, key) | |
if char_count != input[key]: | |
return False | |
return True | |
def get_char_count(input, key): | |
count = 0 | |
for key2 in input: | |
for x in [int(d) for d in str(key2)]: | |
if x == key: | |
count += 1 | |
for x in [int(d) for d in str(input[key2])]: | |
if x == key: | |
count += 1 | |
return count | |
def print_sentence(i): | |
print 'In this sentence, the number of occurrences 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 {}.'.format(i[0], i[1],i[2],i[3],i[4],i[5],i[6],i[7],i[8],i[9])) | |
foundAnswer = False | |
while not foundAnswer: | |
for key in origin: | |
if is_valid_sentence(origin): | |
print_sentence(origin) | |
foundAnswer = True | |
break | |
x = get_char_count(origin, key) | |
if x != origin[key]: | |
origin[key] = x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment