Trying to understand a bit more of the generator/send function in python. I read some in the link: generator send function purpose which helped a lot.
But I am trying to understand the code below. Why is the next(checker)
necessary? Doesnt the send function automatically ask for the next item in the generator? I tried just having the next(checker)
before the for loop but that doesnt function the same way. I thought the send function sends 'attempt' as x and yields whether x == password
. I just dont understand why its necessary to have the next(checker) in the loop.
def checkPassword(attempts, password):
def check():
while True:
x = yield
yield x == password
checker = check()
for i, attempt in enumerate(attempts):
next(checker)
if checker.send(attempt):
return i + 1
return -1
The function above is base on the problem:
"In order to validate your function, you want to test it locally. Given a list of attempts and the correct password, return the 1-based index of the first correct attempt, or -1 if there were none.
Example
For attempts = ["hello", "world", "I", "like", "coding"]
and
password = "like"
, the output should be
checkPassword(attempts, password) = 4."