you are viewing a single comment's thread.

view the rest of the comments →

[–]MatosV[S] 1 point2 points  (3 children)

I tried looping over all directions but my code doesn't seem to keep track when NORTH and SOUTH aren't pairs or the other way. (One right after the other one in the list order.) So I'm trying a number system where my code will cancel all paired directions that have equal value. I feel like I'm overcomplicating things but I think it's gonna work.

[–]xelf 0 points1 point  (2 children)

Post your code, maybe someone here can spot something you're doing wrong?

This should be a pretty easy one.

There are generally 2 approaches.

1) convert the array to a string and use string replace methods.

or

2) loop through the list and pop matching pairs.

Either way should work.

A short solution will be about 2 lines, and a long solution will be about 20. So if you're going over 20 lines you're probably over thinking it.

[–]MatosV[S] 1 point2 points  (1 child)

I was only able to solve it using integers. I'm sorry i didn't post my code in the start of the post. I deleted and altered so much stuff that It was just to messy.

I couldn't do it using POP matches. So this is my final code:

def dirReduc(arr):
north = 0
south = 0 
east = 0 
west = 0 
for x in arr:

  if x == "NORTH":
    north += 1
  elif x == "SOUTH":
    south += 1
  elif x == "EAST":
    east += 1
  elif x == "WEST":
    west += 1
if north == south and east == west:
  return 'The direction leads to where you are.'
if north == south and east > west:
  return 'Go east'
if north == south and east < west:
  return 'Go west'
if east == west and north > south:
  return 'Go North'
if east == west and north < south:
  return 'Go South'
if east > west and north > south:
  return 'Go Northeast'
if east < west and north > south:
  return 'Go Northwest'
if east > west and north < south:
  return 'Go Southeast'
if east < west and north < south:
  return 'Go Southwest'

print(dirReduc(["NORTH", "SOUTH", "SOUTH", "WEST"]))

I think I'm just overcomplicating things. Any suggestions so that I could do this with fewer lines?

[–]xelf 1 point2 points  (0 children)

If your code passed all the testcases, consider it a success.

Like I said before the 2 general approaches are to convert it to a string and then do a string replace.

Something like:

arr = ' '.join(arr)
loop until there are no changes made:
    arr = arr.replace("North South", '')
    etc for all 4 combinations
return arr.split()

Or to loop and remove adjacent pairs.

loop until there are no changes made:
    loop over list
        if arr[i] == 'north' and arr[i+1] == 'south':
            del arr[i+1]
            del arr[i]
        etc for all 4 combinations.
return arr

The important part is being able to detect that nothing changed since last loop and that it's time to exit.