-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbacus.py
128 lines (115 loc) · 3.17 KB
/
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class Abacus:
def add(self, original, val):
top = []
for line in original:
value = 0;
for i in range(11, -1, -1):
if line[i] != '-':
value += 1
else:
break
top.append(str(value))
result = str(int("".join(top)) + val)
result_str = []
for i in range (6):
result_str.append('o' * (9 - int(result[i])) + '---' + 'o' * int(result[i]))
return result_str;
test = Abacus()
print(test.add(["ooo---oooooo","---ooooooooo","---ooooooooo","---ooooooooo","oo---ooooooo","---ooooooooo"], 5))
print(test.add(["ooo---oooooo",
"---ooooooooo",
"---ooooooooo",
"---ooooooooo",
"oo---ooooooo",
"---ooooooooo"], 21))
"""
Problem Statement
An abacus can be used to do arithmetic. The version that we have has 6 horizontal threads, each with nine beads on it.
The beads on each thread are always arranged with just one gap, possibly at one of the ends.
However many beads are adjacent and at the right end of the thread is the digit value of the thread.
The value on the abacus is read by taking the digits in order from top thread to bottom thread and arranging them from left to right
(so the top thread is the one that contains the most significant digit).
Create a class Abacus that contains a method add that is given a String[] original and a number val and
that returns a String[] showing the abacus after val has been added to the original abacus.
Both in original and in the return,
the String[] will contain exactly 6 elements representing the 6 threads in order from top thread to bottom thread.
Each element will contain a lowercase 'o' to represent each bead and three consecutive hyphens '-' to indicate the empty part of the thread.
Each element will thus contain exactly 12 characters.
Definition
Class: Abacus
Method: add
Parameters: String[], int
Returns: String[]
Method signature: String[] add(String[] original, int val)
(be sure your method is public)
Constraints
- original will contain exactly 6 elements.
- Each element of original will contain exactly 12 characters, 9 lowercase 'o's and 3 consecutive '-'s.
- val will be between 0 and 999,999 inclusive.
- val added to the original abacus will result in a value that can be shown on the abacus.
Examples
0)
{"ooo---oooooo",
"---ooooooooo",
"---ooooooooo",
"---ooooooooo",
"oo---ooooooo",
"---ooooooooo"}
5
Returns:
{"ooo---oooooo",
"---ooooooooo",
"---ooooooooo",
"---ooooooooo",
"o---oooooooo",
"ooooo---oooo" }
When we add 5 to the original, it is necessary to "carry" 1 to the next thread up.
This shows the arithmetic 699979 + 5 = 699984
1)
{"ooo---oooooo",
"---ooooooooo",
"---ooooooooo",
"---ooooooooo",
"oo---ooooooo",
"---ooooooooo"}
21
Returns:
{"oo---ooooooo",
"ooooooooo---",
"ooooooooo---",
"ooooooooo---",
"ooooooooo---",
"ooooooooo---" }
This shows 699979 + 21 = 700000
2)
{"ooooooooo---",
"---ooooooooo",
"ooooooooo---",
"---ooooooooo",
"oo---ooooooo",
"---ooooooooo"}
100000
Returns:
{"oooooooo---o",
"---ooooooooo",
"ooooooooo---",
"---ooooooooo",
"oo---ooooooo",
"---ooooooooo" }
3)
{"o---oooooooo",
"---ooooooooo",
"---ooooooooo",
"---ooooooooo",
"---ooooooooo",
"---ooooooooo"
}
1
Returns:
{"---ooooooooo",
"ooooooooo---",
"ooooooooo---",
"ooooooooo---",
"ooooooooo---",
"ooooooooo---" }
"""