Task:
You have an 8-wind compass, like this:

You receive the direction you are
facing(one of the 8 directions:N, NE, E, SE, S, SW, W, NW) and a certain degree toturn(a multiple of 45, between -1080 and 1080); positive means clockwise, and negative means counter-clockwise.
Return the direction you will face after the turn.
Examples
"S", 180 --> "N"
"SE", -45 --> "E"
"W", 495 --> "NE"
Solution:
def direction(facing, turn):
dir = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']
turns = turn//45
start_idx = dir.index(facing)
end_idx = (turns+start_idx) % 8
return dir[end_idx]
print(direction('SE', -45))
Turn with a Compass Solutions | Codewars
No comments :
Post a Comment