Slow Steps and Weapons
I keep having to remind myself that my brain thinks in a certain dialect.
That I'm not the sponge I once was. Sometimes though I'll forget this, and reality has a harsh way of reminding me. Directly in the face. Repeatedly.
What I mean is that I know I'm trying to teach my brain to think completely differently than how its been used to the past few decades. I understand the logic of mechanics, and can mentally create a flow chart on how computer hardware works. But programming? No, its not the same. Its exact, its direct, its unforgiving.
An example would be like if your video card cooling fan goes out. So you'll experience graphics artifacts on the screen, or the system might lock up all together. You change out the entire card, you're done. If your code isn't correct, you don't just change out the computer and it suddenly works.
But then, I had a weapon. In fact, I had several of them.
This last week I was on Week 5 of Learn to Program: The Fundamentals by University of Toronto. I had finished all of the videos, answered all the quizzes, and got to the final test for the week. Per normal, I went through and answered all of the questions I knew first. Then I went to the ones I didn't know. And thats the problem I had. I didn't know them. I mean, at all. I stared at the parenthesis, colons, 'i' variables, etc. and things got more and more fuzzy and my brain got mushy the longer I stared. So I took educated guesses. You're allowed to take the test 3 times every 8 hours. First time around I got a 71, the second time I got an 88. So I passed. But....I didn't truly know the answers even though I'd passed. This bothered me. I'm taking these courses to learn, not to pass by guessing. So even though I'd passed, I went back to week 5 and looked at each question. I needed to understand this, for me.
def while_version(L): """ (list of number) -> number """ i = 0 total = 0 while i < len(L) and L[i] % 2 != 0: total = total + L[i] i = i + 1 return total
Above is an example on how I went from Ok, I can follow this mostly to Ok wait, I am the dumb. Yes, you might see this easily enough because your brain works that way. To me, this was 10 lines of confusion. If programming is exact, this code block is wrong.
The 2nd line (DocString) explains that the format to call this function is a LIST of numbers, and has an output of a number. Now, in Python, a LIST is encompassed by square [ ] brackets. Such as
list1 = [1, 2, 3, 4, 5] but the code example shows me its in parenthesis ( ). So if programming is exact, this is wrong syntax. So it didn't make sense to me. And it got me so confused, I went down a dark hole. Never again to understand any of the previous code I once understood. It was cold. It was lonely. It was damp.
But then, I remembered I had a weapon. In fact, I had several of them. I work with the kind of weird people that look at this stuff for fun (and profit). If I have questions, I'm going to use this arsenal for my benefit.
First I went to Sam. He's a Python guy, so he'll easily understand the syntax and be able to explain it. Sure enough, he pulled up a chair next to me and walked me through this. He showed me that while the argument/parameter for the initial function call was shown in parenthesis, it still required the input to be in a LIST format. So
def while_version(L): is whats listed, the actual call could be in the
def while_version([L]) format. Or that the list could be a variable of
list = [1, 2, 3, 4, 5] and then the call could be
def while_version(list). That really opened a door for me. I could then follow the process. Sam is one of my weapons.
What he did that really showed me a light was had me write it down on the dry erase board. Each step, write down the actual value. If i = 1 in this step, write down 1. If L equals 'A', write down 'A'. With this, it allowed me to connect the dots to see what 'i' was. I could then see that on the first iteration, i was equal to 0.
So now I'm back home and while week 6 started 3 days ago, I refrained. I refused to start until I understood week 5 first.
With this knowledge, I felt revived and excited again. I wasn't behind a rock. Or worse, under one. Something so simple was tripping me up. I looked at other questions I'd guessed on and slowly...letter by letter slowly, I was able to see what the code was doing. Man this is neat!!! It was like learning how to introduce yourself in another language. Its the only thing you can say, but damn it you can say it!
One question I missed was so easy, I became upset at my exuberance of this newfound knowledge.
What is the sum of the odd numbers from 1523 through 10503, inclusive Hint: write a while loop to accumulate the sum and print it. Then copy and paste that sum. For maximum learning, do it with a FOR loop as well, using range.
To me, I knew I could use the built in sum function and find the answer. So I ran
sum(range(1523, 10503, 2)) and of course the immediate answer was 26993880. And of course the immediate answer was incorrect. First, my code was neither a WHILE loop, nor a FOR loop, nor was it inclusive. I showed this to Sam, and he quickly deduced that the first thing he would do would be to break out each number into a variable first, then write the loop. It took me a few tries to get it, but I finally got my brain to think detailed enough to understand that A + B = C. This equation tells you there are 3 different numbers. So I would set variables for each number. A = 1523. B = 10503. C = the sum/total. Now, for the loop.
a = 1053 b = 10503 c = 0 while a < b: a = a + 2 c = c + a print (c)
Problem, while I got an answer with this (Yay me), it was still the wrong answer. But I didn't know this yet. I went to another expert (see websters: geek/nerd) as I was curious to how he saw this question. Brian mainly writes in C++, I asked him to write code in his preferred language to see how it looks. Without missing a beat he asks me "inclusively or exclusively"? I had to think. What did the question say? Inclusively! Brian commenced writing down ancient latin symbols and spoke in tongues, the kind not used for centuries before mankind. I swear he had over 30 lines of code where I only had 6. But damn, the code ensured the input and output were correct before doing anything. It was impressive. Needless to say, my brain went to a state of mush again.
I showed him a few of my lines. Then I showed him the first trials I had that failed. And while this guy doesn't know Python, he immediately saw every error I had originally. I wrote down my complete first try and he said "oh that won't work at all. You'll get 10503 each time, and it will loop with that number. Here, let me show you why". Amazing. I just saw a guy not only show his expertise and knowledge, but his ability to quickly change his brain to think programmatically. Even when he knew nothing of the language. This guy is way above me. He's one of my weapons too.
Today I went back into work, and asked Sam to go through another problem on the test with me, so I would see where I might be thinking incorrectly. Again, he grabbed a chair and walked me through it. Then I went through another and he verified I was on the right path. So now I'm back home and while week 6 started 3 days ago, I refrained. I refused to start until I understood week 5 first. Now, I'll click on Resume Course and begin taking more notes in earnest.
Thanks guys for your guidance and putting up with someone thats as slow to see the light as me. Your help is invaluable to me.
Oh, why was my code wrong? Because I didn't write it inclusively. My WHILE loop needed to be
while a <= b: less than or equal to. And the FOR loop was:
total = 0 for i in range(1523, 10503, 2): total = total + i print(total)