zlacker

[return to "My Favorite Programming Problem to Teach: Digit Length"]
1. svnpen+Jm[view] [source] 2019-11-11 03:14:08
>>jstrie+(OP)
> As a result, solutions using strings are disallowed on problem sets and quizzes until they are taught. However, the few students who have prior Python programming experience may be tempted to find digit length without loops using a variant of the following (for our purposes) invalid solution.

Wow. this is one of the reasons I hated school. No programmatic reason what given for why a string solution couldnt be used, only an arbitrary reason. Here students may have knowledge from self teaching or whatever, but they are unallowed to use that knowledge because "reasons".

To any teacher that thinks its a good idea to punish students for thinking outside the box: shame on you. All youre going to end up doing is crushing enthusiasm and/or creating drones. Please dont.

◧◩
2. userbi+xo[view] [source] 2019-11-11 03:43:26
>>svnpen+Jm
Strings are disallowed because they are not necessary for this problem and although the solution is shorter, is far more inefficient; it also doesn't demonstrate the algorithmic thinking that the course is obviously trying to teach.

I've taught CS courses before, and have seen plenty of self-proclaimed self-taught know-it-alls who seem to be more stackoverflow-copy-pasters than anything else.

◧◩◪
3. hinkle+tu[view] [source] 2019-11-11 05:12:54
>>userbi+xo
Why do you need to know the decimal length of a number if not for display purposes? If you have one then make that the problem.

I wonder what would happen if you implemented itoa and then counted digits. It’s pretty much the same thing.

◧◩◪◨
4. throwa+SC[view] [source] 2019-11-11 07:20:30
>>hinkle+tu
We're lacking a bit of context here, but this is a good way of understanding the relationship between number of digits, closest power of ten, math.log and decimal representation. Once you understand it, it works the same in any base; in particular in base 2. This would be a necessary step should you want to implement some data structures (eg. quadtrees, octrees, etc. which heavily rely on powers of two). You wouldn't expect the implementation to measure the length of the binary string representation!

Regarding itoa(), the first implementation provided in the article actually match it's implementation, without the unnecessary bits (building the output).

[go to top]