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

◧◩◪◨⬒
5. slavik+UN[view] [source] 2019-11-11 10:15:20
>>throwa+SC
I wish they didn't phrase it as being about removing a loop. If math.log10 supported arbitrary integer inputs, there would be a loop inside.

They should consider the upper bound of their algorithm. Because it uses floating point, it doesn't work for all natural numbers, as required by their specification.

To address your point, counting digits in base 2 is much simpler than in base 10, because the internal representation of the number on the computer is already base 2. You can use numeric calculations, but you can also just look at your digits.

(Edited for fewer tangents and to be more positive.)

◧◩◪◨⬒⬓
6. gpdere+oe1[view] [source] 2019-11-11 14:42:23
>>slavik+UN
Once you compute your log2, computing log10 is just a single multiplication though, so, it is in fact as most as easy.
◧◩◪◨⬒⬓⬔
7. Mauran+Fo1[view] [source] 2019-11-11 15:56:05
>>gpdere+oe1
It's a single multiplication - with an irrational (transcendental, actually) number. That is most certainly not "easy" for a computer, and elsewhere in the comments you can see how much of a problem that actually poses.

Also, counting digits in base 2 is not the log2. The former gives you the latter but not the reverse. Finding the number of decimal digits in a number given in base 2 is not a simplification.

[go to top]