Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
complex solution to a simple task (introducing Python's imaginary unit) solution in Creative category for Follow Instructions by mu_py
from typing import Tuple
# directions encoded as complex numbers (in Python, "j" denotes the imaginary unit: j**2==-1)
directions = {
'f': 1j,
'b': -1j,
'r': 1,
'l': -1,
}
#simply adding up real and imaginary part (using j avoids fiddling with [0] and [1])
def follow(instructions: str) -> Tuple[int, int]:
pos = sum(directions[i] for i in instructions)
return (pos.real, pos.imag)
if __name__ == '__main__':
print("Example:")
print(follow("fflff"))
# These "asserts" are used for self-checking and not for an auto-testing
assert follow("fflff") == (-1, 4)
assert follow("ffrff") == (1, 4)
assert follow("fblr") == (0, 0)
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 26, 2022
Comments: