Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pyhton based scientific computing #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def arithmetic_arranger(problems):
temp1a,temp2a = [], []
for i in problems:
temp1,op,temp2 = i.split()
if len(temp1) > len(temp2):
temp1a.append(" " + temp1)
temp2a.append(op +" " + " "*(len(temp1)-len(temp2)-1) + temp2)
else:
temp1a.append(" " + " "*(len(temp2) - len(temp1) + 1) + temp1)
temp2a.append(op + " " + temp2)
for i in range(len(temp1a)):
if i < len(temp1a) -1:
print(temp1a[i], end=" ")
else:
print(temp1a[i])
for j in range(len(temp2a)):
if j < len(temp1a) -1:
print(temp2a[j], end=" ")
else:
print(temp2a[j])
for k in temp2a:
print("-"*len(k),end=" ")
return ''
10 changes: 10 additions & 0 deletions Scientific Computing with Python/Arithmetic_Formatter/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This entrypoint file to be used in development. Start by reading README.md
from arithmetic_formatter import arithmetic_formatter
from unittest import main


print(arithmetic_formatter(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))


# Run unit tests automatically
#main(module='test_module', exit=False)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest
from arithmetic_formatter import arithmetic_formatter


# the test case
class UnitTests(unittest.TestCase):
def test_arrangement(self):
actual = arithmetic_formatter(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
expected = " 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----"
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_formatter()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')

actual = arithmetic_formatter(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
expected = " 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------"
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_formatter()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]')

def test_too_many_problems(self):
actual = arithmetic_formatter(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
expected = "Error: Too many problems."
self.assertEqual(actual, expected, 'Expected calling "arithmetic_formatter()" with more than five problems to return "Error: Too many problems."')

def test_incorrect_operator(self):
actual = arithmetic_formatter(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
expected = "Error: Operator must be '+' or '-'."
self.assertEqual(actual, expected, '''Expected calling "arithmetic_formatter()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')

def test_too_many_digits(self):
actual = arithmetic_formatter(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
expected = "Error: Numbers cannot be more than four digits."
self.assertEqual(actual, expected, 'Expected calling "arithmetic_formatter()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."')

def test_only_digits(self):
actual = arithmetic_formatter(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
expected = "Error: Numbers must only contain digits."
self.assertEqual(actual, expected, 'Expected calling "arithmetic_formatter()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')

def test_solutions(self):
actual = arithmetic_formatter(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)
expected = " 32 1 45 123\n- 698 - 3801 + 43 + 49\n----- ------ ---- -----\n -666 -3800 88 172"
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_formatter()" with arithemetic problems and a second argument of `True`.')

if __name__ == "__main__":
unittest.main()