Live data from Hacker News

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

jstrieb.github.io

31–40 of 60 posts

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

#31

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

Might not work (I think Python supports bignums, not too sure).

I did this which will work with any length of number[1], and appears to work for all edge cases, including numbers that start with zero and doesn't use loops:

     (defun num-digits (n)
       (if (
[1] Millions of digits, if your computer has the RAM for it.

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

#32
post #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)).

> In Python it's as simple as len(str(x)).

I consider that to be wrong - leading zeros are not part of the number and should be ignored. Using `len(str(x))` results in `2` for the input `"01"`.

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

#33

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.

I've come to the retrospective conclusion decades after educational abuse that professors like this are more interested in showboating their mathematics knowledge than drive students to find good pragmatic solutions.

i.e. I know this super complex answer in which is it happens to be the only purely accurate thing. Can you guess it? vs what I would like to see which is: here are the foundational concepts, bring them all together and arrive at the naturally correct conclusion demonstrating your knowledge and understanding

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

#34
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.

At the bottom of the article they mention that this was discouraged because they hadn't covered strings in the course yet

[deleted]

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

#36
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"

assuming python

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

#37
post #19

Earlier quoted context omitted.

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)

Not since python 2.5 if I recall correctly. Python has had unlimited size integers for a long time now.

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

#38
post #30

Earlier quoted context omitted.

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

> In Python it's as simple as len(str(x)). I consider that to be wrong - leading zeros are not part of the number and should be ignored. Using `len(str(x))` results in `2` for the input `"01"`.

It's assumed the input is an int. If not you can do len(str(int(x))) to make sure it is which strips leading zeroes off in the process.

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

#39
post #30

Earlier quoted context omitted.

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

> In Python it's as simple as len(str(x)). I consider that to be wrong - leading zeros are not part of the number and should be ignored. Using `len(str(x))` results in `2` for the input `"01"`.

The input "01" fails all the other solutions as well. You can't divide or take the log of a string.

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

#40
post #30

Earlier quoted context omitted.

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

> In Python it's as simple as len(str(x)). I consider that to be wrong - leading zeros are not part of the number and should be ignored. Using `len(str(x))` results in `2` for the input `"01"`.

str(x) converts the argument to a string. Since the argument is an integer, there won't be any leading zeroes. More problematic are negative values though.
Post reply on HN