zlacker

[parent] [thread] 16 comments
1. userbi+(OP)[view] [source] 2019-11-11 03:43:26
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.

replies(6): >>svnpen+m >>saagar+95 >>hinkle+W5 >>rmetzl+c9 >>pd0wm+xh >>hoseja+Lk
2. svnpen+m[view] [source] 2019-11-11 03:48:03
>>userbi+(OP)
> 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.

replies(2): >>jstrie+6T >>tzs+B91
3. saagar+95[view] [source] 2019-11-11 05:02:12
>>userbi+(OP)
> is far more inefficient

Is it?

replies(1): >>bjoli+Jb
4. hinkle+W5[view] [source] 2019-11-11 05:12:54
>>userbi+(OP)
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.

replies(1): >>throwa+le
5. rmetzl+c9[view] [source] 2019-11-11 06:00:25
>>userbi+(OP)
There 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.
◧◩
6. bjoli+Jb[view] [source] [discussion] 2019-11-11 06:33:58
>>saagar+95
Probably not, at least not compared to the naive solution using Euclidean division.

for larger numbers it is probably faster.

◧◩
7. throwa+le[view] [source] [discussion] 2019-11-11 07:20:30
>>hinkle+W5
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).

replies(1): >>slavik+np
8. pd0wm+xh[view] [source] 2019-11-11 08:26:12
>>userbi+(OP)
Funny fact: the python round implementation actually uses strings. https://github.com/python/cpython/blob/master/Objects/floato...
9. hoseja+Lk[view] [source] 2019-11-11 09:17:48
>>userbi+(OP)
It's Python. Efficiency has gone out the window long ago.
◧◩◪
10. slavik+np[view] [source] [discussion] 2019-11-11 10:15:20
>>throwa+le
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.)

replies(1): >>gpdere+RP
◧◩◪◨
11. gpdere+RP[view] [source] [discussion] 2019-11-11 14:42:23
>>slavik+np
Once you compute your log2, computing log10 is just a single multiplication though, so, it is in fact as most as easy.
replies(1): >>Mauran+801
◧◩
12. jstrie+6T[view] [source] [discussion] 2019-11-11 15:06:13
>>svnpen+m
I 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.

◧◩◪◨⬒
13. Mauran+801[view] [source] [discussion] 2019-11-11 15:56:05
>>gpdere+RP
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.

replies(1): >>gpdere+y81
◧◩◪◨⬒⬓
14. gpdere+y81[view] [source] [discussion] 2019-11-11 16:50:41
>>Mauran+801
Oh, agree that for arbitrary large or non integer inputs it might not work. But elsethread jepler has shown a solution off Hacker's Delight which which simply multiplies the index of the most significant bit with a magic constant.
replies(2): >>Mauran+jA1 >>slavik+jC1
◧◩
15. tzs+B91[view] [source] [discussion] 2019-11-11 16:58:19
>>svnpen+m
Generally, 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.

◧◩◪◨⬒⬓⬔
16. Mauran+jA1[view] [source] [discussion] 2019-11-11 19:32:33
>>gpdere+y81
> But elsethread jepler has shown a solution off Hacker's Delight which which simply multiplies the index of the most significant bit with a magic constant.

...and then checks for and corrects the potential off-by-one thus incurred.

◧◩◪◨⬒⬓⬔
17. slavik+jC1[view] [source] [discussion] 2019-11-11 19:46:47
>>gpdere+y81
When N is fixed, all algorithms become O(1). That's why they appear the same.

If you do these calculations by hand, the complexity will be more obvious because each operation will be smaller.

[go to top]