My Brain Hurts: recursion with factorials

With pencil and paper in hand, I have no problem with recursive patterns. Fibonacci schmibbo-hotsytotsy. Factorials schmacks-Oreos.

“Write a recursive function to …” blah mork loopy loo let me leave the interview now so you can continue with someone better, and I can drown my sorrows in two scoops of ImNotWorthy. 

I know this because it was not until I was asked to demonstrate recursion in a mock interview (I requested this topic), that I saw that my comprehension was passive. I understood it when I saw it. … ish. Understood-ish. But there was absolutely no active comprehension. I could not do it on my own. No ish. Nuthin. Fear? Nerves? A pebble in my shoe?

It is that whole “snake eating it’s tail” that was throwing me. I could not recreate what was going on. There is no stack with pencil and paper. I can write it out and do them in my head until I needed a calculator, but even with a calculator, all I needed was a line:

5! = 5 x 4 x 3 x 2 x 1

I understood stacks in other terms (callbacks, pancakes), but the recursion was hurting my brain, until I made it a literal stack in my head. I was treating the part of the code block after the base case as a spiral when (for me) thinking about it as slapping 3×5 cards down. No wonder I didn’t understand the stack; I wasn’t treating it like one.

I’ve been playing with recursion and different tasks as I plow through books on algorithms and tech interview skills. Factorials and Fibonacci are the easier ones to grasp. Not easy. EasIER. Comparative. Like farts smell better than decomposing bodies. They don’t smell good, just better.

I wouldn’t say that I could whip out recursion any time, and I know that I wouldn’t want to with factorials (O(n2) and all that), but I’m getting there. I spent some time on JSFiddle and with my markers to play.

factorial_recursion

The code is old news.

const factorial = n => {
if(n===1){
return 1; //my base case
}
return n * factorial(n - 1); //my nut case
}

I did not set include checking to see that n is a non-negative integer. That was not the point of my doodling because checking for that was not the hard part. This post isn’t about a fully-tested function to return the factorial of a number; this is about me getting the recursive part.

750Words day 7: teaching programming to kids

Today’s topic came easy to me as teaching programming to children is something I am doing a little of and want to do more of. Also, it is a topic that sets me off. Not the programming part, but the idea that teachers are those who cannot do.

Keep in mind that what is below is unedited and part of a 750word challenge. It’s not my attempt to enter the Great American Essay contest. Egads, no. What I’m telling you is that the paragraphs and words appear as I thought of them. Sozz.



It will come as no surprise to find that I, a former teacher, am offended by “Those who can, do; those who cannot, teach”. It is a simple way of separating those people who want to teach what they love from those who want to research what they love. I can tell you that one is significantly more selfless than the other. It is also a naive understanding of what it takes to teach. Expert knowledge in a field (something that most people are not, anyway) does not mean you can explain it to someone who has never heard of the topic, struggles with the topic, or would rather be outside than listening to you blather on about the topic. I am glad that people who do not teach are not in the classroom. I find that to be selfless. Just like I do not want to have children because I know I do not have what it takes. It’s a selfless act not to knowingly screw up a child. My physicians were those who did not choose to teach biology. Good. I needed a doctor when I broke my leg, not a biology teacher. But do you sincerely think that a surgeon could explain mitochondria to a 9th-grade biology class?
Step back. Remember that teaching it means that they start from little to no understanding and end up with a workable gist (who on earth understands cells straight off?). Maybe. The classroom will have students who are inclined towards science and others who are in the class to satisfy a requirement. Maybe there’s hormonal drama, bullying, abuse at home. Teaching biology to 9th graders or even first-year university students is not going on about your research at a med school faculty cocktail party. It. Is. Not. The. Same! So stop it. Those who can teach, teach; those who cannot teach or do not wish to teach, find another profession.
People who get into teaching from another profession without reading up on child development are in for a shocker. Teachers have to be open to being wrong. Ok. I’ll be a victim of sweepinggerneralisationus maximus, but I can’t imagine the typical surgeon being ok being wrong. In her defense, surgeons are not allowed to make mistakes by people who feel they should be allowed to make mistakes. Did you follow that? Physicians are never allowed to make a mistake. People confuse that with malpractice. There may be a mindset to a physician that would make them less than ideal in the elementary school classroom. Teachers have to deal with the different types of intelligence in a classroom of different personalities at different levels of development. But no. Go on. Tell me more that those who cannot, teach.
I thought about that moronic quotation when I was teaching Python to middle schoolers. We forget that we have already made the leap from concrete to abstract, but our students may not be there. It’s not like teaching math (which I did). People teaching coding and programming languages to middle schoolers do not have thousands of master teachers to lean on for that topic. Teachers are all over the place for advice about behaviour, but many (I’d say most) have zero to zero-est experience with Python, Java, JavaScript, HTML, CSS … Many think coding, programming, and computer classes are all the same. “We already have a computer class.” Super. Are the kids designing? Are they working with backend? Are they making buttons to hope from page to page or add or check an answer? Or are these MSOffice products? No, really. What are you saying?
In one class, I realised how much I still have to learn when I was trying to help a child get his if/elif/else tree down. While the other children understood that the first level of if/elif/else need to answer that one question or raw_input, he had the elifs as questions that would be the nested and next-level questions. ARGH! Palm to forehead. We did not see that happening when we started, but knew we had to step back and add, for that child, another element to the lesson. It is not patently obvious that the if/elif/else must be at the same level. It is to us, but not to a child who might still be at the concrete-sequential level of development.
I am not the teacher in that class. I assist. The teacher is much younger than I am. 22 years! She had no formal education in teaching but knew what to do was to step back and rethink how to teach if/elif/else to a child who doesn’t naturally see the levels. This is not what a computer science professor worries about. Her lecture hall will be full of young adults who just know that if they’re asked “Are you hungry”, the possible answers (at a basic level) are “Yes”, “No”, “Maybe”, “I don’t know”. They know that the answers are not “Yes”, “What do you want?”, and “What fridge?”.
Teaching to middle schoolers is bringing back to me what I love about teaching. It’s the problem-solving. It’s getting a group of children to understand more than they did when they entered. It’s adjusting how you teach or present a topic on the fly. It’s not about dealing with the administration. It’s not being told that a child who is a bully cannot be disciplined because the parents are wealthy and donate a lot of money. Teaching with this group also brings back that teaching comes from a passion, not a degree.
I am still looking for a full-time job; however, I hope to always keep space open for teaching or tutoring. I would think my life was darned near perfect if I could have a full-time job as a software developer and then teach it on the side.