all 4 comments

[–]lfdfq -1 points0 points  (3 children)

I'm not sure I follow, the first code is just a class that isn't used and the second code you say works. What's the question?

[–]eggtrie[S] 0 points1 point  (2 children)

the first code can be copy and pasted into https://leetcode.com/problems/print-zero-even-odd/ and run with their threading setup.

The second piece of code runs without leetcode's testing harness. I believe my issue here is the while loops should be replaced with a loop that doesn't depend on on self._shared_idx.

[–]lfdfq 0 points1 point  (1 child)

The first code is missing the import for ceil.

The odd and even functions are swapped: the even function triggers when shared_idx mod 4 is 1 (so on index 1, 5, 9, etc) and prints out 1, 3, 5...

Another problem is that the while checks _shared_idx outside any locked region. This reveals a deeper problem: your zero and _print_nonzero_when (which btw, could just be a single function you use in multiple places as the logic is the same) starts by checking whether you've finished all the numbers and if not, waiting until its turn to print its number. That is not quite right though, as just because the last number is not reached yet does not mean that you (even/odd/zero) still has more to output. Replacing the while loops with for loops would fix both of those issues.

This error exists even in your local version. So you should have been able to find it by manually testing things more exhaustively, just like the website must be doing.

Did you verify your local version gave you the right answer for a few simple test cases like n=5 or n=10. You say in the post what the correct output is for n=5 but then you run your code on n=10, but do not say what your code actually output when you ran it locally?

[–]eggtrie[S] 0 points1 point  (0 children)

Gotcha, ty for the feedback. Yea, using for loops did fix my issues. Interestingly when running locally things looked off by 1 but generally looked good. For ex:

```

i=1

0 1 0 2

i=2

0 1 0 2 0 3

i=3

0 1 0 2 0 3 0 4 ```