Learn in Public: My First Time Writing Code
Two weeks ago, I had a wonderful opportunity to write my very first code. The opportunity came in form of a series of exercises I had to complete during my application to participate in a free online class on programming.
What Is This Online Class About?
This free online class, called Code in Place (as in learning to code while sheltering in place) is an introductory computer science course using the Python language. The class is organized by a group of computer science professors at Stanford University who want to offer their teaching services free of charge as an act of community service during this time of COVID-19.
The online class has learning materials from the first half of Stanford's established introductory course, CS106A. CS106A is one of the most popular courses at Stanford University. It teaches the fundamentals of computer programming using the widely-used Python language.
The class is for those with no prior programming experience (like me), who want to learn introductory coding. Participants would not receive a grade in the course, nor would completing the class earn us a university credit. However, by the end of the course, we would have acquired a new and invaluable skill - how to program.
When I heard about this opportunity, I was excited because earlier that week, I had just made the decision to learn to code so I knew that class was going to be my starting point. So off I went, to register my interest.
At the site, applicants were encouraged to learn the basics of a robot named Karel, after which we were to finish some coding tasks before a deadline (which for me, was only 2 days away!).
Little bit about Karel
Karel was designed in the 80s as an introductory programming environment in which students teach a robot (Karel) to solve simple problems. He lives in a world consisting of a grid of streets (left-right) and avenues (up-down) and understands 4 basic commands:
move() - Karel moves by 1 square in the direction he is facing;
turn_left() - Karel turns 90° left;
put_beeper() - Karel puts a beeper on the square he is standing at; and
pick_beeper() - Karel lifts a beeper from the square he is standing at.
Karel programs have the same structure and involve the same fundamental elements as the Python programming language except that Karel's programming language is extremely small and easy to master.
I went through the required chapters of Karel Reader (an eBook containing detailed information about Karel) and gained amazing knowledge on:
Defining new functions.
Basic For loops and Nested loops.
Matching postconditions with preconditions.
and a bunch of other stuff.
Now Onto The Code I Wrote!
There were 3 coding exercises to be completed and each task was to be done in an Integrated Development Environment (IDE). The IDE had a code editor and an area titled 'World' to run the code already written. For each task, we were to write a program that made Karel accurately carry out a particular action.
One of the exercises, Shelter-in-Place involve writing a program in which Karel collect some food (represented by an object called beeper), right outside his door and then returns to his initial position inside his house. Apparently as people around the world are staying home to reduce the spread of COVID-19, Karel is playing his part by sheltering in place.
Even though the main function for the Shelter-in-Place code I wrote was only a few lines, the task was very much an exercise in decomposition and a lesson in problem solving. It reminded me of this quote by Steve Jobs:
'Everyone in this country should learn how to program a computer, because it teaches you to think.'
In my solution, I defined functions (from the 4 basic commands highlighted above) for Karel to move to the food package and return to his starting point.
Here's the code I wrote:
And here's Karel executing my code:
What a wonderful experience.
PS: Two days after the deadline, I received a mail that I was admitted into the online class!
This story was previously published here