Though powers of 10 is a boundary condition so would be one of the first things I test against
My favorite programming problem to teach: Digit length (2019)
21–30 of 60 posts
Re: My favorite programming problem to teach: Digit length (2019)
#22I would prefer a solution based on multiplication than division. If the number is less than 10, return one, otherwise is it less than 100, ... You should prefer the simplest tool that can do the job. Multiplication is simpler than division. You could digress about floating point errors (Python has infinite-precision integers, but not infinite- precision floats) or performance (division may be in some contexts meaning…
Re: My favorite programming problem to teach: Digit length (2019)
#23Re: My favorite programming problem to teach: Digit length (2019)
#24Re: My favorite programming problem to teach: Digit length (2019)
#25Wouldn't len(str(num)) be adequate here? This is a quite literal translation of what the code should be doing: measuring the length of the text representation of a number. The mathematical approach seems a little convoluted, although it serves the purpose of teaching a lesson.
Len() and str() are a loop under the hood.
Re: My favorite programming problem to teach: Digit length (2019)
#26Wouldn't len(str(num)) be adequate here? This is a quite literal translation of what the code should be doing: measuring the length of the text representation of a number. The mathematical approach seems a little convoluted, although it serves the purpose of teaching a lesson.
Re: My favorite programming problem to teach: Digit length (2019)
#27Am I the only one who, in response to: "dont use loops" thought of the following: int numDigits(int num){ if (num
Unfortunately in Python, integers have no fixed maximum size and can grow indefinitely, so your algorithm would need to be long enough to cover all the numbers that can conceivably fit in a computer's memory. I guess there's still a finite size there, but I'm not sure how the memory usage scales with digit length, so I don't know what the largest number would be.
Re: My favorite programming problem to teach: Digit length (2019)
#28Am I the only one who, in response to: "dont use loops" thought of the following: int numDigits(int num){ if (num
return 1 + (n >= 10) + (n >= 100) + (n >= 1e3) + (n >= 1e4) + (n >= 1e5) + (n >= 1e6) + (n >= 1e7) + (n >= 1e8) + (n >= 1e9) ...Re: My favorite programming problem to teach: Digit length (2019)
#29 perl -e 'print length 987654'
6
Similarly easy in Lisp with either of write-to-string, prin1-to-string, and princ-to-string. I expect python to have some such built-in function too.Re: My favorite programming problem to teach: Digit length (2019)
#30Hmm, my solution would be to convert the number to a string and return its length. Easily done in Perl: perl -e 'print length 987654' 6 Similarly easy in Lisp with either of write-to-string, prin1-to-string, and princ-to-string. I expect python to have some such built-in function too.