-3

I am trying to make a program that determines if something is the clockwise direction or counterclockwise direction based on string input that only contains north south east west (n s e w) for example, if I had the string "NESSWN" I know it is clockwise because of drawing it out but I can't figure out a definitive solution applicable to every case for it no matter how hard I think

noer
  • 1

1 Answers1

0

this is how you can show all direction changes, 1 is clock, 0 is no move, 3 is counter clock, and 2 means a jump from N<->S or E<->W. your example is translated into {0,1} which means clockwise. I added another function layer to tell the direction

d={c:i for i,c in enumerate('NESW')}
def direction(s):
    s=s.upper()
    return set([(d[x]-d[y])%4 for x,y in zip(s[1:],s)])
def ClockOrCounter(direction):
    direction=tuple(sorted(direction))
    if direction in ((0,1),(1,)):
        return 'clockwise'
    elif direction in ((0,3),(3,)):
        return 'counterclock'
    return 'undetermined'
print(ClockOrCounter(direction('NESSWN')))
print(ClockOrCounter(direction('NWSEN')))
Bing Wang
  • 1,548
  • 1
  • 8
  • 7
  • Thank you very much for this. It is very helpful, however, I just have one question. So in this something like {0,-1} would mean counterclockwise correct? – noer Feb 28 '21 at 00:33
  • Don't you want %2 actually? N->S is neither, and N->W is -1, not 3. – Mad Physicist Feb 28 '21 at 00:39
  • @MadPhysicist Thanks for the input. I fixed my solution to remove reference to -1 which never happens. I belive a slightly more granular information is better. – Bing Wang Feb 28 '21 at 06:26
  • @noer, the current updated version self-answed your counterclockwise question – Bing Wang Feb 28 '21 at 06:27