0

I have created a function which takes two parameters computes them and returns a list with each element set to the sum of the parameter n and the corresponding value in the input list. Here is my code for the function:

    def add_n_all(L,n):
        listx = map(lambda x:x + n, L)
        return listx

The code to test my function looks like this:

    def testmap_1(self):
        L = [2, 3, 4]
        n = 2
        L2 = map.add_n_all(L,n)
        self.assertListAlmostEqual(L2, [4, 2, 5, 3, 6, 4])

    def testmap_1(self):
        L = [2, 3, 4]
        n = 2
        L2 = map.add_n_all(L,n)
        self.assertListAlmostEqual(L2, [3, 1, 6, 4, 8, 6])

However when I run my test I keep getting this error. I've tried to change the variables around but it doesn't seem to work so I'm not exactly sure what I'm doing wrong.

FAIL: testmap_1 (main.TestCases)

     Traceback (most recent call last):
        File "map_tests.py", line 33, in testmap_1
        self.assertListAlmostEqual(L2, [3, 1, 6, 4, 8, 6])
     File "map_tests.py", line 7, in assertListAlmostEqual
        self.assertEqual(len(l1), len(l2))
     AssertionError: 3 != 6

   Ran 3 tests in 0.001s

   FAILED (failures=1)
bpp
  • 3
  • 2
  • 1
    Your tests have the same names. Which one failed? – fips Apr 30 '16 at 22:01
  • Why do you expect there to be *6* returned values when the list going into the function has only got 3? – Martijn Pieters Apr 30 '16 at 22:03
  • 2
    I'm not sure what the relationship is between `L` and your expected lists you are testing with. Adding `2` to each element in `L` you'd expect a list with `[4, 5, 6]`, not `[3, 1, 6, 4, 8, 6]`. – Martijn Pieters Apr 30 '16 at 22:04
  • Insert `print(L2)` before the assert so that you can see what the function is doing, then it will be clear what's wrong. – Alex Hall Apr 30 '16 at 22:05
  • @MartijnPieters it's because OP copied the test case but forgot to change `L`. It should be `[1, 4, 6]`. – Alex Hall Apr 30 '16 at 22:07
  • Use `L.insert(index, value)` to solve your problem. `map` will not be useful, just use a for loop. – Alex Hall Apr 30 '16 at 22:08

2 Answers2

1

Your function doesn't add length, so you are comparing a list with 3 elements(L2) to a list with 6 elements, which are obviously not equal.

Natecat
  • 2,175
  • 1
  • 17
  • 20
0

However when I run my test I keep getting this error.

AssertionError in Python is raised when a test (or in other words an assert fails.

in your assertListAlmostEqual there is a check on the length of input and output lists.

 self.assertEqual(len(l1), len(l2))

In your test case the input

L = [2, 3, 4]

is of length 3.

and length of map() function's output list will be same as the length of its input. So, comparing the length of your output L2 (which is 3) with the length of [3, 1, 6, 4, 8, 6] (which is 6) would obviously fail the assertion.

So, in your test case you need to rectify what you compare your L2 with.

Changing

self.assertListAlmostEqual(L2, [3, 1, 6, 4, 8, 6])

to

self.assertListAlmostEqual(L2, [4, 5, 6])

passes the test (unless your intention is to test negative cases, in which case you need to use assertNotEqual).

OR

If your intention is to get an output list with +n on each element and also the corresponding element from the list then you can do one of the following (not writing the code...this is your HW rt...):

def add_n_all(L,n):
    # Consturct an empty list, which will be used to return as the output of this function
    # for each element in the list
        # do .extend() with [element+n, element] on the list initialized earlier for the purpose of this function return

    # return the output list

OR

def add_n_all(L,n):
    # good luck figuring out this list comprehension
    return [item for sublist in [[x+n, x] for x in L] for item in sublist]
LearnerEarner
  • 1,020
  • 6
  • 12
  • I'm not sure if I'm mistaken by my teacher's instructions but from what's written in the instructions it seems that the function's output should be the sum of the parameter n and the corresponding value in the input list therefore would there be two outputs for every input? Or is not possible to have an input list of 3 which returns a list of 6, or could I possibly be mistaken by the instructions? Sorry I'm still a beginner at Python so I'm trying to get through the basics. – bpp May 01 '16 at 01:04
  • ah..understood. You can have a function with a list of size 3 as input and list of size 6 as output but `map()` is not that function. Updated my answer (intentionally gave you hints but not the actual solution) – LearnerEarner May 01 '16 at 01:38
  • Okay great thanks for your help, I had to write three functions and one of them had to use map() but now i see it won't work for this function thanks! – bpp May 01 '16 at 02:45
  • Sure. And if my answer or any other answer helped you resolve or bail out of the problem please accept the answer – LearnerEarner May 01 '16 at 02:51