-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-arithmetic-formatter-project.py
122 lines (80 loc) · 4.13 KB
/
06-arithmetic-formatter-project.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
Instructions = """
Build an Arithmetic Formatter Project
Students in primary school often arrange arithmetic problems vertically
to make them easier to solve. For example, "235 + 52" becomes:
235
+ 52
-----
Finish the 'arithmetic_arranger' function that receives a list of strings which are arithmetic problems,
and returns the problems arranged vertically and side-by-side. The function should optionally take a
second argument. When the second argument is set to True, the answers should be displayed.
Example:
Function Call:
arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])
Output:
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
Function Call:
arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)
Output:
32 1 9999 523
+ 8 - 3801 + 9999 - 49
---- ------ ------ -----
40 -3800 19998 474
Rules:
The function will return the correct conversion if the supplied problems are properly formatted,
otherwise, it will return a string that describes an error that is meaningful to the user.
Situations that will return an error:
- If there are too many problems supplied to the function.
The limit is five, anything more will return: 'Error: Too many problems.'
- The appropriate operators the function will accept are addition and subtraction.
Multiplication and division will return an error. Other operators not mentioned in
this bullet point will not need to be tested. The error returned will be: "Error: Operator must be '+' or '-'."
- Each number (operand) should only contain digits. Otherwise, the function will return: 'Error: Numbers must only contain digits.'
- Each operand (aka number on each side of the operator) has a max of four digits in width.
Otherwise, the error string returned will be: 'Error: Numbers cannot be more than four digits.'
- If the user supplied the correct format of problems, the conversion you return will follow these rules:
- There should be a single space between the operator and the longest of the two operands,
the operator will be on the same line as the second operand, both operands will be in the
same order as provided (the first will be the top one and the second will be the bottom).
- Numbers should be right-aligned.
- There should be four spaces between each problem.
- There should be dashes at the bottom of each problem.
The dashes should run along the entire length of each problem individually.
(The example above shows what this should look like.)
"""
def arithmetic_arranger(problems, display_answers=False):
if len(problems) > 5:
return 'Error: Too many problems.'
first_line = []
second_line = []
dashes = []
answers = []
for problem in problems:
parts = problem.split()
if len(parts) != 3:
return "Error: Invalid format."
num1, operator, num2 = parts
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
if not (num1.isdigit() and num2.isdigit()):
return 'Error: Numbers must only contain digits.'
if len(num1) > 4 or len(num2) > 4:
return 'Error: Numbers cannot be more than four digits.'
width = max(len(num1), len(num2)) + 2 # Two extra spaces for operator and space
first_line.append(num1.rjust(width))
second_line.append(operator + ' ' + num2.rjust(width - 2))
dashes.append('-' * width)
if display_answers:
result = str(eval(problem)) # Evaluating the mathematical expression
answers.append(result.rjust(width))
arranged_problems = " ".join(first_line) + "\n" + \
" ".join(second_line) + "\n" + \
" ".join(dashes)
if display_answers:
arranged_problems += "\n" + " ".join(answers)
return arranged_problems
# Test Examples
print(arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True))
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))