Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Annotated for Understanding "Follow Instructions" solution in Clear category for Follow Instructions by BootzenKatzen
# This is my solution without hints:
def follow(instructions: str) -> tuple[int, int] | list[int]:
fb = rl = 0 # establishing our right/left and forward/back coordinates as 0
for i in instructions: # iterating through the letters in the string
if i == "f": # if the letter is "f"
fb += 1 # add one to the forward/back variable
if i == "b": # if it is "b"
fb -= 1 # subtract one
if i == "r": # If it is "r"
rl += 1 # add one to the right/left variable
if i == "l": # if it is "l"
rl -= 1 # subtract one
return (rl, fb) # return the right/left forward/back variables as a tuple
# The first solution in the hints:
def follow1(instructions: str) -> tuple[int, int] | list[int]:
c = instructions.count # Storing the function instructions.count(*) as c, where * would be a letter to count
axis_fb = c('f') - c('b') # So we're essentially counting the number of times we go forward/back right/left
axis_rl = c('r') - c('l') # Then subtracting back from forward and left from right to get our f/b l/r positions
return axis_rl, axis_fb # returns the positions as a list
# so example one would be axis_fb = 4 - 0
# axis_rl = 0 - 1
# and it would return -1, 4
# Bonus solution 1:
def follow2(instructions: str) -> tuple[int, int] | list[int]:
move = {"f": 1, "b": -1, "l": -1, "r": 1} # a dictionary with the directions stored with how they change the co-ords
x = y = 0 # establishing their x and y variables for position
for i in instructions: # iterating through the string of instructions
x += move[i] * (i in "lr") # This confused me for a minute, I'll admit but it's basically an if
y += move[i] * (i in "fb") # but it's basically a backwards if statement
# move[i] will pull either +1 or -1 no matter what the letter is
return x, y # but the (i in "lr/fb") will essentially return either True or False
# In python True == 1 and False == 0
# So if the letter is "b" then in the x += statement
# It will be x += -1 * 0 or x =+ 0 and the value won't change
# Then it returns the coordinates at the end
# Bonus solution 2:
follow3 = lambda i: [i.count(x)-i.count(y) for x, y in ('rl', 'fb')]
# Ok, lets break this down from right to left, because that's how I understand it
# at the end ('rl', 'fb') is essentially setting up the tuple we want to get out of it
# I was confused at first thinking x was rl and y was fb, but that doesn't make sense with the math
# essentially it will run the count(x) - count(y) function twice, once where x, y = r, l and once where x, y = f, b
# So if it applied that function inside the parenthesis it would be: [(count(r) - count(l)), (count(f) - count(b))]
# I know that's not how it really works, but it's how my brain could understand it.
print("Example:")
print(list(follow("fflff")))
# These "asserts" are used for self-checking
assert list(follow("fflff")) == [-1, 4]
assert list(follow("ffrff")) == [1, 4]
assert list(follow("fblr")) == [0, 0]
print("The mission is done! Click 'Check Solution' to earn rewards!")
May 4, 2023