'Comparing items of lists using assertEqual
list_num = [4,3,2,7,8,2,3,1]
new_list = set()
def duplicado(list_num):
for i in list_num:
if list_num.count(i) >= 2:
new_list.add(i)
print(list(new_list))
print(list_num)
duplicado(list_num)
import unittest
class Pruebas(unittest.TestCase):
def test(self):
self.assertEqual(duplicado(list_num),new_list)
unittest.main(argv=['first-arg-is-ignored'], exit=False)
I have an error that says:
self.assertEqual(duplicado(list_num),new_list)
this is the error I'm been given. I want to assert the values of new_list[2,3] to the values of list_num. It's an exercise class, the question as follows:
Find All Duplicates in an Array
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Example:
Input: [4,3,2,7,8,2,3,1]
Output: [2,3]
I've tried asserCountEqual, and assertEqual
AssertionError: None != {2, 3}
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Solution 1:[1]
Your error is: AssertionError: None != {2, 3} because of this line here:
self.assertEqual(duplicado(list_num),new_list)
You're checking whether the return value of duplicado(list_num) equals new_list.
The problem is that your function's return value is None because there is no return statement.
Jailton Silva's comment isn't quite right either because you would just be returning the list you got as a parameter without changing anything.
To fix this put new_list = set() inside the duplicado function, swap the print(list(new_list)) to return list(new_list) and remove the print(list_num) line.
Also, when unit testing you should check against fixed values so in the self.assertEqual(duplicado(list_num),new_list) line you should make it self.assertEqual(duplicado(list_num),[2,3])
Solution 2:[2]
Your duplicado function is returning None. You must return new_list
list_num = [4,3,2,7,8,2,3,1]
new_list = set()
def duplicado(list_num):
for i in list_num:
if list_num.count(i) >= 2:
new_list.add(i)
print(list(new_list))
print(list_num)
return new_list # <-- This return was missing
import unittest
class Pruebas(unittest.TestCase):
def test(self):
self.assertEqual(duplicado(list_num), new_list) # It will be used here
unittest.main(argv=['first-arg-is-ignored'], exit=False)
Solution 3:[3]
You have a number of issues with your code. As others have stated, your duplicado function is not returning anything.
Fix that, then ask yourself what the purpose of AssertEquals is. This should be comparing the result of running your code against what you expect the answer to be. That is, the AssertEquals is testing if you got the right answer. But where is that right answer in your code? You haven't supplied what you think is the right answer to your code, so how can it check for that? You need to include the correct answer in your code.
Here's a version of your program that solves these and other issues:
list_num = [4,3,2,7,8,2,3,1]
def duplicado(list_num):
new_list = set()
for i in list_num:
if list_num.count(i) >= 2:
new_list.add(i)
return sorted(list(new_list))
import unittest
# Correct result must be in ascending sorted order
correct = [2, 3]
class Pruebas(unittest.TestCase):
def test(self):
result = duplicado(list_num)
self.assertEqual(result, correct)
unittest.main(argv=['first-arg-is-ignored'], exit=False)
Result:
Ran 0 tests in 0.000s
OK
Ran 1 test in 0.002s
OK
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 |
