5 ms·
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 demonstrat
by userbinator 7y ago
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.
- svnpenn 7y ago> Strings are disallowed because they are not necessary for this problem the division examples are not necessary either, thats the point. you can solve it different ways, that doesnt mean one way is not necessary, it just means its different. one may be faster, one may be more readable. If you dont allow different solutions you cant explore the tradeoffs between them.
- jstrieb 7y agoI note that the solution is invalid at the bottom of my post, but it is worth mentioning that I know all of the students in the group session when I teach this problem, and I am familiar with their background. If I know that a student has prior Python experience, I will discuss the string solution with them, but let them know not to use it in their homework until strings have been covered in class. In this case, even though the solution is disallowed for grading purposes, there is an opportunity to discuss it during the lesson.
- tzs 7y agoGenerally, the purposes of the exercises in a class is to reinforce the material that has been taught up to that point and to demonstrate that the student can use it. For example, if early in an elementary number theory class the student is asked to prove that there are in infinite number of primes of the form 4n+3, a solution that just invokes Dirichlet's theorem on primes in arithmetic progressions would probably not be acceptable. That approach does work to show that there are an infinite number of 4n+3 primes, but completely fails to show that that the student understood the material actually taught in class. It's the exact same thing with the digit counting problem. Solving it by just invoking the built in string length function does little to demonstrate that the student understands the material taught so far.
- saagarjha 7y ago> is far more inefficient Is it?
- bjoli 7y agoProbably not, at least not compared to the naive solution using Euclidean division. for larger numbers it is probably faster.
- hinkley 7y agoWhy 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.
- throwaway744678 7y agoWe'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).
- slavik81 7y agoI 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.)
- gpderetta 7y agoOnce you compute your log2, computing log10 is just a single multiplication though, so, it is in fact as most as easy.
- MauranKilom 7y ago
- rmetzler 7y agoThere are other HN comments pointing out that the proposed solution still doesn't work correctly because of floating point operations. If the goal is to learn something about programming, yes, I agree with you. But if the goal is to write code with as few bugs as possible then I would take the "create a string and count chars" solution over every solution involving floating point math.
- pd0wm 7y agoFunny fact: the python round implementation actually uses strings. https://github.com/python/cpython/blob/master/Objects/floatobject.c#L965-L972 https://github.com/python/cpython/blob/master/Objects/floato...
- hoseja 7y agoIt's Python. Efficiency has gone out the window long ago.