Setup
A boy is walking home from school. To make the walk more enjoyable, he decides to add up the numbers of the houses he passes during his walk. Unfortunately, the numbers won't appear to him in any specific order, since he's regularly taking turns.
At some point during the walk, the boy encounters a house marked number 0. This surprises him so much that he stops adding the numbers after that house to the total.
For the given array of house numbers, determine the sum that the boy will get. There will always be at least one number 0 house on the path.
Examples
inputArray = [5, 1, 2, 3, 0, 1, 5, 0, 2]
=> 11
inputArray = [1, 4, 34, 124, 2, 0, 14, 51]
=> 165
Tests
inputArray = [5, 1, 2, 3, 0, 1, 5, 0, 2]
inputArray = [4, 2, 1, 6, 0]
inputArray = [4, 1, 2, 3, 0, 10, 2]
Good luck!
This challenge comes from myjinxin2015 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (8)
My original solution in Ruby
and after being inspired by Craig's solution:
Simple Haskell solution
Elixir
C++ solution
Javascript
JavaScript
If a test case doesn't have an expected output, is it really a test case?
Also, what did the boy do for houses with numbers like "14A" or houses that have names, and no number?