Hacker News new | ask | show | jobs
by Jtsummers 1335 days ago
Why did you choose to decrement n twice on each iteration? What happens when n is odd vs even now?
2 comments

Oh, sorry, I didn't paste the right code.

On my machine I used this code:

  package main
  import "fmt"
  func below(n uint64 ) {
      for n>0 {
      fmt.Println(n-1)
          n--
      }
  }


  func main() {
      below(10);
      below(0);
  }
The actual code I put into the bugfix site was:

  func below(n uint64, to chan uint64) {
          for n>0 {
              to <- n-1
              n--
          }
          close(to)
      }
but when writing this comment I went back and didn't modify the for loop to be a while.
The above code is correct, and of course it is accepted.
Why does it say "What happens at 0" when you omit the decrement?
When your code is wrong, the server gives you a clue hinting at what's wrong in the original code.

It doesn't know what's wrong in the code you submitted... it is not understanding deeply what's wrong with your code. It's not some huge multi-terabyte language model analyzing arbitrary code, or whatever.

It just knows your code is wrong and gives you a clue so you can try again.

If you omit the decrement, it's an infinite loop for n>1, presumably, you are detecting that?
The server is checking the output of the function.

Here is an example of what's running behind the scenes, to help you understand:

https://bugfix-66.com/contribute

The above code is what's being used for Bug #1:

https://bugfix-66.com/a6cb1e062ae0fdc47b43ec489aa40a958db728...

Is that pretty clear?

I also came to a solution very similar to the sibling comment here. I'd love to see why this doesn't work server-side but does work on my machine. What other tests are you running aside from checking each decrement is correct?
Show me your code, that you think is correct, and the server rejects.

I'll tell you what's wrong with your code.

Might I recommend appropriate debugging output? It would save the mystery and back and forth. Not everyone who uses your site has access to you on HN. :)

    func below(n uint64, to chan uint64) {
        for ; n >= 0; n-- {
            var t = n - 1
            to <- n
        }
        close(to)
    }
I've run this locally with to <- n replaced with a print statement and it works with unsigned integers.
Did you make other changes besides `to <- n` becoming print? Because as it stands, that will still produce an infinite loop.
Oh, yes. The for loop init should be "; n > 0; n--"