Lab 10: Using resources (Google and Stack Overflow)

After solving the Muffin Heist case (with your help of course), Sherlock and Watson have decided to stay at Brown for another semester. Even though they have now enrolled in and taken a bunch of different classes, they still have a lot of unanswered questions about things that weren’t covered in their courses. They have been directed to the internet – but the vast and juicy interwebs can be convoluted and overwhelming. Since they’re not yet accustomed to the ways of the world wide web (being from the late Victorian era), they’re going to need some help.

Sherlock and Watson often find themselves wanting to use a bit of code to handle a problem that’s not covered in the course. To do this, it helps to know how to search effectively online for operations and information on programming languages. This week’s lab is designed to help you, Sherlock, and Watson learn how to do this! (In other words, getting stuck and unstuck is part of the point this week, so don’t get frustrated.)

We will be working on searching online for answers to Python/coding questions! Searching the Internet for useful information to debug your code is an important skill in computer science! There’s a lot of info out there, and it can feel overwhelming to parse through it all. We’re going to start with a couple examples of scenarios and potential queries, and then we’ll apply these skills to write a program that reads from and writes to a file.

Googling for Python questions

We’ll go through a couple scenarios in which you might want to search online for information. Each scenario has several queries. For each of these scenarios:

  1. Predict which example queries might be good and which might be bad.
  2. Google the queries and look into a couple of the search results. Try to note whether the same sites are consistently helpful.
  3. Write a ranking of the queries.

Then, after you’ve made a ranking for each scenario, think about the common elements of the good and bad queries.

Scenario #1: Getting Help on Error Messages

Sherlock and Watson are taking their first CS class, and they write the following code 2+"1", which throws an error: TypeError: unsupported operand type(s) for +: 'int' and 'str'. Help Sherlock and Watson search online for help understanding the error.

Example queries:

Scenario #2: Getting Details on an Operation

Sherlock and Watson have grown quite fond of ASCII art, and they want to be able to use print, but without a new line (as is printed by default). In other words, they want print("(\") and print("(\") to print:
(\(\
rather than:
(\
(\

Example queries:

Scenario #3: Finding an Appropriate Operation

Sherlock and Watson are doing list operations but want to write a base case that checks if a list is empty, but they don’t remember or know how to do that.

Example queries:

After thinking about your own takeaways, take a look at the TAs’ recommendations here.

Scenario #4: Which resources should I use?

Each time Sherlock and Watson do a Google search, they get just a little bit frightened at the pages and pages of results that pop up – how do they know which ones to click on? Which ones are actually legitimate and useful?

Let’s say you want to write a function that takes in two lists and adds them together elementwise. For example:

list1 = [1, 2, 3, 4, 5]
list2 = [10, 25, 7, 1, 1]

>> element_wise_add(list1, list2)
[11, 27, 10, 5, 6]

Consider the following three search results for the query: add elements pairwise across two lists Python

Discuss with your partner: which site was the most helpful? When might a Stack Overflow post be more useful? How about a blog post?

When you’re done, take a look at our list of suggestions.

File I/O

Sherlock and Watson are now equipped to navigate and effectively use sites such as Stack Overflow! They want to test their skills by tackling this topic that they’ve been hearing about a lot: File Input and Output in Python.

Sherlock and Watson learn that when you create a file on your computer, you can write a Python program that reads the contents of the file, makes calculations, and even writes data to a new file on your machine.

Candy 2.0

Sherlock and Watson have recently been craving candy a lot, so we’ve been requested to revisit the candy dataset from Lab 3.

Unlike Pyret, Python has no built-in table functionality (reading a table directly from Google Sheets, table functions, etc). To complete this lab, we’re going to have to take advantage of Python’s ability to mutate data, iterate through data, and read and write data to and from input/output files.

Setup

  1. Start off by creating a directory in your computer called lab10. This directory should in the PycharmProjects folder.
  2. Navigate to this GitHub file. In the top right of the dataset, click ‘Raw’. You should see a wall of text on your browser.
  3. Go ahead and copy the entire page of text, paste it into a text editor (you can use something like TextEdit for Mac or Notepad for Windows. Call over a TA if you’re not sure what to use), and save the file as candy-data.csv inside your lab10 directory.
  4. Create a Python file called candy-data.py inside your lab10 directory. This is where you’ll implement the functions below.

Your assignment

You, Sherlock, and Watson should be experts on surfing the web for relevant information and answers now, so let’s put those skills to the test. We aren’t going to give you much guidance about how to complete these tasks; remember the takeaways from Part 1, and try to use online resources (but if you get stuck, the TAs are still here to help).

  1. Write a function that reads from candy-data.csv and calculates the name of the candy with the highest win percentage.

    HINT: If you’re not sure where to start, try following the steps below:

    • Figure out how to read a csv file into Python. Try to find a method that turns the csv into a datatype you’re already familiar with.
    • Try to print out a few win percentages for different candies to make sure you know how to access them.
    • Design your function! Calculating the highest win percentage shouldn’t require looking up new approaches. Instead, use things that we’ve already learned in class (for loops might be helpful here)
    • Test your function to ensure that it works properly. Call over a TA when you’re done.
  2. Write a second function that writes the results of your answer to Question 1 to a file named result-1.csv.

    HINT: This time, start by writing a function that just prints the string “Hello, World” onto the first line of a file. Once you’ve done so, integrate data from the CSV.

  3. Write a function that reads from candy-data.csv and writes the names of the candies with chocolate to a file named result-2.csv, such that each name is on a separate line.

    HINT: Consider which parts of the code your wrote for problems 1 and 2 help you solve this problem.

NOTES:

Graduation

Now that Sherlock and Watson have learned how to find everything they need on the internet, they’ve fullfilled all 30 credits at Brown, cracked a bunch of new cases, and are graduating with full honors!