0

I'm doing Codewars and got stuck at this simple question. The question was:

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result. It should remove all values from list a, which are present in list b keeping their order. If a value is present in b, all of its occurrences must be removed from the other.

This is what I've tried:

def array_diff(a, b):
    if a == []: return b
    if b == []: return a
    for occurrence in b:
      if occurrence in a:
        a.remove(occurrence)
    return a

and for some reason I got 2 failed tests, the requirements for those failed tests are:

a was [1,2,2], b was [2], expected [1]: [1, 2] should equal [1]

a was [], b was [1,2], expected []: [1, 2] should equal []

can anyone help me and also explain if possible? any help is appreciated. edit: my excuse is that I'm a beginner to Python so, sorry if this question has super obvious errors/mistakes

  • 2
    Wouldn't a simple `return [value for value in a if value not in b]` be enough? – Matthias Nov 27 '21 at 11:06
  • Please do not edit your question to include an answer, you are able to answer your own question if you wish. Or if picchiolu's answer works for you then you can accept it (little green checkmark). – Jared Smith Nov 27 '21 at 11:19
  • ah ok, thank you for the suggestion. Also thank you Matthias for the answer. It worked and was way cleaner than my code – Dương Duy Nhật Minh Nov 27 '21 at 12:26

4 Answers4

1

You can try and modify your code as follows:

def array_diff(a, b):
    if a == []: return a
    if b == []: return a
    for occurrence in b:
        if occurrence in a:
            a = list(filter(lambda val: val != occurrence, a)
    return a
picchiolu
  • 1,120
  • 5
  • 20
0

I fixed it. Here's the code if anyone's stuck on the same question: The first code was provided by Matthias, which was much cleaner, please use it.

def array_diff(a, b):    
    return [value for value in a if value not in b]
    # CREDITS TO MATTHIAS FOR THIS SIMPLE SOLUTION

My code, if anyone's interested:

def array_diff(a, b):
    #if a == []: return b
    #if b == []: return a
    # these if statements are replaced by the"for item in range" code below
    for occurrence in b:
      if occurrence in a:
        for item in range(a.count(occurrence)):
            a.remove(occurrence)
    return a
0

The answer provided did not completely work for me although it was very helpful, this is what i came up with and it worked. Happy Coding!

def array_diff(a, b):
    #your code here
    for i in b: 
        if i == None:
            return a
            break  
        elif i in a:
            for item in range(a.count(i)):
                a.remove(i)
    return a
paigeph
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 24 '22 at 13:20
0

def array_diff(a, b): # your code here if a == []: return b if b == []: return a b = [2, 4] newlist = [] for value in b: if value in a: a.remove(value) newlist.append(a) print(newlist) array_diff(a=[1,2], b=[2])

  • Hello new user! Welcome to StackOverflow :) My question was answered already, but I thank you for your effort. However, please correctly format your code so that it is more readable (You can read this here: https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks). Again, thanks! – Dương Duy Nhật Minh Aug 30 '22 at 13:07
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – mufazmi Sep 04 '22 at 08:10