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:

Now let's try out some more! Given a list of numbers lst, write one-line list comprehensions to do the following:

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:

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:

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!