Write your code: 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]

Function definition#

def arithmetic_solver(problems):
    # write your function here
  File "/tmp/ipykernel_2464/1334290660.py", line 2
    # write your function here
                              ^
SyntaxError: unexpected EOF while parsing

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")