Skip to content

Commit 98b6f98

Browse files
authored
Add files via upload
1 parent ba02daa commit 98b6f98

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

arithmetic_arranger.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This code was made by NIDHAL LABRI #
2+
def arithmetic_arranger(problems, show_answers=False):
3+
if len(problems) > 5:
4+
return "Error: Too many problems."
5+
6+
arranged_problems = ""
7+
line1 = line2 = line3 = line4 = ""
8+
9+
for problem in problems:
10+
num1, operator, num2 = problem.split()
11+
#Errors check
12+
if operator not in ['+', '-']:
13+
return "Error: Operator must be '+' or '-'."
14+
if not num1.isdigit() or not num2.isdigit():
15+
return "Error: Numbers must only contain digits."
16+
if len(num1) > 4 or len(num2) > 4:
17+
return "Error: Numbers cannot be more than four digits."
18+
19+
width = max(len(num1), len(num2)) + 2
20+
line1 += num1.rjust(width) + " "
21+
line2 += operator + " " + num2.rjust(width - 2) + " "
22+
line3 += "-" * width + " "
23+
24+
if show_answers:
25+
if operator == '+':
26+
answer = str(int(num1) + int(num2))
27+
else:
28+
answer = str(int(num1) - int(num2))
29+
line4 += answer.rjust(width) + " "
30+
31+
arranged_problems = line1.rstrip() + "\n" + line2.rstrip() + "\n" + line3.rstrip()
32+
33+
if show_answers:
34+
arranged_problems += "\n" + line4.rstrip()
35+
36+
return arranged_problems

main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This entrypoint file to be used in development. Start by reading README.md
2+
from arithmetic_arranger import arithmetic_arranger
3+
from unittest import main
4+
5+
6+
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))
7+
8+
9+
# Run unit tests automatically
10+
main(module='test_module', exit=False)

test_module.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import unittest
2+
from arithmetic_arranger import arithmetic_arranger
3+
4+
5+
# the test case
6+
class UnitTests(unittest.TestCase):
7+
def test_arrangement(self):
8+
actual = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
9+
expected = " 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----"
10+
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
11+
12+
actual = arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
13+
expected = " 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------"
14+
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]')
15+
16+
def test_too_many_problems(self):
17+
actual = arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
18+
expected = "Error: Too many problems."
19+
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."')
20+
21+
def test_incorrect_operator(self):
22+
actual = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
23+
expected = "Error: Operator must be '+' or '-'."
24+
self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')
25+
26+
def test_too_many_digits(self):
27+
actual = arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
28+
expected = "Error: Numbers cannot be more than four digits."
29+
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."')
30+
31+
def test_only_digits(self):
32+
actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
33+
expected = "Error: Numbers must only contain digits."
34+
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')
35+
36+
def test_solutions(self):
37+
actual = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)
38+
expected = " 32 1 45 123\n- 698 - 3801 + 43 + 49\n----- ------ ---- -----\n -666 -3800 88 172"
39+
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
40+
41+
42+
if __name__ == "__main__":
43+
unittest.main()

0 commit comments

Comments
 (0)