Section #9
November 15th, 2020
Written by Nick Parlante, Brahm Capoor, and Juliette Woodrow
Note: Section this week is mostly about life after CS106A! Feel free to ask your section leader about their path in computer science or at Stanford in general. This is a great time to ask about different classes to take. If you are interested in becoming a section leader, you can also ask about that process! The problems this week are just for fun. They are out of the scope of the class, but I wanted to give you a way to practice list comprehensions and Jupyter Notebooks. Feel free to post on Ed or email Juliette if you have an questions about them.
List Comprehensions
In this problem, we'll go over how list comprehensions can be used to solve hard problems in satisfyingly few lines of code.
Here are a few problems you can start with. These should be solved with one line of code:
- Construct a list of all the numbers 1-1000 inclusive that are divisible by 8.
- Construct a list of all the numbers 1-1000 that contain 3 as a digit at least once.
Now let's try out some more! Given a list of numbers lst, write one-line list comprehensions to do the following:
- Produce a list of the absolute difference between each of the numbers in lst and 10. Recall that the abs function returns the absolute value of a number.
- Produce a list of the absolute difference between the numbers in lst that are between 10 and 15 inclusive, and 10. Recall that the abs function returns the absolute value of a number.
Now, suppose we have a list of pairs such as this one:
pairs = [('zzz', 3), ('bbb', 10), ('ccc', 4), ('aaa', 6)]
Write one-line list comprehensions to do the following:
- Produce a list of the second elements of each tuple in pairs.
- Produce a list of the second elements of each tuple in pairs, so long as the tuple's first element does not begin with a 'c'.
- Produce a list of the first elements of each tuple in pairs, except that the first character of each of these elements is made uppercase. You can assume that each such element in the tuples has at least one character.
Data Analysis Using Jupyter Notebooks
In class this week, we explored the use of Jupyter notebooks as a means of more interactively engaging with the code we write by embedding it in a narrative that also includes textual commentary, images and graphs. In this problem, we'll be employing those same tools to aid us in solving a very real problem.
To get started, make sure you have the Jupyter notebook package installed, which allows you to open and run Jupyter notebooks on your own computer as well as matplotlib, which allows you to quickly and easily draw graphs. If you haven't done this already, open your Terminal application (called 'Terminal' on a Mac and 'Command Prompt' or 'PowerShell' on Windows) and run the following command:
$ python3 -m pip install jupyter matplotlib
If you have a Windows computer, substitute python for python3.
Next, go ahead and download and unzip the section starter code here. There are two files in this starter code:
- who_won_the_race.ipynb: The Jupyter notebook you'll be doing all your work on.
- times_and_distances.csv: The dataset you'll be analyzing.
Now, you'll need to open this folder in your terminal. On a Mac, type cd into your terminal and (without pressing enter), drag the folder from Finder into the terminal window (you should see the full folder path show up) and then press enter. On a PC, open the directory containing the folder, right-click the folder and click 'Open Command Window here'.
You should now have a terminal window that is open in the Section8 folder. Now, type jupyter notebook and press enter. This command should open up your web browser to the Jupyter Notebook explorer. Open who_won_the_race.ipynb to start the project!