Skip to content

Instantly share code, notes, and snippets.

@mrdmnd
Last active December 12, 2015 01:58

Revisions

  1. mrdmnd renamed this gist Feb 2, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. mrdmnd created this gist Feb 2, 2013.
    37 changes: 37 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    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')