Hi @AndreyKartsev
Welcome to the Exercism forums! Thanks for raising this issue.
It does look that way.
Because these are generators, I’ve written the tests to save results in variables, and it appears to be screwing with the detailed error message.
However, when I tested your function in the UI, this was the full error message:
AssertionError: Lists differ: [] != ['12AKL1022000', '38BKL1022000', '69CKL1022000', '102BKL102200']
Second list contains 4 additional elements.
First extra element 0:
'12AKL1022000'
- []
+ ['12AKL1022000', '38BKL1022000', '69CKL1022000', '102BKL102200'] :
Called generate_codes([], KL1022).
The function returned ['12AKL1022000', '38BKL1022000', '69CKL1022000', '102BKL102200'],
but the tests expected ['12AKL1022000', '38BKL1022000', '69CKL1022000', '102BKL102200'] when generating ticket numbers.
The issue here is your while-loop
. It is pop-ing off seats from the passed-in seat-number list
to create the codes. It is then modifying and yielding
the codes out. But when the tests then unpack that generator to compare actual vs expected, there is no longer any seat_numbers list
to run
code = seat_numbers.pop(0) + flight_id`
and
code += "0" * (12 - len(code))`
If you look at the first error message being tossed by pytest, you can see the issue:
AssertionError: Lists differ: [] != ['12AKL1022000', '38BKL1022000', '69CKL1022000', '102BKL102200']
In effect, the test code is being handed the generator generate_codes([], KL1022)
– a list
with no seat numbers. Since at this point there aren’t any seat numbers to modify (they were all popped off), so there aren’t any seat numbers returned.
The way out of the situation is to use a for-loop
and not (destructively) mutate the passed-in data:
for seat in seat_numbers:
code = seat + flight_id
code += "0" * (12 - len(code))
yield code
This leaves seat_numbers
intact, so that when the generator is unpacked, there are seat numbers to operate on, and codes can be generated.
Meanwhile, I will need to think about how to modify the test to give better feedback, and rewrite the tests (if I can) to avoid this scenario.