Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Straightforward FSM solution in Clear category for Rolling 🎲! by amandel
def rolling_dice(moves: str) -> int:
t,f,r = 1,2,3 # top, front, right, kept mod 7
for x in moves:
t,f,r = {'N':(f,-t,r), 'S':(-f,t,r), 'E':(-r,f,t), 'W':(r,f,-t)}[x]
return t if t > 0 else 7+t
print("Example:")
print(rolling_dice("SE"))
# These "asserts" are used for self-checking
assert rolling_dice("SN") == 1
assert rolling_dice("") == 1
assert rolling_dice("EESWN") == 6
assert rolling_dice("NWSNWEESNW") == 3
assert rolling_dice("NNNSESNESWNENSWNNWSWNSSNWWSWNW") == 5
print("The mission is done! Click 'Check Solution' to earn rewards!")
May 7, 2023
Comments: