[Community Puzzle] Number of Digits - Puzzle discussion

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Created by @NicknamedTwice,validated by @LeMasSon,@DrBrask and @David_Augusto_Villa.
If you have any issues, feel free to ping them.

Python:

Tried iterating through strings, concatenating into one long string, tried some divmodding to separate the tens thousands etc into integers then comparing, still the final validation was too memory intensive or took way too long. Whichever way I tried to cut it I couldn’t seem to get away from iterating for loops millions of times. Any ideas? My best solutions work but take too long to complete.

Try to see what’s happening when you print each result from 1 to 1020, for example.

For a continuous sequence of numbers, there is a fixed pattern for digits to appear.

e.g. 0000…4218

print out the numbers line-by-line to study the digits. Use space or zero to force them to align to the right.

The last digit changes from 0 to 9 and then repeats.
If the target is to find a 4 in this digit, it means
for every 10 numbers there is one wanted digit to appear.
=> there are about 421.8 chance to meet a 4 in this digit.

For the tenth-place digit, there is a sequence of 0 and then a sequence of 1,… finally to see a sequence of 9. And then repeats. For every 100 numbers there are 10 occurrence of the wanted digit.
=> there are about 42.18 x 10 chance to meet a 4 in this digit.

The above is a guesstimation shedding light on a possible approach.
You can refine the algorithm (how to handle cut-offs from some blocks of wanted digits at the end) to make it an accurate calculation.

Thanks, I am not certain that I have found the correct solution but, I’m certain the validations are wrong - it is for sure double counting:

Example for n = 219, for digit 5. expected answer is 42, but the actual answer is 40

This was one of my slower solutions, it validated all but the final large number, but it is incorrect
[Mod edit: Please avoid posting codes on the forum.]
Console:


3
5
15
25
35
45
50
51
52
53
54
55
55
56
57
58
59
65
75
85
95
105
115
125
135
145
150
151
152
153
154
155
155
156
157
158
159
165
175
185
195
205
215

42

[Mod edit: Please avoid posting codes on the forum.]

returns 40 as the answer

which would be correct as in the first example: 55 and 155 is counted twice

5, 15, 25, 35, 45, 65, 75, 85, 95 (9 5’s)
50 to 59 (11 5’s)
105, 115, 125, 135, 145, 165, 175, 185, 195 (9 5’s)
150 to 159 (11 5’s)
205, 215 (2 5’s)

Total number of 5’s = 9 + 11 + 9 + 11 + 2 = 42

Ah, I see- it would be correct to count 55 as 2 as it is double digits

1 Like

I pass everything for submission then it says the last two validators fail. Not sure why.

In those cases, the given number n contains the given digit k (not in the initial position nor the final position). You may first check whether your code handles such a scenario correctly.

I now have a solution that works for all test cases, every case I’ve thrown at it works, but the first validator isn’t passing when I submit, I guess because I’ve hardcoded something in? All the other validators pass. Hard to tell where I’ve gone wrong without knowing n and k I’ve gone wrong with.

Thanks for the support, my criticism of this puzzle is that it seems more about a problem to find a series formula from a mathematics perspective than an actual coding challenge.

“actual coding challenges” are straight forward coding exercises or tests - for testing how much one knows about a language. I do not feel they are true puzzles.

True puzzles challenge problem solving skills, not coding knowlege.

1 Like