Live data from Hacker News

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

jstrieb.github.io

1–10 of 60 posts

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

#2
This is a fantastic example of how seemingly simple programming tasks can teach deep concepts. His methodical approach to uncovering edge cases and alternative solutions demonstrates the importance of critical thinking and comprehensive testing in software development, a valuable lesson for all developers.

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

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

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

#5
> a clever, unintuitive solution to a difficult problem

If you approach from the mathematics side of things, building on log₁₀ is the completely obvious approach to use. If it seems unintuitive, that’s just because you don’t understand logarithms.

> the autograding test cases did not include a test using a power of 10.

That’s a pretty glaring oversight. Boundary cases are probably the most important things to cover in such tests. I’d be wanting to test things like 0, 1, 9, 10, 11, 99, 100, 101, and so forth. (Also negative numbers, unless the problem explicitly constrained it to {0, 1, 2, …}.)

We often talk about edge cases in testing, but this reveals that the edges can be not just at the extremes, but also at intermediate value changes. Put another way (still fuzzy!), these are the interesting values. You test interesting values.

This would also be a good place to use property testing; instead of hard-coding a bunch of tests (or perhaps as well as), compare against a large number of generated values, comparing with a known-good implementation, probably just len(str(number)).

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

#6
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

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

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

And more importantly, because it sidesteps the interesting pedagogy around edge cases and testing that the instructor is interested in.

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

#9
post #7

Earlier quoted context omitted.

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

And more importantly, because it sidesteps the interesting pedagogy around edge cases and testing that the instructor is interested in.

The correct solution here is to give credit for the problem to acknowledge genuine clever problem solving, and then offer extra credit for doing it the pedagogical way.

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

#10

> a clever, unintuitive solution to a difficult problem If you approach from the mathematics side of things, building on log₁₀ is the completely obvious approach to use. If it seems unintuitive, that’s just because you don’t understand logarithms. > the autograding test cases did not include a test using a power of 10. That’s a pretty glaring oversight. Boundary cases are probably the most important things to cover i…

> Write a function digitLength(n) that takes a natural number as input (i.e., 0, 1, 2, … – a nonnegative integer)

On another note, you have a very interesting website, I will be in touch at some point when I decide to tackle Rust

Post reply on HN