Highlights From Week 2
After an introductory week of defining key Python syntax, binary notation, and type properties, week 2 of MITx’s 6.00x course jumped right into some more “meaty” programming 101. It started with one of the most fundamental concepts of programming – iteration and loop mechanisms.
The concept was ultimately demonstrated with an exercise that asked for a guess and checking program that would print out any number you’re thinking of between 1 and 100…with a little user input verification of whether the current guess was higher or lower than their number. One way to do this game is obviously to just make the program ask the user if his/her number was “1” and wait for confirmation, and then guess the next number. The number can’t be lower than “1”, so the user will input “higher” which tells the program to guess the next number. In this case, “2” then “3” and so on until it guesses correctly.
That would technically work, but it would also be incredibly monotonous for the user, and also horrendously slow. (If you thought of the number “99” you’d be there forever. And forget about it if you ever wanted to increase this program’s range to guess a number between say, 1 and 1000.) So in addition to learning about loops, this week was also a demonstration in making more efficient programs. In this case, with a binary search algorithm.
The next lecture and problem set series was on functions, and looking at how and why defining custom functions (as well as importing functions from external files) can give a program tremendous power beyond its initial built-in capabilities. For example, if you wanted to convert a temperature from Celsius to Fahrenheit (for us American folk), there’s no built-in ability to just do that on Python. BUT, you can certainly create a formula yourself.
Simply apply the proper math with the proper code. Sample below:
return temperature * 9/5 + 32
Now you have permanently defined a formula called “CelsiusToFahrenheit” that you can forever use at any point in that program and you won’t have to retype the math each time. Neat.
Now back to the course. There were two psets this week that asked us to write functions and programs that incorporated loops to count the number of vowels or substrings within a word and a nifty assignment to use the bisection search algorithm to figure out the minimum monthly payment on a hypothetical credit card bill. It took me a total of roughly 6-8 hours to go through every video, every finger exercise (generally multiple choice mini quizzes between videos), and both psets for Week 2. That was with a bit of previous knowledge from last year’s CS50x course. Although I won’t lie, I did get a bit anxious when I read this in pset 1:
Note: This problem is fairly challenging. We encourage you to work smart. If you’ve spent more than a few hours on this problem, we suggest that you move on to a different part of the course. If you have time, come back to this problem after you’ve had a break and cleared your head.
Ah yes. The ol’ “walk away and clear your head” tactic. Programmers know it well.