zlacker

[parent] [thread] 49 comments
1. svnpen+(OP)[view] [source] 2019-11-11 03:14:08
> 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.

replies(14): >>ianai+l1 >>userbi+O1 >>wflemi+22 >>em-bee+43 >>hechan+z3 >>_bxg1+n6 >>lilyba+K8 >>atoav+Nl >>hoseja+pm >>AlecSc+Do >>tomxor+Sx >>surfsv+tO >>jstrie+uU >>MrSton+OX
2. ianai+l1[view] [source] 2019-11-11 03:36:15
>>svnpen+(OP)
Agreed. I think you could even argue that string manipulation is even more common than uses of math.ceil, floor, the floor division operator (//), and so on. I love math, but this is a programming class.

This also fails my personal rule to always need to have a reason to do something mean - like failing someone on a problem, and no reason to do something nice - like saying it’s fine since it works.

replies(3): >>kevin_+o7 >>moron4+kL >>jstrie+qV
3. userbi+O1[view] [source] 2019-11-11 03:43:26
>>svnpen+(OP)
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+a2 >>saagar+X6 >>hinkle+K7 >>rmetzl+0b >>pd0wm+lj >>hoseja+zm
4. wflemi+22[view] [source] 2019-11-11 03:46:41
>>svnpen+(OP)
Because I am a smart-ass, I did something like this in college. There was an intro course in MATLAB my freshman year, but I'd been programming for several years at that point so I found it too basic to hold my interest. In one of our early labs, we were told to implement a function that calculated a Fibonacci number. I figured that sounded like something MATLAB probably had in its stdlib, and it did, so I wrote a 1 liner and called over a TA to approve my work.

Thankfully this TA agreed with you (as do I). He said it looked good, and I think that's the shortest lab I ever had.

replies(1): >>em-bee+b3
◧◩
5. svnpen+a2[view] [source] [discussion] 2019-11-11 03:48:03
>>userbi+O1
> 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+UU >>tzs+pb1
6. em-bee+43[view] [source] 2019-11-11 04:01:35
>>svnpen+(OP)
working with contraints (don't use loops, or don't use the string function) is a learning tool. don't dismiss it just because it prevents your clever trick from working.
replies(1): >>MrSton+oZ
◧◩
7. em-bee+b3[view] [source] [discussion] 2019-11-11 04:02:59
>>wflemi+22
if i was the TA i would have said: "congratulations, for you have not learned anything new"

personally, when something is to basic to hold my interest, i try to find ways to make it more challenging.

replies(2): >>Retric+w5 >>wflemi+TH
8. hechan+z3[view] [source] 2019-11-11 04:07:16
>>svnpen+(OP)
I don't know about this. This feels like cheating rather than thinking outside of the box. Like when you ask a student to implement a vector in C++, instead of writing the code the student makes a wrapper around std::vector. Internally, `str` probably also use some equivalent of the naive solution implemented somewhere to convert a integer to its decimal string.
replies(1): >>kadoba+Ii
◧◩◪
9. Retric+w5[view] [source] [discussion] 2019-11-11 04:37:20
>>em-bee+b3
I personally learned quite a bit in a similar situation.

In an operating systems class we had a little project to create a command line calculator in C, with the added hoop of using x86 ASM for all control flow and calculations. As this was not a programming class we had a very brief overview of the very basics needed to get this done. I assumed using floating point arithmetic would make this easier, but knew that was not part of the early spec, so I asked the professor in class what version of x86 and he said pentium 1.

I then found and read intel’s documentation for the first generation Pentium. Which completely changed my mental model of CPU’s. Honestly, it was probably the closest collage assignment to how real world coding works and much more useful long term than just implementing some simple algorithms by hand.

10. _bxg1+n6[view] [source] 2019-11-11 04:52:56
>>svnpen+(OP)
This isn't thinking outside the box, it's an incredibly obvious way to avoid using your brain. There's nothing wrong with creating constraints on a problem, as long as those are clearly defined. Presumably the OP didn't neglect to mention that strings were disallowed, and then just fail anyone who happened to use them.
◧◩
11. saagar+X6[view] [source] [discussion] 2019-11-11 05:02:12
>>userbi+O1
> is far more inefficient

Is it?

replies(1): >>bjoli+xd
◧◩
12. kevin_+o7[view] [source] [discussion] 2019-11-11 05:07:47
>>ianai+l1
> I love math, but this is a programming class.

Sometimes you actually have to do arithmetic when programming. It's useful to teach people these sort of things so they don't cook up half baked solutions in the future.

◧◩
13. hinkle+K7[view] [source] [discussion] 2019-11-11 05:12:54
>>userbi+O1
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+9g
14. lilyba+K8[view] [source] 2019-11-11 05:25:59
>>svnpen+(OP)
> 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.

replies(2): >>ruroun+Sa >>cjfd+eM
◧◩
15. ruroun+Sa[view] [source] [discussion] 2019-11-11 05:59:23
>>lilyba+K8
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.

replies(3): >>raegis+mk >>NightM+1o >>dTal+IV2
◧◩
16. rmetzl+0b[view] [source] [discussion] 2019-11-11 06:00:25
>>userbi+O1
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.
◧◩◪
17. bjoli+xd[view] [source] [discussion] 2019-11-11 06:33:58
>>saagar+X6
Probably not, at least not compared to the naive solution using Euclidean division.

for larger numbers it is probably faster.

◧◩◪
18. throwa+9g[view] [source] [discussion] 2019-11-11 07:20:30
>>hinkle+K7
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+br
◧◩
19. kadoba+Ii[view] [source] [discussion] 2019-11-11 08:14:19
>>hechan+z3
Using math.log10 seems at least as cheating to me. (though to be clear, I'd accept both if it were me)
◧◩
20. pd0wm+lj[view] [source] [discussion] 2019-11-11 08:26:12
>>userbi+O1
Funny fact: the python round implementation actually uses strings. https://github.com/python/cpython/blob/master/Objects/floato...
◧◩◪
21. raegis+mk[view] [source] [discussion] 2019-11-11 08:46:37
>>ruroun+Sa
Many things people consider "programming" but not mathematics are things we actually study in mathematics classes. They're just unaware.
22. atoav+Nl[view] [source] 2019-11-11 09:08:16
>>svnpen+(OP)
I think this is a communications issue. If you ask them to solve it and then they clearly did, but you then say: “bUt yOu dIDn’T SoLvE iT tHe RiGht wAy”, then it is clearly your fault.

I think if you tell stuff like this upfront it is totally acceptable: “Reduce the number of digits in a given float without converting it to a String”.

Even students with previous knowledge could take this as a challenge where they learn something.

23. hoseja+pm[view] [source] 2019-11-11 09:15:39
>>svnpen+(OP)
Precisely. It was the first solution I came up with after reading the title. It's perfect in that all the subtleties are already handled by the language. Digit length of base10 numbers isn't really a logic/math/computer problem, it's a string and writing convention problem.
◧◩
24. hoseja+zm[view] [source] [discussion] 2019-11-11 09:17:48
>>userbi+O1
It's Python. Efficiency has gone out the window long ago.
◧◩◪
25. NightM+1o[view] [source] [discussion] 2019-11-11 09:37:16
>>ruroun+Sa
This particular problem (number of digits in a number) is actually monumentally important for performance reasons for e.g. json generation. See https://m.facebook.com/notes/facebook-engineering/three-opti... for details.

That said, I do agree that as stated the problem is a toy. The problem statement could at least motivate the lack of string operations - i.e. pretend you’re the language designer and you’re tasked with implementing str(int) in C. Just saying “don’t do that” isn’t helpful. Gaining an understanding that nothing is magical is useful though.

replies(2): >>FabHK+Yt >>gpdere+mP
26. AlecSc+Do[view] [source] 2019-11-11 09:46:03
>>svnpen+(OP)
This problem is a funny one for me and gives me real nostalgia, back to about ten years ago when I was learning to program.

I'd just picked up python and was having so much fun going through the project Euler problems.

I can't remember which question it was for but we had to know the length of an integer. At that point I hadn't learnt about type conversion so had to implement it the way the author wants his students to do it.

I remember fondly as well that I hadn't learnt about the modulo operator and had to implement that by myself as well.

I couldn't believe it when I got the answer and progressed to the forum where others shared their answers and they were doing it on one line with %!

Good times!

◧◩◪◨
27. slavik+br[view] [source] [discussion] 2019-11-11 10:15:20
>>throwa+9g
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+FR
◧◩◪◨
28. FabHK+Yt[view] [source] [discussion] 2019-11-11 10:47:53
>>NightM+1o
That link has a very insightful discussion (and incidentally provides a nice fast correct solution, and use case, for the initial problem!).
29. tomxor+Sx[view] [source] 2019-11-11 11:36:06
>>svnpen+(OP)
It's also not great advice to be so ridgid in this case... Having written numerical grading code, I learned the hard way that log10 based methods of counting SF is a very bad idea (fp errors occur toward but not at word precision limit) counting string length with some solid regex for extracting integer fraction and exponent parts on the other hand can handle any input precision.
◧◩◪
30. wflemi+TH[view] [source] [discussion] 2019-11-11 13:16:46
>>em-bee+b3
I was quite comfortable with writing the Fibonacci function. Writing that would have hardly taken me any longer and would not have taught me anything either.

The thing I did learn, and which I think has paid more dividends in my career than knowing the definition of the Fibonacci sequence, is that knowing your tools well can save you time and effort and reduce the amount of your own code you need to maintain and put effort into.

◧◩
31. moron4+kL[view] [source] [discussion] 2019-11-11 13:48:28
>>ianai+l1
Drastically depends on the kind of problems you're working on.
◧◩
32. cjfd+eM[view] [source] [discussion] 2019-11-11 13:58:43
>>lilyba+K8
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....
replies(1): >>WorldM+QF1
33. surfsv+tO[view] [source] 2019-11-11 14:18:32
>>svnpen+(OP)
In school the goal isn’t to solve the problem, but to learn something in the process of solving that problem.

Thinking outside the box is of course valuable, but the teachers have specific concept they want to teach and constraints like that help them accomplish that.

When it comes to tests, it’s not as easy. At tests it’s is more about solving the actual problem, but one could still argue that the test is meant to test how well one has learned what was thought, not to solve the problem.

I think this is totally fair in this case.

◧◩◪◨
34. gpdere+mP[view] [source] [discussion] 2019-11-11 14:23:39
>>NightM+1o
Probably the students do not know about big O notation at this point, but requiring a solution that uses at most O(1) additional space would be enough. Then again, automatic bignums in python make it hard to evaluate the space complexity.
◧◩◪◨⬒
35. gpdere+FR[view] [source] [discussion] 2019-11-11 14:42:23
>>slavik+br
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+W11
36. jstrie+uU[view] [source] 2019-11-11 15:02:13
>>svnpen+(OP)
This problem is designed more in the spirit of teaching the algorithmic thinking side of the course objectives than the Python side.

If you prefer, you can view the problem restrictions as a means to encourage students to develop a more general algorithm for solving the problem, rather than a programmatic implementation specific to Python. Hopefully this makes them seem less arbitrary.

◧◩◪
37. jstrie+UU[view] [source] [discussion] 2019-11-11 15:06:13
>>svnpen+a2
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.

◧◩
38. jstrie+qV[view] [source] [discussion] 2019-11-11 15:10:34
>>ianai+l1
It's a programming class where "algorithmic thinking" (and therefore, in my opinion, some amount of math) is one of the course objectives.

Since the discussion takes place in a low-pressure, guided group lesson, students will certainly not be penalized for suggesting the string method in that setting. Also, students trying to use strings on homework will fail tests in the linter and autograder before they finally submit, so they will have plenty of opportunity to fix their mistake before receiving a grade on their work.

39. MrSton+OX[view] [source] 2019-11-11 15:26:07
>>svnpen+(OP)
There is also the issue that they may not know that the thing isn't "taught" yet because they don't know its a "thing".

I once got points taken off for using a recursive function as part of my solution. I didn't know the language before, nor did I have much experience with recursive functions as a formal concept.

I didn't even know at that time that recursive functions were a special thing that had to be taught to me.

◧◩
40. MrSton+oZ[view] [source] [discussion] 2019-11-11 15:37:14
>>em-bee+43
Constraints are fine.

Blocking off a generic "anything you haven't been taught yet" is a hellfire of ambiguous egg shells.

Strictly speaking you might not have been taught to use division with loops. Maybe you haven't been taught about the log10 function, or you have but you haven't been taught about combining the log10 function with the round/ceil functions, are you sure you were taught about putting if's before these exact functions?

The issue is every programming question requires by its very nature that you use something you haven't been taught to solve it. So you have to know what the teacher counts as acceptable vs unacceptable, and this comes into weird edge cases where the logic behind why something would be considered part of the "you must be taught first" category vs the "you are to assume figuring this out is part of the question" category requires knowing an overview of how to teach computer science, something the students would not have.

I once got points taken off for using a recursive function as part of my solution because that wasn't taught until the next quarter. I didn't know the language before, nor did I have much experience with recursive functions as a formal concept. We knew how to make functions, call functions, do loops, and even do gotos.

I didn't even know at that time that recursive functions were a special thing that had to be taught to me, it just came to me as the way to solve the problem.

replies(1): >>em-bee+Z91
◧◩◪◨⬒⬓
41. Mauran+W11[view] [source] [discussion] 2019-11-11 15:56:05
>>gpdere+FR
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+ma1
◧◩◪
42. em-bee+Z91[view] [source] [discussion] 2019-11-11 16:48:23
>>MrSton+oZ
i agree, constraints and "anything you haven't been taught yet" are two different things.

constraints should be spelled out explicitly.

The issue is every programming question requires by its very nature that you use something you haven't been taught to solve it

that, i don't agree with. it's entirely possible to state programming questions in such a way that they only require things you have already been taught. (in its simplest form "being taught" means "being told about". most (if not all) freecodecamp exercises work like that.

◧◩◪◨⬒⬓⬔
43. gpdere+ma1[view] [source] [discussion] 2019-11-11 16:50:41
>>Mauran+W11
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+7C1 >>slavik+7E1
◧◩◪
44. tzs+pb1[view] [source] [discussion] 2019-11-11 16:58:19
>>svnpen+a2
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.

◧◩◪◨⬒⬓⬔⧯
45. Mauran+7C1[view] [source] [discussion] 2019-11-11 19:32:33
>>gpdere+ma1
> 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.

◧◩◪◨⬒⬓⬔⧯
46. slavik+7E1[view] [source] [discussion] 2019-11-11 19:46:47
>>gpdere+ma1
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.

◧◩◪
47. WorldM+QF1[view] [source] [discussion] 2019-11-11 19:58:20
>>cjfd+eM
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.
replies(1): >>ghusba+w62
◧◩◪◨
48. ghusba+w62[view] [source] [discussion] 2019-11-11 23:12:51
>>WorldM+QF1
We're talking about converting numbers to strings, for which none of that matters.
replies(1): >>WorldM+Un3
◧◩◪
49. dTal+IV2[view] [source] [discussion] 2019-11-12 10:46:42
>>ruroun+Sa
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.

◧◩◪◨⬒
50. WorldM+Un3[view] [source] [discussion] 2019-11-12 15:34:17
>>ghusba+w62
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]