Skip to content
Snippets Groups Projects
Commit 3b4effbf authored by LOUIS TYRRELL OLIPHANT's avatar LOUIS TYRRELL OLIPHANT
Browse files

Merge branch 'main' of git.doit.wisc.edu:cdis/cs/courses/cs220/cs220-lecture-material

parents e5f8dc6f 123909ee
No related branches found
No related tags found
No related merge requests found
Showing
with 43610 additions and 0 deletions
File added
File added
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
__student__ = []
def __init__():
"""This function will read in the csv_file and store it in a list of dictionaries"""
reload()
def reload():
"""Reloads the data from the CSV file"""
import csv
__student__.clear()
with open('cs220_survey_data.csv', mode='r', encoding='utf-8') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
__student__.append(row)
def count():
"""This function will return the number of records in the dataset"""
return len(__student__)
def get_lecture(idx):
"""get_lecture(idx) returns the lecture of the student in row idx"""
return __student__[int(idx)]['Lecture']
def get_section(idx):
"""get_lecture(idx) returns the section of the student in row idx"""
return __student__[int(idx)]['section']
def get_age(idx):
"""get_age(idx) returns the age of the student in row idx"""
return __student__[int(idx)]['Age']
def get_primary_major(idx):
"""get_primary_major(idx) returns the primary major of the student in row idx"""
return __student__[int(idx)]['Primary major']
def get_other_majors(idx):
"""get_other_majors(idx) returns the secondary major of the student in row idx"""
return __student__[int(idx)]['Other majors']
def get_zip_code(idx):
"""get_zip_code(idx) returns the residential zip code of the student in row idx"""
return __student__[int(idx)]['Zip Code']
def get_pizza_topping(idx):
"""get_pizza_topping(idx) returns the preferred pizza toppings of the student in row idx"""
return __student__[int(idx)]['Pizza topping']
def get_cats_or_dogs(idx):
"""get_cats_or_dogs(idx) returns whether student in row idx likes cats or dogs"""
return __student__[int(idx)]['Cats or dogs']
def get_runner(idx):
"""get_runner(idx) returns whether student in row idx is a runner"""
return __student__[int(idx)]['Runner']
def get_sleep_habit(idx):
"""get_sleep_habit(idx) returns the sleep habit of the student in row idx"""
return __student__[int(idx)]['Sleep habit']
def get_procrastinator(idx):
"""get_procrastinator(idx) returns whether student in row idx is a procrastinator"""
return __student__[int(idx)]['Procrastinator']
def get_song(idx):
"""get_procrastinator(idx) returns the student in row idx favorite song"""
return __student__[int(idx)]['Song']
__init__()
File added
File added
This diff is collapsed.
%% Cell type:markdown id: tags:
## Warmup 1: Take a look at some list methods
List methods [here](https://www.w3schools.com/python/python_ref_list.asp)...
%% Cell type:code id: tags:
``` python
dairy = ["milk", "ice cream", "cheese", "yogurt", "butter"]
# use the .index() method to get the index of "ice cream"
```
%% Cell type:markdown id: tags:
## Warmup 2: Because a list is a sequence, we can use the `in` operator
%% Cell type:code id: tags:
``` python
food_shelf = ["peanut butter", "milk", "bread", "cheese", "YOGURT"]
for item in food_shelf:
if ...:
print(item, "is dairy")
else:
print(item, "is not dairy")
```
%% Cell type:markdown id: tags:
# CS220: Lecture 15
## Learning Objectives
After this lecture you will be able to...
- Open an Excel file and export it to a Comma Separated Value file.
- Open a CSV file in TextEditor/Jupyter and connect the elements of the CSV file to the rows and columns in the spreadsheet.
- Use pre-written Python code to read a CSV file into a list of lists.
- Write Python statements with double list indexing to access any element of a CSV file via a list of lists.
- Write code that answers questions about CSV data by writing for loops on lists of lists.
%% Cell type:markdown id: tags:
## Reading a CSV
%% Cell type:markdown id: tags:
## Example 1: Store the contents of the CSV file into Python lists
%% Cell type:code id: tags:
``` python
# adapted from https://automatetheboringstuff.com/chapter14/
import csv
def process_csv(filename):
# open the file, its a text file utf-8
example_file = open(filename, encoding="utf-8")
# prepare it for reading as a CSV object
example_reader = csv.reader(example_file)
# use the built-in list function to convert this into a list of lists
example_data = list(example_reader)
# close the file to tidy up our workspace
example_file.close()
# return the list of lists
return example_data
```
%% Cell type:markdown id: tags:
#### Call the `process_csv` function and store the list of lists in `cs220_csv`
%% Cell type:code id: tags:
``` python
cs220_csv = process_csv('cs220_survey_data.csv')
cs220_csv
```
%% Cell type:markdown id: tags:
#### Store the header row into `cs220_header`
%% Cell type:code id: tags:
``` python
cs220_header = cs220_csv[0]
cs220_header
```
%% Cell type:markdown id: tags:
#### Store all of the data rows into `cs220_data`
%% Cell type:code id: tags:
``` python
cs220_data = cs220_csv[1:]
cs220_data
```
%% Cell type:markdown id: tags:
## Example 2: CSVs as a List of Lists
%% Cell type:markdown id: tags:
#### Print out the lecture number of the 4th student by hardcoding its row and column
%% Cell type:code id: tags:
``` python
cs220_data[...][...] # [row][col]
```
%% Cell type:markdown id: tags:
#### Print out the sleeping habit for the 2nd student by hardcoding its row and column
%% Cell type:code id: tags:
``` python
cs220_data[...][...]
```
%% Cell type:markdown id: tags:
#### Print out how many students completed the survey
%% Cell type:code id: tags:
``` python
len(...)
```
%% Cell type:markdown id: tags:
#### Print out every student's sleep habits and major
%% Cell type:code id: tags:
``` python
for i in range(len(cs220_data)):
current_sleep_habit = ...
current_major = ...
print(current_sleep_habit + '\t\t' + current_major)
```
%% Cell type:markdown id: tags:
#### Print out every students' age in 10 years
Fix the bug in the code below
%% Cell type:code id: tags:
``` python
for i in range(...):
current_age = cs220_data[i][2]
print(current_age + 10)
```
%% Cell type:markdown id: tags:
## It would be nice to have a helper function!
Let's introduce `cell`
%% Cell type:markdown id: tags:
#### Remember creating `cs220_header`?
%% Cell type:code id: tags:
``` python
cs220_header
```
%% Cell type:markdown id: tags:
#### Get the column index of `"Pizza topping"`
%% Cell type:code id: tags:
``` python
cs220_header.index(...)
```
%% Cell type:markdown id: tags:
## Example 3: Create a `cell` function
We want to invoke something like...
* `cell(24, "Pet owner")`
* `cell(63, "Zip Code")`
%% Cell type:code id: tags:
``` python
def cell(row_idx, col_name):
col_idx = ... # get the index of col_name
val = ... # get the value of cs220_data at the specified cell
return val
```
%% Cell type:markdown id: tags:
#### Print out the lecture number of the 4th student using the `cell` function
%% Cell type:code id: tags:
``` python
cell(..., ...)
```
%% Cell type:markdown id: tags:
#### Print out every student's sleep habits and major using the `cell` function
%% Cell type:code id: tags:
``` python
for i in range(len(cs220_data)):
current_sleep_habit = ...
current_major = ...
print(current_sleep_habit + '\t\t' + current_major)
```
%% Cell type:markdown id: tags:
#### Print out every students' age in 10 years using the `cell` function
This does not quite work. Fix this code:
%% Cell type:code id: tags:
``` python
for i in range(len(cs220_data)):
current_age = cell(i, "Age")
if current_age != None:
print(current_age + 10)
```
%% Cell type:markdown id: tags:
## Example 4: Improve the `cell` function
Improve the `cell` function so it returns the appropriate type. If there is **nothing** in the cell, return `None`.
%% Cell type:code id: tags:
``` python
def cell(row_idx, col_name):
col_idx = cs220_header.index(col_name)
val = cs220_data[row_idx][col_idx]
if ...:
return None
elif ...:
return int(val)
else:
return val
```
%% Cell type:markdown id: tags:
#### Print out every student's sleep habits and major using the `cell` function
%% Cell type:code id: tags:
``` python
for i in range(len(cs220_data)):
current_age = cell(i, "Age")
if current_age != None:
print(current_age + 10)
```
%% Cell type:markdown id: tags:
## Example 5: Get the average age of each lecture
%% Cell type:code id: tags:
``` python
students_lec_001 = []
students_lec_002 = []
students_lec_003 = []
students_lec_004 = []
students_lec_005 = []
for i in range(len(cs220_data)):
current_lec = ...
current_age = ...
if ...: # TODO: check for missing data
continue
if current_lec == "LEC001":
students_lec_001.append(current_age)
elif current_lec == "LEC002":
students_lec_002.append(current_age)
elif current_lec == "LEC003":
students_lec_003.append(current_age)
elif current_lec == "LEC004":
students_lec_004.append(current_age)
elif current_lec == "LEC005":
students_lec_005.append(current_age)
print("Average age for LEC001 is", round(sum(students_lec_001) / len(students_lec_001), 2))
print("Average age for LEC002 is", round(sum(students_lec_002) / len(students_lec_002), 2))
print("Average age for LEC003 is", round(sum(students_lec_003) / len(students_lec_003), 2))
print("Average age for LEC004 is", round(sum(students_lec_004) / len(students_lec_004), 2))
print("Average age for LEC005 is", round(sum(students_lec_005) / len(students_lec_005), 2))
```
%% Cell type:markdown id: tags:
#### Bonus challenge: Can you do this with a little less hardcoding?
%% Cell type:code id: tags:
``` python
lectures_of_ages = [
[],
[],
[],
[],
[]
]
for i in range(len(cs220_data)):
current_lec = int(cell(i, "Lecture")[-1]) - 1 # Will be a number 0 - 4
current_age = cell(i, "Age")
if current_age != None:
lectures_of_ages[current_lec].append(current_age)
# TODO: Print the average ages
```
%% Cell type:markdown id: tags:
## Example 6: What are the unique ages for each lecture?
%% Cell type:code id: tags:
``` python
for i in range(len(lectures_of_ages)):
ages_of_lecture_i = ...
unique_ages = ...
print(unique_ages)
```
%% Cell type:markdown id: tags:
## You try!
%% Cell type:markdown id: tags:
Complete the challenges below. First try completing the problem directly using the list of lists (e.g. double indexing `[][]`), then try using the `cell` function!
%% Cell type:markdown id: tags:
## Exercise 1: Of all runners, how many are procrastinators?
%% Cell type:markdown id: tags:
## Exercise 2: What percentage of 18-year-olds have their major declared as "Other"?
%% Cell type:markdown id: tags:
## Exercise 3: Does the oldest basil/spinach-loving Business major prefer cats, dogs, or neither?
This diff is collapsed.
%% Cell type:markdown id: tags:
# Lecture 16 Worksheet Solutions
You should do the worksheet by hand, then check your work.
%% Cell type:markdown id: tags:
## Problem 1:
%% Cell type:code id: tags:
``` python
nums = [100, 2, 3, 40, 99]
words = ["three", "two", "one"]
```
%% Cell type:markdown id: tags:
#### Solutions:
%% Cell type:code id: tags:
``` python
print(nums[-1])
print(nums[1:3])
print(words[1])
print(words[1][1])
print(words[1][-2] * nums[2])
print()
print(words.index("two"))
print(nums[words.index("two")])
print(nums[:1] + words[:1])
print(",".join(words))
print((",".join(words))[4:7])
```
%% Output
99
[2, 3]
two
w
www
1
2
[100, 'three']
three,two,one
e,t
%% Cell type:markdown id: tags:
## Problem 2:
%% Cell type:code id: tags:
``` python
rows = [["x", "y","name"], [3,4,"Alice"], [9,1,"Bob"], [-3,4,"Cindy"]]
header = rows[0]
data = rows[1:]
X = 0
Y = 1
NAME = 2
```
%% Cell type:markdown id: tags:
#### Solutions:
%% Cell type:code id: tags:
``` python
print(len(rows))
print(len(data))
print(len(header))
print(rows[1][-1])
print(data[1][-1])
print()
print(header.index("name"))
print(data[-1][header.index("name")])
print((data[0][X] + data[1][X] + data[2][X]) / 3)
print((data[-1][X] ** 2 + data[-1][Y] ** 2) ** 0.5)
print(min(data[0][NAME], data[1][NAME], data[2][NAME]))
```
%% Output
4
3
3
Alice
Bob
2
Cindy
3.0
5.0
Alice
%% Cell type:markdown id: tags:
## Problem 3:
%% Cell type:code id: tags:
``` python
rows = [ ["Food Science", "24000", "0.049188446", "62000"],
["CS", "783000", "0.049518657", "78000"],
["Microbiology", "70000", "0.050880749", "60000"],
["Math", "433000", "0.05293608", "66000"] ]
hd = ["major", "students", "unemployed", "salary"]
```
%% Cell type:markdown id: tags:
#### Solutions:
%% Cell type:code id: tags:
``` python
print(rows[1][0])
print(rows[3][hd.index("students")])
print(len(hd) == len(rows[1]))
print(rows[0][1] + rows[2][1])
```
%% Output
CS
433000
True
2400070000
%% Cell type:markdown id: tags:
## Problem 4:
%% Cell type:code id: tags:
``` python
rows = [ ["city", "state", "y14", "y15"],
["Chicago", "Illinois", "411", "478"],
["Milwaukee", "Wisconsin", "90", "145"],
["Detroit", "Michigan", "298", "295"] ]
hd = rows[0]
rows = rows[1:] # this removes the header and stores the result in rows
```
%% Cell type:markdown id: tags:
#### Solutions:
%% Cell type:code id: tags:
``` python
print(rows[0][hd.index("city")])
print(rows[0][hd.index("y14")])
print(rows[2][hd.index("y14")] < rows[2][hd.index("y15")])
print(", ".join(rows[-1][:2]))
```
%% Output
Chicago
411
False
Detroit, Michigan
This diff is collapsed.
restaurant_id,name,x_coord,y_coord
MCD_1,McDonalds,-3,-3
EIN_1,Einsteins Bagels,1,3
STA_1,Starbucks,0,1
MCD_2,McDonalds,2,0
GRE_1,Greenbush Donuts,0,-3
STA_2,Starbucks,-2,1
PAN_1,Panda Express,,
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment