Saturday, April 23, 2022

Turn with a Compass Python Codewars

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 to turn (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



Related Posts


No comments :

Post a Comment