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. lilyba+tv[view] [source] 2019-11-11 05:25:59
>>svnpen+Jm
> No programmatic reason what given for why a string solution couldnt be used, only an arbitrary reason

The entire problem is arbitrary. It's not a real-world problem looking for a solution. It's a programming exercise.

◧◩◪
3. cjfd+X81[view] [source] 2019-11-11 13:58:43
>>lilyba+tv
Actually, this problem does occur in practice. When one needs to output some numbers in a column it can be important how wide the widest one is. Ironically, in that case the numbers are going to be converted to text anyway so you might as well use the string size....
◧◩◪◨
4. WorldM+z22[view] [source] 2019-11-11 19:58:20
>>cjfd+X81
Except string size isn't co-determinate with output size either. Number of code points is sometimes an approximation of number of glyphs after text rendering, more usefully approximate with ASCII, and a lot less usefully approximate with a large amount of Unicode.
◧◩◪◨⬒
5. ghusba+ft2[view] [source] 2019-11-11 23:12:51
>>WorldM+z22
We're talking about converting numbers to strings, for which none of that matters.
◧◩◪◨⬒⬓
6. WorldM+DK3[view] [source] 2019-11-12 15:34:17
>>ghusba+ft2
Because there are no numbers other than Arabic numerals? Numbers are never localized?

Even within just Arabic numbers, it possible for localization systems to add digit separators such as commas, underscores, or periods depending on region.

Sure, str() of an out-of-the-box number in Python won't do localization by default, but Python is a highly dynamic language and I've seen __str__() handlers that do localization, even on things that otherwise act like numbers.

"Simple" string conversion-based "math" rarely is simple in the real world, and ignoring localization and the complexities of Unicode in situations where you are assuming they can't possibly happen is a jungle of mistakes that programmers continue to make every day. The world doesn't run on EBCDIC or ASCII alone anymore, and localization is a thing that matters to a lot of people. The correspondence between code points reported by len(str()) and either input to str() and output to a display device is tenuous at best in anything but the best ASCII circumstances, and handwaving it away is an exercise in your particular bias.

[ETA: And the parent discussion was explicitly about table formatting the output, and that itself specifically is a scenario where you should want to localize the output first and not just rely on str(yourNumber).]

[go to top]