TASK #1 › Smallest Positive Number Bits
This is a relatively straight forward task. Check the input, stuff the values in a %seen
hash (which is faster than using any
or grep
), and then loop from 1 until you find a missing value.
Minified
Everyone except maintainers loves minified code. This task can also be completed in 24 characters of Perl as per this example:
» perl -E '$r=1;$r++while(grep{$r==$_}@ARGV);say$r' 5 2 -2 0
1
Examples
» ./ch-1.pl 5 2 -2 0
1
» ./ch-1.pl 1 8 -1
2
» ./ch-1.pl 2 0 -1
1
TASK #2 › Count Candies
Some observations:
- The original task had an error, but thankfully Mohammad fixed it in a Tweet
- While the task mentions candidates are ranked, it's probably more accurate to use the analogy of scored. By definition, the highest placed thing is ranked 1 (i.e. 1st). However, from the examples a higher placed candidate has higher value. Therefore I've used the word score throughout this task for that reason.
- Finally, we call candy lollies, so I'll be using that word too :)
In solving this task, I look at the number of consecutive increases in the score on either side. On the left side, if the candidate's score is higher than the previous score, I add one more candy than the previous candidate. For the right hand side, I count the number of decreasing scores. The number of lollies is the higher of those two figures.
Examples
» ./ch-2.pl 1 2 2
Result is 4 (1, 2, 1)
» ./ch-2.pl 1 4 3 2
Result is 7 (1, 3, 2, 1)
Top comments (0)