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
Asked
Active
Viewed 186 times
-3
-
Is it possible that there is no definitive solution? – Mad Physicist Feb 28 '21 at 00:37
-
I'm sure there is a definitive solution since this problem was sent to me by a friend. I just asked him regarding this question and he said there is definitely a solution – noer Feb 28 '21 at 00:38
-
1To this one specific input sure, but that's not what "definitive solution" generally means. – Mad Physicist Feb 28 '21 at 00:40
-
1What happens if I give you "NS"? Or "NESEN"? – Mad Physicist Feb 28 '21 at 00:40
1 Answers
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