-
Notifications
You must be signed in to change notification settings - Fork 0
/
L8Q3_Abacus.py
85 lines (82 loc) · 2.85 KB
/
L8Q3_Abacus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#########################################################################
# 10-row School abacus
# by
# Michael H
#########################################################################
# Description partially extracted from from wikipedia
#
# Around the world, abaci have been used in pre-schools and elementary
#
# In Western countries, a bead frame similar to the Russian abacus but
# with straight wires and a vertical frame has been common (see image).
# Helps schools as an aid in teaching the numeral system and arithmetic
#
# |00000***** | row factor 1000000000
# |00000***** | row factor 100000000
# |00000***** | row factor 10000000
# |00000***** | row factor 1000000
# |00000***** | row factor 100000
# |00000***** | row factor 10000
# |00000***** | row factor 1000
# |00000**** *| row factor 100 * 1
# |00000*** **| row factor 10 * 2
# |00000** ***| row factor 1 * 3
# -----------
# Sum 123
#
# Each row represents a different row factor, starting with x1 at the
# bottom, ascending up to x1000000000 at the top row.
######################################################################
# TASK:
# Define a procedure print_abacus(integer) that takes a positive integer
# and prints a visual representation (image) of an abacus setup for a
# given positive integer value.
#
# Ranking
# 1 STAR: solved the problem!
# 2 STARS: 6 < lines <= 9
# 3 STARS: 3 < lines <= 6
# 4 STARS: 0 < lines <= 3
def print_abacus(value):
beads = {0:'|00000***** |', 1:'|00000**** *|',2:'|00000*** **|',3:'|00000** ***|',4:'|00000* ****|',5:'|00000 *****|',6:'|0000 0*****|',7:'|000 00*****|',8:'|00 000*****|',9:'|0 0000*****|',10:'| 00000*****|'}
for i in range(10-len(str(value))):
value = '0' + str(value) #format value by adding zeros at front
for j in range(len(value)):
print beads.get(int(value[j]))
### 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*****|