Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
string index + use try-except to capture ValueError solution in Clear category for Goes Right After by hbczmxy
def goes_after(word: str, first: str, second: str) -> bool:
# your code here
try:
# Calculate the indexes of the first and second symbols respectively
first_index = word.index(first)
second_index = word.index(second)
except ValueError:
# If either of the two symbols is not in the word
return False
else:
# Check if the second match immediately follows the first symbol
if first_index + 1 == second_index:
return True
else:
return False
if __name__ == '__main__':
print("Example:")
print(goes_after('world', 'w', 'o'))
# These "asserts" are used for self-checking and not for an auto-testing
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
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 10, 2021
Comments: