Last active
December 12, 2015 01:58
A solution to SMBC #2874. http://www.smbc-comics.com/index.php?db=comics&id=2874#comic
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
import string | |
# Algorithm for SMBC #2874 - the "fouriest" transform. | |
# Support bases 2-36. | |
BASE_LIST = string.digits + string.letters | |
def encode(integer, base): | |
# Stolen off the internet from somewhere, | |
# almost certainly guaranteed to be buggy. | |
base_list = BASE_LIST[:base] | |
ret = '' | |
while integer != 0: | |
ret = base_list[integer % base] + ret | |
integer /= base | |
return ret | |
def transform(inp): | |
# inp (integer) - A base-10 encoded integer to be transformed. | |
# Returns (best_string, best_base) tuple. | |
inp_str = str(inp) | |
best_base = '10' | |
best_string = inp_str | |
max_num_fours = inp_str.count('4') | |
for i in range(5, len(BASE_LIST)): | |
# No base less than 5 will contain any '4' characters. | |
tmp = encode(inp, i) | |
cnt = tmp.count('4') | |
if cnt > max_num_fours: | |
max_num_fours = cnt | |
best_base = str(i) | |
best_string = tmp | |
return best_string, best_base | |
# Usage: | |
# fouriest.transform(624) | |
# ('4444', '5') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment