-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabacus.py
More file actions
executable file
·95 lines (90 loc) · 3 KB
/
Copy pathabacus.py
File metadata and controls
executable file
·95 lines (90 loc) · 3 KB
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
86
87
88
89
90
91
92
93
94
95
#########################################################################
# 10-row School abacus
#
#########################################################################
# 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.
######################################################################
# The procedure print_abacus(integer) takes a positive integer
# and prints a visual representation (image) of an abacus setup for a
# given positive integer value.
#SOLUTION ONE
#def print_abacus(value):
# lo = []
# if value == 0:
# print('|00000***** |\n'*10)
# else:
# g = len(str(value))
# while value >= 1:
# p = '|00000*****|'
# i = 11
# k = value%10
# lo.append(p[:i-k]+" "*3+p[i-k:])
# value = value / 10
# print('|00000***** |\n'*(10-g)),
# while g > 0:
# print lo.pop()
# g-=1
#SOLUTION TWO
def print_abacus(value):
arr = ['|00000***** |','|00000**** *|','|00000*** **|','|00000** ***|','|00000* ****|','|00000 *****|','|0000 0*****|','|000 00*****|','|00 000*****|','|0 0000*****|']
s = '0'*(10-len(str(value)))+str(value)
print "\n".join([arr[int(i)] for i in s])
### 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*****|