Skip to content

Instantly share code, notes, and snippets.

@mrdmnd
Last active December 12, 2015 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdmnd/4695092 to your computer and use it in GitHub Desktop.
Save mrdmnd/4695092 to your computer and use it in GitHub Desktop.
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