4 ms·
Your second example subtracts and adds 1 nearly arbitrarily, which wouldn't be needed if the convention of the 0-index wasn't so widespread.
by Jerrrry 2y ago
Your second example subtracts and adds 1 nearly arbitrarily, which wouldn't be needed if the convention of the 0-index wasn't so widespread.
- School-Cotton 2y agoYou need the first three elements to go into the first group, the next three to go into the second group, and so on. How would you write it?
- binary132 2y agoThat’s not what that loop does, it puts one item into each next group and loops back over the groups after every three items. Really it ought to be a by-three stepped loop over items, inserting each into each group inline: groups[1], groups[2], groups[3] = items[i], items[i+1], items[i+2] If the group count is dynamic, you can just loop over groups instead, and then step through items by #groups, inserting.
- School-Cotton 2y agoFair enough, for some reason I thought it was i/3 and not i%3. Still, I think the point stands.
- Benjamin_Dobell 2y agoIf it is dynamic one of the loops will also suffer from an off-by-one issue. You can't add 1-based indices together like you can zero-based indices. It's also worth noting your solution exhibits similar off-by-one behaviour. The left hand side constants (integer values) do not match the right. It's error prone.
- binary132 2y agosorry, where is the off by one? the code offered is of course only a solution for the fixed-size groups
- Jerrrry 2y ago>You can't add 1-based indices together like you can zero-based indices. I think you are right but I am unable to articulate why. But I think 0 based indexes are able to fruitfully capture "going nowhere" iteratively than 1 based indexes, which do require a decrement in that circumstance.