Solution: Arithmetic Operation#

Create a function that receives a list of strings that are arithmetic problems and returns the answers.

Rules#

  1. Input should be a list of string(s).

  2. Every string should have a integer number, an operator (+ or -), and another integer number - all separated by a single space.

  3. The function should return a list of integer(s), where every integer is the solution for the respective arithmetic operation.

  4. For any non-valid operation, the result should be None.

Example#

Function Call

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

Return:

[730, 3799, 88, None]

Solution 1#

In this solution every step is created “manually” and stored in a list.

If it is a downward staircase, the list is reversed.

def arithmetic_solver(problems):

    operand1 = [problem.split()[0] for problem in problems]
    operator = [problem.split()[1] for problem in problems]
    operand2 = [problem.split()[2] for problem in problems]

    result = []
    for i in range(len(problems)):
        if not operand1[i].isdigit():
            result.append(None)
        elif not operand2[i].isdigit():
            result.append(None)
        elif operator[i]=="+":
            result.append(int(operand1[i]) + int(operand2[i]))
        elif operator[i]=="-":
            result.append(int(operand1[i]) - int(operand2[i]))
        else:
            result.append(None)
    return result
arithmetic_solver(["32 + 698", "3801 - 2", "45 + 43", "123 + A"])
[730, 3799, 88, None]

Testing#

Check if your function returns the expected value using the cell below.

import unittest

class UnitTests(unittest.TestCase):

    def test_type(self):
        self.assertTrue(isinstance(arithmetic_solver(["32 + 698", "3801 - 2", "45 + 43", "123 + A"]), list), 'The function should return a list')

    def test_item_type(self):
        self.assertTrue(all(isinstance(item, int) for item in arithmetic_solver(["32 + 13", "301 - 25", "5 + 43"])), 'The function should return a list of integers')

    def test_sum(self):
        self.assertEqual(arithmetic_solver(["0 + 0", "5418 + 1", "0 + 12"]), [0, 5419, 12])

    def test_sub(self):
        self.assertEqual(arithmetic_solver(["0 - 0", "5418 - 1", "0 - 12"]), [0, 5417, -12])

    def test_mix(self):
        self.assertEqual(arithmetic_solver(["1 + 1", "5418 - 1", "0 - 12"]), [2, 5417, -12])

    def test_error_operator(self):
        self.assertEqual(arithmetic_solver(["1 + 1", "5418 - 1", "0 * 12"]), [2, 5417, None], "Operators different from + and - are invalid")

    def test_error_operand1(self):
        self.assertEqual(arithmetic_solver(["1 + 1", "5418.0 - 1", "0 + 12.0"]), [2, None, None], "Operants different from int are invalid")

    def test_error_operand2(self):
        self.assertEqual(arithmetic_solver(["1 + 1", "A - 1", "0 + B"]), [2, None, None], "Operants different from int are invalid")


unittest.main(argv=[''], verbosity=2,exit=False)
test_error_operand1 (__main__.UnitTests) ... ok
test_error_operand2 (__main__.UnitTests) ... ok
test_error_operator (__main__.UnitTests) ... ok
test_item_type (__main__.UnitTests) ... ok
test_mix (__main__.UnitTests) ... ok
test_sub (__main__.UnitTests) ... ok
test_sum (__main__.UnitTests) ... ok
test_type (__main__.UnitTests) ... ok

----------------------------------------------------------------------
Ran 8 tests in 0.004s

OK
<unittest.main.TestProgram at 0x7fd2a5b40f70>