Home > other >  Python Unittest how to add an "OR" for Two Number Sum
Python Unittest how to add an "OR" for Two Number Sum

Time:10-16

I am currently writing test for this problem called twoNumberSum, and the problem simply asks to return the 2 numbers, on the given array, that add up to the target_number. On my tests I currently check if the 2 numbers exists, but how do I check if they are in an array, and how to state that the order does not matter? I have this

import unittest
from problems import two_number_sum

class TestTwoNumberSum(unittest.TestCase):
    def test_two_number_sum(self):
        result = two_number_sum.twoNumberSum([4, 6, 1, -3], 3)
        self.assertTrue(len(result))
        self.assertTrue(6 in result)
        self.assertTrue(-3 in result)
        # self.assertEqual(result, [6, -3])

This line right here, checks if the values are in an array, # self.assertEqual(result, [6, -3])(or at least that's what I think it does since I am new to it). This is the code which works,

def twoNumberSum(array, targetSum):
    array.sort()
    left = 0
    right = len(array) - 1
    while left < right:
        currentSum = array[left]   array[right]
        if currentSum == targetSum:
            return [array[left], array[right]]
            # return array[left], array[right]
        elif currentSum < targetSum:
            left  = 1
        elif currentSum > targetSum:
            right -= 1
    return []

But notice what I get on the terminal,

Traceback (most recent call last):
  File "/Users/jeffersonlopezgarcia/Desktop/App Academy/Diagnostic/tests/two_number_sum_test.py", line 10, in test_two_number_sum
    self.assertEqual(result, [6, -3])
AssertionError: Lists differ: [-3, 6] != [6, -3]

First differing element 0:
-3
6

- [-3, 6]
  [6, -3]

Hence why I want to learn how to say, it could be this OR this other one. Meaning, that the order does NOT matter. Plus, the question says that the order does not matter.

        self.assertTrue(len(result))
        self.assertTrue(6 in result)
        self.assertTrue(-3 in result)

This only checks if: There are 2 numbers, and if the 2 numbers are there the ones that add up to the target_number.

Also, how do I raise errors here? Thanks in advanced!

CodePudding user response:

A simple solution would be to convert your lists to sets for testing. Sets are "unordered" data structures that inherently don't care about the order of their elements.

self.assertEqual(set(result), set([6, -3]))
# you can also just create a set directly like this
self.assertEqual(result, {6, -3})

The unit test library also lets your assert any arbitrary boolean expression is true. Something like this would also work. Sets would work better for lists longer than two, because with this method you have to list every possible permutation of the list in your OR expression.

self.assertTrue(result == [6, -3] or result == [-3, 6])

https://docs.python.org/3.9/library/stdtypes.html#set-types-set-frozenset

CodePudding user response:

If you sort the result you simplify the check because there will only be one result.

self.assertEqual(sorted(result), [-3, 6])

Alternatively you can use or inside the assertTrue().

self.assertTrue(result == [6, -3] or result == [-3, 6])

Another solution would be to compare two sets.

self.assertEqual(set(result), set([6, -3]))
  • Related