Home > database >  Code to remove specfic elements from list doesn't work
Code to remove specfic elements from list doesn't work

Time:03-22

I am trying to solve this codewars problem which involves an input of a list of directions, which include north, south, east and west. The problem consists of creating a function that eliminates the directions inputs that contradict themselves and return them in a list. For example, there is no point in going one unit south and then one unit back north.

I have been trying to solve this problem for a while and I have no idea why it doesn't work. If anyone could give me any help that would be great!

def dirReduc(arr):
    north = 0
    south = 0
    east = 0
    west = 0
    for i in arr:
        if i == "NORTH":
            north  = 1
        if i == "SOUTH":
            south  = 1
        if i == "EAST":
            east  = 1
        if i == "WEST":
            west  = 1
    while (north == 0 or south == 0) or (east == 0 or west == 0):
        if north != 0 and south != 0:
            arr.remove("NORTH")
            arr.remove("SOUTH")
            north -= 1
            south -= 1
        if west != 0 and east != 0:
            arr.remove("EAST")
            arr.remove("WEST")
            east -= 1
            west -= 1
    return arr

EDIT: The actual problem is that the returned array at the end doesn't have the strings removed. It only returns the initial array.

For reference, the inputs tested will look something like this:

["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]

CodePudding user response:

Consider what this line means:

while (north == 0 or south == 0) or (east == 0 or west == 0):

While any of the directions equals 0, keep doing something, right? (The parentheses don't actually do anything here)

However, this example:

["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]

It contains all directions, so none of those counts will be 0 and the loop never even runs once.

You probably meant:

while (north != 0 and south != 0) or (east != 0 and west != 0):

Or the equivalent:

while not ((north == 0 or south == 0) and (east == 0 or west == 0)):

But you could also just avoid a bunch of conditions with:

    while north != 0 and south != 0:
        arr.remove("NORTH")
        arr.remove("SOUTH")
        north -= 1
        south -= 1
    while west != 0 and east != 0:
        arr.remove("EAST")
        arr.remove("WEST")
        east -= 1
        west -= 1

Edit: you asked for clarification on why the loop never runs:

  • you call the function with:
    dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"])
  • so, north == 2, south == 2, east == 1 and west == 2
  • in other words, none of them are 0
  • your loop only runs if:
    (north == 0 or south == 0) or (east == 0 or west == 0)

Is that True, if the values are as stated above? No. So your loop doesn't run. Compare what happens if the condition is changed as I suggested.

Of course, the simpler solution is:

def dirReduc(arr):
    while 'NORTH' in arr and 'SOUTH' in arr:
        arr.remove('NORTH')
        arr.remove('SOUTH')
    while 'EAST' in arr and 'WEST' in arr:
        arr.remove('EAST')
        arr.remove('WEST')
    return arr

There's fancy ways to make the code even shorter, but that would hurt readability.

  • Related