Live data from Hacker News

Show HN: Astro-Bot – A programming question with every new tab

chrome.google.com

11–20 of 22 posts

Re: Show HN: Astro-Bot – A programming question with every new tab

#15

Nice! It would be cool if there was some kind of points to denote how many questions we have answered correctly.

Scores will be coming in the next few days. I'm also planning on adding an (opt-in) leaderboard so you can see how you compare to other people.

Is a leaderboard something that would appeal to you?

Re: Show HN: Astro-Bot – A programming question with every new tab

#17
The following is incorrect for finding a missing number in a sequence.

  func foo( _ nums: [Int] ) -> Int {
    var sum = 0
    var _ = nums.map {sum += $0}

    return (nums.count * ((nums.count+1) / 2)) - sum
  }

The last line should be

  return ((nums.count+1) * ((nums.count+2) / 2)) - sum
The count of nums will give the incorrect answer since it's already missing a value.

Re: Show HN: Astro-Bot – A programming question with every new tab

#18
post #17

The following is incorrect for finding a missing number in a sequence. func foo( _ nums: [Int] ) -> Int { var sum = 0 var _ = nums.map {sum += $0} return (nums.count * ((nums.count+1) / 2)) - sum } The last line should be return ((nums.count+1) * ((nums.count+2) / 2)) - sum The count of nums will give the incorrect answer since it's already missing a value.

You're absolutely right, thanks for noticing this. There was also a bug with the parentheses I hadn't caught.
Post reply on HN