Live data from Hacker News

My favorite programming problem to teach: Digit length (2019)

jstrieb.github.io

21–30 of 60 posts

Re: My favorite programming problem to teach: Digit length (2019)

#22
post #15

I 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…

Nice insight. Similarly, dynamic programming solutions that "build up" from the bottom can often be clearer than those "go down" recursively from the top, even though they're equivalent.

Re: My favorite programming problem to teach: Digit length (2019)

#23
It's very strange to me that the teacher would push the students from the correct solution using a loop, towards an incorrect solution using a logarithm. A logarithm could work in a language like C where ints can't get too large, but Python has arbitrary precision integers so any solution using floating point numbers is doomed. For example, the code given in the post returns 16 instead of 15 for 999_999_999_999_999.

Re: My favorite programming problem to teach: Digit length (2019)

#25
post #14
post #4

Wouldn'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.

len() is an O(1) lookup of the stored length on the unicode/bytes object in Python.

Re: My favorite programming problem to teach: Digit length (2019)

#26
post #4

Wouldn'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.

Be careful, doing str(num) requires python to convert the binary representation it has num stored as to a decimal. (C)Python implements a quadratic time change of basis algorithm. This is slow enough that now for very large inputs python will raise "ValueError: Exceeds the limit (4300 digits) for integer string conversion"

Re: My favorite programming problem to teach: Digit length (2019)

#27
post #19

Am 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.

Good point, I was thinking in Python 2 (where there are integer limits)

Re: My favorite programming problem to teach: Digit length (2019)

#28

Am I the only one who, in response to: "dont use loops" thought of the following: int numDigits(int num){ if (num

The statements will duck type to numbers so you could just be really silly and do something like:

   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
Hmm, 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.

Re: My favorite programming problem to teach: Digit length (2019)

#30
post #29

Hmm, 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.

It is mentioned at the end of the article. In Python it's as simple as len(str(x)).
Post reply on HN