zlacker

[parent] [thread] 6 comments
1. gnuvin+(OP)[view] [source] 2019-11-11 00:34:34
By using a do-while loop, you don't have to handle the special case of zero (although one could argue that the do-while loop is the special handling for zero). Python does not have such a loop, so you need to do an infinite loop with an explicit break and I know some people recoil in horror at the sight of such code.

    def digitlength(n):
        digits = 0
        while True:
            digits += 1
            n /= 10
            if n == 0:
                return digits
replies(1): >>a-niko+O1
2. a-niko+O1[view] [source] 2019-11-11 01:04:47
>>gnuvin+(OP)

    def digitlength(n):
        digits = 1
        while (n > 9):
            digits += 1
            n /= 10
        return digits
Also may want to set n = abs(n) in the beginning, in case n is negative.
replies(1): >>gnuvin+T1
◧◩
3. gnuvin+T1[view] [source] [discussion] 2019-11-11 01:06:25
>>a-niko+O1
The problem statement said that the input would be natural numbers.
replies(1): >>lifthr+A8
◧◩◪
4. lifthr+A8[view] [source] [discussion] 2019-11-11 02:54:32
>>gnuvin+T1
Wait, do natural numbers include zero? evil grin
replies(2): >>talake+Ag >>chopin+xJ
◧◩◪◨
5. talake+Ag[view] [source] [discussion] 2019-11-11 04:59:09
>>lifthr+A8
No support for that in Sloane's. https://oeis.org/A000027
◧◩◪◨
6. chopin+xJ[view] [source] [discussion] 2019-11-11 11:59:07
>>lifthr+A8
I looked up wikipedia which says there's no conclusive definition.
replies(1): >>lifthr+dL
◧◩◪◨⬒
7. lifthr+dL[view] [source] [discussion] 2019-11-11 12:14:16
>>chopin+xJ
The joke is that, if you don't need to check if the input is negative because it is not a natural number, you can also choose a definition of the natural number without zero to avoid special casing it at all.
[go to top]