Case11. Locator is oriented to one side of the world ("N" - north, "W" - west, "S" - south, "E" - east) and can take three digital commands of rotation: 1 - turn left, -1 - turn right , 2 is the rotation to \(180^\circ\). The symbol C is the initial orientation of the locator and the integers \(N_1\) and \(N_2\) are the two commands sent. Display the orientation of the locator after executing these commands.

Solution in Python 3:

import random

d = ['North', 'West', 'South', 'East']
r = {-1 : "turn right", 1 : "turn left", 2 : "rotation to 180 degrees"}

try:
i_bak = i = random.randrange(0,4)
print("i : ", i)
C = d[i]
print("Initial direction (C) : ", C)
N1 = random.choice([-1,1,2])
print("Command 1 (N1) : ", N1)
print("Turn: ", r[N1])
i = (4+i+N1)%4
print("i : ", i)
C = d[i]
print("New direction (C) : ", C)
N2 = random.choice([-1,1,2])
print("Command 2 (N2) : ", N2)
print("Turn: ", r[N2])
i = (4+i+N2)%4
print("i : ", i)
C = d[i]
print("New direction (C) : ", C)

i_bak = (4+i_bak+N1+N2)%4
print("i_bak : ", i_bak)
C = d[i_bak]
print("New direction (C) : ", C)

except KeyError as e:
print('Error')