Skip to content

Commit 8d8cdb0

Browse files
authored
Merge pull request #2 from hzoscar/develop
Develop
2 parents 28c6353 + ef7c8fd commit 8d8cdb0

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

main.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
def arithmetic_arranger(problems, show_answers=False):
2-
3-
return problems
4-
5-
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49", "99 + 1"])}')
6-
71
def do_operation(first_value,operator,second_value):
82
if operator == "+":
93
return str(int(first_value) + int(second_value))
@@ -17,33 +11,46 @@ def arithmetic_arranger(problems, show_answers=False):
1711
output_second_line_list = []
1812
dash_line = []
1913
output_answer_list = []
20-
separator=" "
21-
14+
separator = " "
15+
if len(problems) > 5:
16+
return "Error: Too many problems."
17+
2218
for problem in problems:
2319
problem = problem.split()
2420
first_line= problem[0]
2521
operator = problem[1]
26-
second_line= problem[2]
27-
len_answer_line = max(len(first_line), len(second_line))+2
28-
answer_line = "_" * len_answer_line
29-
dash_line.append(answer_line)
22+
if operator not in ["+", "-"]:
23+
return "Error: Operator must be '+' or '-'."
24+
second_line= problem[2]
3025

26+
if len(first_line)>=5 or len(second_line)>=5:
27+
return "Error: Numbers cannot be more than four digits."
28+
29+
if not first_line.isdigit() or not second_line.isdigit():
30+
return "Error: Numbers must only contain digits."
31+
32+
bigger_number = max(len(first_line), len(second_line))
33+
difference = abs(len(first_line) - len(second_line))
34+
answer_line = "-" * (bigger_number + 2)
35+
dash_line.append(answer_line)
36+
get_answer = do_operation(first_line, operator, second_line)
37+
spaces = bigger_number + 2 - len(get_answer)
38+
3139
if len(first_line)>len(second_line):
32-
second_line = " " * (len(first_line) - len(second_line)) + second_line
40+
second_line = " " * (difference) + second_line
3341
output_first_line_list.append(" " + first_line)
3442
output_second_line_list.append(operator +' '+ second_line)
35-
output_answer_list.append(" " + " " * (len(first_line) - len(second_line))+do_operation(first_line, operator, second_line))
43+
output_answer_list.append(" " * spaces + get_answer)
3644

3745
elif len(first_line)==len(second_line):
3846
output_first_line_list.append(" " +first_line)
3947
output_second_line_list.append(operator +' '+ second_line)
40-
output_answer_list.append(" " + do_operation(first_line, operator, second_line))
48+
output_answer_list.append(" " * spaces + get_answer)
4149
else:
42-
first_line = " " * (len(second_line) - len(first_line)) + first_line
43-
output_first_line_list.append(" " +first_line)
44-
output_second_line_list.append(operator+' '+ second_line)
45-
output_answer_list.append( " " +" " * (len(second_line) - len(first_line))+ do_operation(first_line, operator, second_line))
46-
50+
first_line = " " * (difference) + first_line
51+
output_first_line_list.append(" " + first_line)
52+
output_second_line_list.append(operator +' '+ second_line)
53+
output_answer_list.append(" " * spaces + get_answer)
4754

4855
final_output = [separator.join(output_first_line_list), separator.join(output_second_line_list), separator.join(dash_line)]
4956
if show_answers:
@@ -52,5 +59,5 @@ def arithmetic_arranger(problems, show_answers=False):
5259
for i in final_output:
5360
print(i, end= '\n')
5461

55-
arithmetic_arranger(problems, show_answers= True)
56-
#arithmetic_arranger(problems)
62+
#return "\n".join(final_output)
63+

test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from main import arithmetic_arranger
2+
problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49","99 + 1"]
3+
arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)
4+
arithmetic_arranger(problems, show_answers= True)
5+
arithmetic_arranger(["3801 - 2", "123 + 49"], show_answers=True)
6+
arithmetic_arranger(["1 + 2", "1 - 9380"], show_answers=True)
7+
arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"], show_answers=True)
8+
arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"], show_answers=True)
9+
arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
10+
arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
11+
arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
12+
arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
13+
arithmetic_arranger(["3 + 855", "988 + 40"], True)
14+
arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)

0 commit comments

Comments
 (0)