News for hackers. Stuff that matters.
Re:My Favorite Programming Problem to Teach: Digit Length by rurounijones on Monday November 11, 2019 @12:59AM (#21502517)
I think it is more of a math exercise solved via programming. Which is somehing I am not particularly fond of.

These crop up all the time in those online programming challenge things.

They are usually maths problems (e.g. Find the combined area of two partially intersecting squares based on their coordinates of a,b,c,d and w,x,y,z) where the solution is done via programming but most of the work is, in my opinion, figuring out the maths which is testing for the wrong thing.

Re:My Favorite Programming Problem to Teach: Digit Length by NightMKoder on Monday November 11, 2019 @04:37AM (#21503332)
This particular problem (number of digits in a number) is actually monumentally important for performance reasons for e.g. json generation. See https://m.facebook.com/notes/facebook-engineering/three-opti... for details.

That said, I do agree that as stated the problem is a toy. The problem statement could at least motivate the lack of string operations - i.e. pretend you’re the language designer and you’re tasked with implementing str(int) in C. Just saying “don’t do that” isn’t helpful. Gaining an understanding that nothing is magical is useful though.

Re:My Favorite Programming Problem to Teach: Digit Length by FabHK on Monday November 11, 2019 @05:47AM (#21503701)
That link has a very insightful discussion (and incidentally provides a nice fast correct solution, and use case, for the initial problem!).
Re:My Favorite Programming Problem to Teach: Digit Length by gpderetta on Monday November 11, 2019 @09:23AM (#21505027)
Probably the students do not know about big O notation at this point, but requiring a solution that uses at most O(1) additional space would be enough. Then again, automatic bignums in python make it hard to evaluate the space complexity.
Re:My Favorite Programming Problem to Teach: Digit Length by dTal on Tuesday November 12, 2019 @05:46AM (#21513109)
It's not a math exercise. The problem is stringly - find the length of the string that conventionally represents the given number. The problem is that 0 is a special case, because we write something rather than nothing. Using len(str(n)) is the correct, idiomatic way to solve this. Consider what happens when the problem domain expands to include negative numbers...

Notice that all the straightforward solutions the author rejects, they reject because it fails on 0. Then, with the oh-so-clever log10 solution... surprise! They special-case 0. I'm not sure what the lesson here is supposed to be.

Re:My Favorite Programming Problem to Teach: Digit Length by raegis on Monday November 11, 2019 @03:46AM (#21503105)
Many things people consider "programming" but not mathematics are things we actually study in mathematics classes. They're just unaware.