Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Annotated Goes Right After Solutions solution in Clear category for Goes Right After by BootzenKatzen
# This was my first solution - it changed a lot from beginning to end as I kept reading the hints and source material
# This is pretty much the drawn out version of what they did, because that's just how I think
def goes_after(word: str, first: str, second: str) -> bool:
check = False # establishing check, false by default in case any of the if statements are false
if first in word:
if second in word: # making sure both letters are in the word
if word.index(first) != len(word): # I kept getting errors and this seemed to fix them
# If the first time the first letter comes up is at the end of the word
# Then it can't find the end of the word +1 because it doesn't exist
if word.find(second) > word.find(first): # making sure the first instance of the second letter
# comes up after the first instance of the first letter
if word.find(second) == word.find(first) + 1: # checking if it comes right after
check = True # if all these are true, change check to True
return check
# This is my second solution which I was working on in tandem with the first but trying from a different angle
# It also changed quite a bit
# Initially I tried to use "join" in order to join first and second and then find them
# But it kept throwing errors and I'm not sure why
def goes_after1(word: str, first: str, second: str) -> bool:
check = False
if first and second in word: # I tried this on the first solution and I still got errors for some reason.
if word.find(second) > word.find(first): # same as above
if word.find(first + second) == word.find(first): # checking to see if the 2 together is the same area
# as the first instance of the first letter
if word.find(first + second) != -1: # looking for the 2 letters together
# if they're NOT together, it returns -1
check = True # So as long as they don't return -1 we can change check to True
return check
# Their solution:
def goes_after2(word: str, first: str, second: str) -> bool:
return -1 < word.find(first + second) == word.find(first) < word.find(second)
#This basically does everything from my second solution in one line
# -1 < word.find(first + second) makes sure the 2 are found together and doesn't return -1
# word.find(first + second) == word.find(first) makes sure that the two letters are found togeter
# at the first instance of the first letter
# word.find(first) < word.find(second) makes sure the first instance of the second letter is found after the first
# so it's not picking up a case where the first instance of the second letter comes before the first instance of the first
# so it's not picking up cases of the first and second together where it's not the first instance of the second
#Their second solution:
def goes_after3(word: str, first: str, second: str) -> bool:
return -1 < word.find(first + second) == word.find(first) == word.find(second) - 1
# This essentially does what my first solution did in one line
# The first part is the same as their second solution
# The only difference is that the way they check that it's the first instance of the second letter
# is by asserting that word.find(first) == word.find(second) - 1
# That way if it's NOT the first instance of the second letter, it won't match up
#Bonus solution 1:
def goes_after4(word: str, first: str, second: str) -> bool:
try: # basically instead of having all those conditionals to catch errors, they use try/except
return word.index(second) - word.index(first) == 1 # if the index of the second is greater than the index of the
# first by exactly one (they're next to each other)
# then it will return True
except ValueError: #If they're not together or there's an error it will return false
return False
# Bonus Solution 2:
def goes_after5(word: str, first: str, second: str) -> bool:
return -1 < word.find(first) == word.find(second) - 1
# I honestly don't know why this works when we needed try and except for the one abovea
print("Example:")
print(goes_after("world", "w", "o"))
# These "asserts" are used for self-checking
assert goes_after("world", "w", "o") == True
assert goes_after("world", "w", "r") == False
assert goes_after("world", "l", "o") == False
assert goes_after("panorama", "a", "n") == True
assert goes_after("list", "l", "o") == False
assert goes_after("", "l", "o") == False
assert goes_after("list", "l", "l") == False
assert goes_after("world", "d", "w") == False
assert goes_after("Almaz", "a", "l") == False
print("The mission is done! Click 'Check Solution' to earn rewards!")
April 10, 2023
Comments: