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. ruroun+Bx[view] [source] 2019-11-11 05:59:23
>>lilyba+tv
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.

◧◩◪◨
4. dTal+ri3[view] [source] 2019-11-12 10:46:42
>>ruroun+Bx
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.

[go to top]