"""Builds addition and multiplication tables for base-n systems"""
import sys
def represent(i):
"""Represents a single digit in base-n"""
symbs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if i < 36:
return symbs[i]
else:
return "[" + i + "]"
def show_int_at_base(basen, val):
"""Get integer representation in base-n"""
result = ""
while True:
quot = val // basen
rem = val % basen
val = quot
result = represent(rem) + result
if val == 0:
break
return result
def show_base(basen, val):
"""Get representation in base-n padded to 2 symbols"""
result = show_int_at_base(basen, val)
pad = 2
if len(result) < pad:
return ' '*(pad-len(result))+result
else:
return result
def row(oper, basen, i):
"""Get additon/multiplication table row"""
result = "|"
for start in range(0, basen):
result += " " + show_base(basen, oper(start, i)) + " |"
return result
def table(oper, basen):
"""Get addition/multiplication table for base-n"""
result = ""
for start in range(0, basen):
result += "| " + show_base(basen, start) + " " + row(oper, basen, start) + "\n"
return result+"\n"
def md_table(oper, basen):
"""Get markdown-formatted addition/multiplication table"""
return "| " + row((lambda x, y: x*y), basen, 1) + "\n|"+ \
":--:|"*(basen+1)+"\n"+table(oper, basen)
if len(sys.argv) != 2:
print("Usage: ./{0} BASE".format(sys.argv[0]))
exit(0)
BASE = int(sys.argv[1])
print(md_table(lambda x, y: x+y, BASE))
print(md_table(lambda x, y: x*y, BASE))