Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Raj has solved the puzzle below:
24 -> 1
1264 -> 2
9899 -> 5
349 -> x
Raj determined that the puzzle can be solved by counting the number of holes in the digits of a number. e.g. 1,2,3,5,7 have no holes, 0, 4, 6,
9 have 1 hole each, and 8 has 2 holes. 2 is therefore the value of x to the last line.
Excited by this discovery, Raj now wants to solve this puzzle using a computer program. You have to write a function whose signature is
solvePuzzle(int num) where num is the input integer, and the return value is the number of holes in num.
Constraints:
1 ≤ num ≤ 10
There will be no leading zeroes.
Sample Input #0:
630
Sample Output #0:
2
Explanation #0:
6 and 0 have one hole each
Sample Input #01:
1288
Sample Output #01:
4
Explanation #01:
Both 8's contain 2 holes each.

What I have tried:

echo rand() . "\n";
echo rand() . "\n";

echo rand(5, 15);
?>
Posted
Updated 4-May-16 14:23pm
Comments
Patrice T 4-May-16 17:18pm    
This is your HomeWork. Start working.

Every thing is explain, what else do you need ?
Sergey Alexandrovich Kryukov 4-May-16 20:26pm    
Exactly. From me, it's a big fat vote of one for not starting to work, not explaining what language is used and most ridiculous gibberish in "What I have tried" section, and also the hint in my Solution 1. But I have somewhat bad feeling, not sure the inquirer deserves it.
—SA
Member 12503313 5-May-16 11:54am    
Wasn't looking for the answer just a idea on how to take it on.Thanks for the solution.
Sergey Alexandrovich Kryukov 5-May-16 12:04pm    
My pleasure.
—SA

1 solution

All you need is some logical thinking. If think a bit, you should come to conclusion that there is no predefined information stored in the OS on the number of holes in digits' glyphs. This is the only piece of data you should define in your code.

Everything is reduced to having an array of numbers representing the number of holes. The set of indices of the array, quite obviously, should represent the digits, so it should be an array on the set of indices 0 to 9. From here, you can derive the sum of holes, as well as the "explanation" on each digit encountered. This is really the problem for elementary school. Start working.

—SA
 
Share this answer
 
v2
Comments
Patrice T 4-May-16 21:35pm    
You too kind, absolutely every thing is already in the problem statement.
If the OP can't device something, I recommend to choose another study.
Sergey Alexandrovich Kryukov 4-May-16 21:48pm    
Well, I kinda agree with you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900