Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Last active June 19, 2020 01:26
Show Gist options
  • Save ZechCodes/578646eb630018cf4421813038405372 to your computer and use it in GitHub Desktop.
Save ZechCodes/578646eb630018cf4421813038405372 to your computer and use it in GitHub Desktop.
Challenge #32 - Compass Directions

Challenge #32 - Compass Directions

You face 1 out of the 4 compass directions: N, S, E or W.

  • A left turn is a counter-clockwise turn. e.g. N (left-turn) ➞ W.
  • A right turn is a clockwise turn. e.g. N (right-turn) ➞ E.

Create a function that takes in a starting direction and a sequence of left and right turns, and outputs the final direction faced.

Examples

final_direction("N", ["L", "L", "L"]) ➞ "E"

final_direction("N", ["R", "R", "R", "L"]) ➞ "S"

final_direction("N", ["R", "R", "R", "R"]) ➞ "N"

final_direction("N", ["R", "L"]) ➞ "N"

Notes

  • You can only face 1 out of the 4 compass directions: N, S, E or W.
import unittest
from typing import List
def final_direction(facing: str, turns: List[str]) -> str:
return "" # Put your code here
class TestFinalDirection(unittest.TestCase):
def test_1(self):
self.assertEqual(final_direction('N', ['L', 'L', 'L']), 'E')
def test_2(self):
self.assertEqual(final_direction('N', ['R', 'R', 'R', 'R', 'R', 'R', 'R']), 'W')
def test_3(self):
self.assertEqual(final_direction('N', ['R', 'R', 'R', 'L']), 'S')
def test_4(self):
self.assertEqual(final_direction('N', ['R', 'R', 'R', 'R']), 'N')
def test_5(self):
self.assertEqual(final_direction('N', ['R', 'L']), 'N')
def test_6(self):
self.assertEqual(final_direction('S', ['R', 'L', 'R', 'L', 'R']), 'W')
def test_7(self):
self.assertEqual(final_direction('S', ['R', 'L', 'R', 'L', 'R', 'L']), 'S')
def test_8(self):
self.assertEqual(final_direction('S', ['R', 'L', 'R', 'L', 'L', 'L']), 'N')
def test_9(self):
self.assertEqual(final_direction('S', ['R', 'R']), 'N')
def test_10(self):
self.assertEqual(final_direction('S', ['R']), 'W')
def test_11(self):
self.assertEqual(final_direction('S', ['L']), 'E')
def test_12(self):
self.assertEqual(final_direction('W', ['L', 'R', 'R']), 'N')
def test_13(self):
self.assertEqual(final_direction('W', ['R', 'L', 'L']), 'S')
def test_14(self):
self.assertEqual(final_direction('E', ['L', 'R', 'R']), 'S')
def test_15(self):
self.assertEqual(final_direction('E', ['R', 'L', 'L']), 'N')
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment