Last active
January 10, 2017 05:50
-
-
Save the6p4c/5e65c3642469544314b1d974c6e3c54c to your computer and use it in GitHub Desktop.
Graph generator for looking at functions as mentioned in standupmaths' "Four has Four Letters"
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
def create_graph(classification_function, values, highlight_fixed_points=True): | |
connections = {} | |
for n in values: | |
if n not in connections.keys(): | |
connections[n] = classification_function(n) | |
fixed_points = [item[0] for item in connections.items() if item[0] == item[1]] | |
fixed_point_styles = ['\t{0} [style=filled, fillcolor=red, fontcolor=white, fontsize=30];'.format(fixed_point) for fixed_point in fixed_points if highlight_fixed_points] | |
relationships = ['\t{0} -> {1}'.format(item[0], item[1]) for item in connections.items()] | |
dot_file_text = ( | |
'digraph {{\n' | |
'\toverlap=false;\n' | |
'\tsplines=true;\n' | |
'\tdpi=300;\n' | |
'{0}\n' | |
'{1}\n' | |
'}}' | |
).format('\n'.join(fixed_point_styles), '\n'.join(relationships)) | |
return dot_file_text | |
def main(): | |
def binary_letter_count(n): | |
""" | |
Number of characters in binary string | |
i.e. 1001 -> one zero zero one -> 14 | |
""" | |
binary = '{0:b}'.format(n) | |
binary_text = binary.replace('0', 'zero').replace('1', 'one') | |
return len(binary_text) | |
def decimal_letter_count(n): | |
""" | |
Number of characters in naive decimal string | |
i.e 134 -> one three four -> 12 | |
""" | |
DIGITS = { | |
'0': 'zero', | |
'1': 'one', | |
'2': 'two', | |
'3': 'three', | |
'4': 'four', | |
'5': 'five', | |
'6': 'six', | |
'7': 'seven', | |
'8': 'eight', | |
'9': 'nine' | |
} | |
decimal_text = str(n) | |
for digit in DIGITS: | |
decimal_text = decimal_text.replace(digit, DIGITS[digit]) | |
return len(decimal_text) | |
def sum_digits_squared(n): | |
""" | |
Square each of the digits of the number and sum them | |
""" | |
return sum([int(x) ** 2 for x in str(n)]) | |
# Just change the function you want to graph here and the range of values you want to graph it for,. | |
dot_file_text = create_graph(binary_letter_count, range(0, 101)) | |
with open('graph.dot', 'w') as f: | |
f.write(dot_file_text) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment