@@ -2,4 +2,55 @@ def arithmetic_arranger(problems, show_answers=False):
22
33 return problems
44
5- print (f'\n { arithmetic_arranger (["32 + 698" , "3801 - 2" , "45 + 43" , "123 + 49" ])} ' )
5+ print (f'\n { arithmetic_arranger (["32 + 698" , "3801 - 2" , "45 + 43" , "123 + 49" , "99 + 1" ])} ' )
6+
7+ def do_operation (first_value ,operator ,second_value ):
8+ if operator == "+" :
9+ return str (int (first_value ) + int (second_value ))
10+ else :
11+ return str (int (first_value ) - int (second_value ))
12+
13+ problems = ["32 + 698" , "3801 - 2" , "45 + 43" , "123 + 49" ,"99 + 1" ]
14+
15+ def arithmetic_arranger (problems , show_answers = False ):
16+ output_first_line_list = []
17+ output_second_line_list = []
18+ dash_line = []
19+ output_answer_list = []
20+ separator = " "
21+
22+ for problem in problems :
23+ problem = problem .split ()
24+ first_line = problem [0 ]
25+ 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 )
30+
31+ if len (first_line )> len (second_line ):
32+ second_line = " " * (len (first_line ) - len (second_line )) + second_line
33+ output_first_line_list .append (" " + first_line )
34+ 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 ))
36+
37+ elif len (first_line )== len (second_line ):
38+ output_first_line_list .append (" " + first_line )
39+ output_second_line_list .append (operator + ' ' + second_line )
40+ output_answer_list .append (" " + do_operation (first_line , operator , second_line ))
41+ 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+
47+
48+ final_output = [separator .join (output_first_line_list ), separator .join (output_second_line_list ), separator .join (dash_line )]
49+ if show_answers :
50+ final_output .append (separator .join (output_answer_list ))
51+
52+ for i in final_output :
53+ print (i , end = '\n ' )
54+
55+ arithmetic_arranger (problems , show_answers = True )
56+ #arithmetic_arranger(problems)
0 commit comments