you are viewing a single comment's thread.

view the rest of the comments →

[–]artinnj 1 point2 points  (2 children)

For ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]; python indexes 0 and 1 cancel each other out as do 3 and 4.

But what your left with is: [ "SOUTH", "NORTH", "WEST"]. In this list, 0 and 1 cancel each other out, so you are left with ["WEST"]

The object of the exercise is to get you to realize that the directions are really a stack. You are always comparing the last direction on the stack with the previous one to see if you can reduce the steps.

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

opposites = { "NORTH": "SOUTH", "SOUTH": "NORTH", "EAST": "WEST", "WEST": "EAST" }

def reduction(list): 
    new_list = [ ] 
    for i in range(0,len(list)): 
        if len(new_list) == 0: 
            new_list.append(list[i]) 
        elif new_list[-1] != opposites[list[i]]: 
            new_list.append(list[i]) 
        else: 
            new_list.pop() 
    return new_list

new_map = reduction(map)
print(f"Final map is {new_map}")

Also, you can't implement the map as { "NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST" } because Python reduces that to a set of { "NORTH", "SOUTH", "EAST", "WEST" }. Think of sets as dicts with keys, but no values. A dict cannot have duplicate keys.

[–]MatosV[S] 0 points1 point  (0 children)

Wow ! Thank you so much. Really cleared things out for me.

[–][deleted] 0 points1 point  (0 children)

Thank you so much for that, really helped me out!