C3W3 data_generator - 'index' v. 'num_lines'

In this piece of code, how could index be greater than num_lines?

I realize it makes no practical difference, but wouldn’t it be enough to say If index == num_lines?

It only matters if there may exist a case where index can go over num_lines that I’m not seeing.

1 Like

(Solution code removed by staff)

Hi, @map9.

Because the increment of index is done at the end of the loop - we reset index to 0 only when in the last step we incremented it over than num_lines value.

Hi @map9,

It is against the community’s honour code to share solution code publicly, kindly refrain from doing so in the future. For times when you have to share the solution code, wait for the mentor to ask you to DM them.

Thanks,
Mubsi

1 Like

Thanks for your reply.
At the end of the loop there is a point when index reaches the num_lines value, and this triggers the reset to zero. So I don’t see how the index could ever go above that value.

I’m not sure I understand you correctly because it is quite the opposite:
An the end of the loop the index is not reset to 0, but it is incremented by 1. It is reset to zero at the start of the loop if index is greater than num_lines (if in previous step when filling up the batch, index was incremented).

You could place whole if statement at the end (after the increment of index by one) and the data_generator would work just the same.

Thanks arvyzukai for hanging in with me on this one, even if it is not a conceptually critical issue.
I understand that the index increment happens at the end of the loop. What I’m saying is that once incremented, going back to the top where the index is checked against num_lines, the condition will always detect the equality first and will reset the index to zero, so it is not necessary to check if it has gone over num_lines, because that is, by design, impossible.

1 Like

Ahh, yes. You are absolutely correct, I totally missed the “going over the loop” part. Now when I read it again, it makes perfect sense what you were talking about :+1:
I guess >= vs. == is “better safe than sorry” atitude :slightly_smiling_face: and since i is not used anywhere else it would sufficed to check for equality.