Created
December 4, 2012 04:25
-
-
Save brianteachman/4200573 to your computer and use it in GitHub Desktop.
10 row abacus.
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
## | |
# My solution is not the shortest, but it makes sense to me. | |
## | |
def print_abacus(value): | |
abacus, rod, slices = '', '00000*****', [] | |
for char in str(value): | |
# is there any reason not to use a list of dictionaries? | |
slices.append({'left': (10 - int(char)), 'right': (10 - (10 - int(char)))}) | |
rows = len(slices) # does it make sense to cache here? | |
if rows < 10: | |
abacus = "|00000***** |\n" * (10 - rows) | |
for edge in slices: | |
r_edge = '' if edge['right'] == 0 else rod[-edge['right']:] | |
abacus += "|%(left)s %(right)s|\n" % {'left': rod[:edge['left']], 'right': r_edge} | |
print abacus | |
### TEST CASES | |
print "Abacus showing 0:" | |
print_abacus(0) | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
print "Abacus showing 12345678:" | |
print_abacus(12345678) | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000**** *| | |
#>>>|00000*** **| | |
#>>>|00000** ***| | |
#>>>|00000* ****| | |
#>>>|00000 *****| | |
#>>>|0000 0*****| | |
#>>>|000 00*****| | |
#>>>|00 000*****| | |
print "Abacus showing 1337:" | |
print_abacus(1337) | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000***** | | |
#>>>|00000**** *| | |
#>>>|00000** ***| | |
#>>>|00000** ***| | |
#>>>|000 00*****| |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment