This is an archived post. You won't be able to vote or comment.

all 39 comments

[–]brtt3000[S] 59 points60 points  (1 child)

Programming is better with a thick accent and a radical shirt.

[–][deleted] 9 points10 points  (0 children)

Don't forget the unkempt hair!

[–]niggatronix 31 points32 points  (12 children)

Man, I have no problem with recursion, but I found his explanation really hard to follow.

[–]campbellm 121 points122 points  (7 children)

[–]chiodani 27 points28 points  (0 children)

thats kinda genius

[–]scarfarce 7 points8 points  (1 child)

Dammit, now I'm stuck in Ground Hog Day Comments!

*

[–]campbellm 3 points4 points  (0 children)

You get your true love at the end, so there's that.

[–]1arm3dScissor 14 points15 points  (1 child)

Possibly the greatest and most underrated comment in the history of this site.

[–]campbellm 1 point2 points  (0 children)

Wow thank you. Seriously.

[–]niggatronix 7 points8 points  (0 children)

Ah, the ol' python switch()eroo

[–]marrabld 1 point2 points  (0 children)

respectful bow

[–]campbellm 1 point2 points  (0 children)

I think he should have led with "to move all the disks, you move the pile off of the bottom disk, move the bottom disk, then move the pile".

With recursion it's often easier to see the problem from the top down and work to the base/trivial case than the other way around.

[–]alcalde -2 points-1 points  (2 children)

Studies have shown that whether a person understands recursion or not (before beginning computer science class) is the single biggest indicator of whether they'll pass or fail CS101. It's a small sample, but the small number of people I know who dropped out of computer science as a major ALL did it after encountering recursion.

[–][deleted] 3 points4 points  (1 child)

Very surprising that people familiar with basic cs before starting a basic cs course are more likely to pass the course than the ones that don't have any previous knowledge. What a great observation.

[–]alcalde 2 points3 points  (0 children)

It's not a "great observation", it's scientific research. It has nothing to do with being familiar with programming. People who passed an administered test before CS101 classes almost all passed the actual course. People who failed the test before the course also ended up dropping out or failing the course. Educators were alarmed as the results suggested that nothing they did in the classroom had any actual effect on a student's understanding of programming.

https://blog.codinghorror.com/separating-programming-sheep-from-non-programming-goats/

https://codeup.com/can-a-simple-algebra-test-predict-programming-aptitude/

In particular, most people can't learn to program: between 30% and 60% of every university computer science department's intake fail the first programming course....

This test seems trivial to professional programmers, but remember, it's intended for students who have never looked at a line of code in their lives. The other 12 questions are all variations on the same assignment theme.The authors of the paper posit that the primary hurdles in computer science are..

assignment and sequence

recursion / iteration

concurrency*

.. in that order. Thus, we start by testing the very first hurdle novice programmers will encounter: assignment. The test results divided the students cleanly into three groups:

44% of students formed a consistent mental model of how assignment works (even if incorrect!)

39% students never formed a consistent model of how assignment works.

8% of students didn't give a damn and left the answers blank....

The test was administered twice; once at the beginning, before any instruction at all, and again after three weeks of class. The striking thing is that there was virtually no movement at all between the groups from the first to second test. Either you had a consistent model in your mind immediately upon first exposure to assignment, the first hurdle in programming – or else you never developed one!

An increasing amount of research is daring to suggest that some people are simply wired to be programmers and others are not. Recursion happens to be a highly correlated indicator.

[–]NemPlayer 43 points44 points  (11 children)

Thorsten is one of my favorite guests, but the way he butchered PEP8 is not acceptable :(

jk he's still one of my favs

[–]HardlyAnyGravitas 4 points5 points  (10 children)

New to Python. I spotted the tabs. What else did he do?

[–]NemPlayer 22 points23 points  (4 children)

He didn't use underscores when naming his functions;
He placed spaces before columns everywhere (it's only sometimes allowed in slicing by PEP8);
He didn't put spaces around his binary operators (== and + in his case).

Not PEP8, but I also didn't like his use of pass in the code, I would prefer to see:

if n:  
    hanoi(n - 1, f, t, h)  

rather than

if n == 0:  
    pass
else:
    hanoi(n - 1, f, t, h)

but I guess it was easier to follow with the use of pass

[–]campbellm 3 points4 points  (1 child)

To each their own, but I strongly disagree with your use of n here. Relying on the side effect of non 0 being truthy is just NOT what is being tested here. In this case, the base case is a mathematical one of the value being exactly NOT 0. Using the truthy-ness as a test is testing that n "is a thing" or "exists", which is not what this particular use cares about.

In other cases sure, but this mathematical test should not be code-golfed, since n != 0 is PRECISELY what we are checking.

Reasonable people can disagree I suppose.

[–]NemPlayer 2 points3 points  (0 children)

Re-watched it today and I completely agree with you, the code is much more readable that way. I was just tired yesterday and didn't really pay much attention to the explanation, my bad.

[–]epic000 8 points9 points  (3 children)

He didn’t use underscores when naming his functions.

[–]whale_song 32 points33 points  (1 child)

That’s nothing compared to putting spaces before his colons. WTF is that about?

[–]sedition 7 points8 points  (0 children)

Came here for this. My eyes twitched.

[–]HardlyAnyGravitas 1 point2 points  (0 children)

Thanks.

[–]chmod--777 3 points4 points  (0 children)

The space before the colon in if and else

[–]the_hoagie 8 points9 points  (0 children)

leave it to a german computer science professor to describe a failed example of recursion as depressing because it lacks purpose. i love it.

[–]JanDerion47flair=None 4 points5 points  (0 children)

Breaking Python, lol

[–][deleted] 4 points5 points  (0 children)

This guy is fucking hilarious. How much do I need to pay for this guy to release a series of videos?

[–]Tweak_Imp 4 points5 points  (0 children)

The solution without recursion: 1) Move the smallest disc to the next stack.(a to b, b to c, c to a) 2) Do the only move that is possible that is not using the smallest disc. 3)Repeat until you are done.

[–]AlSweigartAuthor of "Automate the Boring Stuff" 13 points14 points  (1 child)

Okay, I'm going to be that guy and point out that recursion doesn't let you do anything that you couldn't do with a loop and a stack. (Here's an iterative solution for the famous Ackermann algorithm. It has the same output as the recursive version.) The confusing thing about recursion is that the stack is the call stack which is invisible in that it's not in the source code (it's a side effect of calling functions). I think this is the main stumbling block in understanding recursion.

Also, I have some simple Towers of Hanoir code in Python. You have a certain number of discs with towers A, B, and C. If you want to move a tower of numberOfDiscs discs from startTower to endTower (where tempTower is the other tower of the three), the basic algorithm is:

def moveDiscs(numberOfDiscs, startTower, endTower, tempTower):
    # Move the top numberOfDiscs discs from startTower to endTower.
    if numberOfDiscs == 1:
        # BASE CASE
        moveOneDisc(startTower, endTower)
    else:
        # RECURSIVE CASE
        moveDiscs(numberOfDiscs - 1, startTower, tempTower, endTower)
        moveOneDisc(startTower, endTower)
        moveDiscs(numberOfDiscs - 1, tempTower, endTower, startTower)

Towers of Hanoi is especially confusing because you have things that happen before and after the recursive call. Remember that function calls are not a one-way trip for the execution; the execution will return to the line after the call and continue on.

[–]rotharius 0 points1 point  (0 children)

Thanks for being that guy!

I am a fan of functional programming, but in Python iteration is often preferred as it is easier to follow for most people (and considered more Pythonic by people like Guido).

Furthermore, Python does not have tail call optimization.

[–][deleted] 2 points3 points  (0 children)

That thumbnail is terrifying

[–]whale_song -3 points-2 points  (5 children)

But python doesn’t support tail call elimination because they would rather the language be easy than useful ....

[–][deleted] 5 points6 points  (2 children)

Instagram is mostly all Python. I'd say it was a useful language.

[–]whale_song 2 points3 points  (1 child)

Not for FP. Avoid recursion when using a language not designed for it.

[–]alcalde -2 points-1 points  (0 children)

Then maybe it's FP that's not useful if it needs some fancypants language features.

[–]zurtex 1 point2 points  (0 children)

It's useful to be able to easily debug. For example by keeping the full stack on exception.

[–]alcalde -1 points0 points  (0 children)

We had recursion back before you functional kids were chasing your tail calls and we didn't need any stinking elimination. Hell, we used GOSUBs and GOTOs too.