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

Louis lec 15 added

parent 77f6450f
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
# Warmup 0: Write a program that manages a grocery list!
# A: Add an item. Ask the user to add an item to their list.
# D: Delete an item. Ask the user what to remove from their list.
# P: Print the grocery list.
# Q: Quit.
my_groceries = []
while True:
choice = input("What do you want to do? (A, D, P, Q): ")
# TODO Improve handling of errors (e.g. user tries to remove a food that doesn't exist)
choice = choice.upper()
if choice == 'A':
add_food = input("What do you want to add? ")
my_groceries.append(add_food)
elif choice == 'D':
rem_food = input("What do you want to remove? ")
while not rem_food in my_groceries:
print('{} is not in your grocery list!'.format(rem_food))
rem_food = input("What do you want to remove? ")
my_groceries.remove(rem_food)
elif choice == 'P':
print("Your groceries are ", my_groceries)
elif choice == 'Q':
break
else:
print('I don\'t understand. Please try again!')
print('Thanks for shopping!')
```
%% Cell type:code id: tags:
``` python
# Warmup #1: Profanity Filter
def profanity_filter(sentence, bad_words):
''' replaces all instances of any word in bad_words with \n
a word with the first letter and then @s and the same length'''
sentence_split = sentence.split(" ")
cleaned_sentence = []
for word in sentence_split:
# TODO We need to improve this! Extra practice!
if word in bad_words:
cleaned_word = word[0] + "@" * (len(word) - 1)
cleaned_sentence.append(cleaned_word)
else:
cleaned_sentence.append(word)
# all done cleaning, now join and return
return " ".join(cleaned_sentence)
bad_word_list = ["darn", "heck", "crud", "exam"]
print(profanity_filter("I unplugged that darn Alexa", bad_word_list))
print(profanity_filter("What the heck was my boss thinking?", bad_word_list))
print(profanity_filter("He is full of crud?", bad_word_list)) # TODO On your own, how would you handle this?
```
%% Cell type:code id: tags:
``` python
# Warmup #2: Take a look at these list methods
# https://www.w3schools.com/python/python_ref_list.asp
dairy = ["milk", "ice cream", "cheese", "yogurt" ]
#use the .index() method to get the index of "ice cream"
dairy.index("ICE cream".lower())
```
%% Cell type:code id: tags:
``` python
# Warmup #3: Because a list is a sequence, we can use the 'in' operator
food_shelf = ["peanut butter", "milk", "bread", "cheese", "YOGURT"]
for item in food_shelf:
if item.lower() in dairy:
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:code id: tags:
``` python
# Now lets store the contents of the CSV file into Python lists
# copied from https://automatetheboringstuff.com/chapter14/
import csv
def process_csv(filename):
# open the file, its a text file utf-8
exampleFile = open(filename, encoding="utf-8")
# prepare it for reading as a CSV object
exampleReader = csv.reader(exampleFile)
# use the built-in list function to convert this into a list of lists
exampleData = list(exampleReader)
# close the file to tidy up our workspace
exampleFile.close()
# return the list of lists
return exampleData
```
%% Cell type:code id: tags:
``` python
# Call the process_csv function and store the list of lists in cs220_csv
cs220_csv = process_csv('cs220_survey_data.csv')
cs220_csv
```
%% Cell type:code id: tags:
``` python
# Store the header row into cs220_header
cs220_header = cs220_csv[0]
cs220_header
```
%% Cell type:code id: tags:
``` python
# Store all of the data rows into cs220_data
cs220_data = cs220_csv[1:]
cs220_data
```
%% Cell type:markdown id: tags:
## CSVs as a List of Lists
%% Cell type:code id: tags:
``` python
# Print out the lecture number of the 4th student...by hardcoding its row and column....
cs220_data[3][1]
```
%% Cell type:code id: tags:
``` python
# Print out the sleeping habit for the 2nd student...by hardcoding its row and column....
cs220_data[1][-3]
```
%% Cell type:code id: tags:
``` python
# Print out how many students completed the survey.
len(cs220_data)
```
%% Cell type:code id: tags:
``` python
# Print out every student's sleep habits and major
for i in range(len(cs220_data)):
current_sleep_habit = cs220_data[i][12]
current_major = cs220_data[i][3]
print(current_sleep_habit + '\t\t' + current_major)
```
%% Cell type:code id: tags:
``` python
# FIX: Print out every students' age in 10 years.
for i in range(len(cs220_data)):
current_age = cs220_data[i][2]
if current_age == "":
continue
current_age = int(current_age)
print(current_age + 10)
```
%% Cell type:markdown id: tags:
## It would be nice to have a helper function!
Let's introduce `cell`
%% Cell type:code id: tags:
``` python
# Remember creating cs220_header?
cs220_header
```
%% Cell type:code id: tags:
``` python
# Get the column index of "Pizza topping"
cs220_header.index("Pizza topping")
```
%% Cell type:code id: tags:
``` python
# We want to invoke something like...
# cell(24, "Pet owner")
# cell(63, "Zip Code")
def cell(row_idx, col_name):
col_idx = cs220_header.index(col_name) # get the index of col_name
val = cs220_data[row_idx][col_idx] # get the value of cs220_data at the specified cell
return val
```
%% Cell type:code id: tags:
``` python
# Print out the lecture number of the 4th student... using the cell function
cell(3, "Lecture")
```
%% Cell type:code id: tags:
``` python
# Print out the fav pizza topping of the 8th student... using the cell function
cell(7, "Pizza topping")
```
%% Cell type:code id: tags:
``` python
# Print out every student's sleep habits and major using the cell function
for i in range(len(cs220_data)):
current_sleep_habit = cell(i, "Sleep habit")
current_major = cell(i, "Primary major")
print(current_sleep_habit + '\t\t' + current_major)
```
%% Cell type:code id: tags:
``` python
# Print out every students' age in 10 years using the cell function
# ... that didn't really help us here!
for i in range(len(cs220_data)):
current_age = cell(i, "Age")
print(type(current_age))
if current_age != None:
print(current_age + 10)
```
%% Cell type:code id: tags:
``` python
# Improve the cell function so it returns the appropriate type.
# If there is nothing in the cell, return None
def cell(row_idx, col_name):
col_idx = cs220_header.index(col_name)
val = cs220_data[row_idx][col_idx]
if val == "":
return None
elif col_name == "Age":
return int(val)
else:
return val
# Yours to do... can you handle Zip Code, Latitude, and Longitude?
```
%% Cell type:code id: tags:
``` python
# Print out every students' age in 10 years using the cell function
# ... much better!
for i in range(len(cs220_data)):
current_age = cell(i, "Age")
if current_age != None:
print(current_age + 10)
```
%% Cell type:code id: tags:
``` python
# Get the average age of each lecture...
students_lec_001 = []
students_lec_002 = []
students_lec_003 = []
students_lec_004 = []
students_lec_005 = []
for i in range(len(cs220_data)):
current_lec = cell(i, "Lecture")
current_age = cell(i, "Age")
if current_age != None:
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 {} is {}".format("LEC001", round(sum(students_lec_001) / len(students_lec_001), 2)))
print("Average age for {} is {}".format("LEC002", round(sum(students_lec_002) / len(students_lec_002), 2)))
print("Average age for {} is {}".format("LEC003", round(sum(students_lec_003) / len(students_lec_003), 2)))
print("Average age for {} is {}".format("LEC004", round(sum(students_lec_004) / len(students_lec_004), 2)))
print("Average age for {} is {}".format("LEC005", round(sum(students_lec_005) / len(students_lec_005), 2)))
```
%% Cell type:code id: tags:
``` python
# Get the average age of each lecture... With less hardcoding!
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)
for i in range(len(lectures_of_ages)):
curr_lec = lectures_of_ages[i]
total_age = sum(curr_lec)
total_students = len(curr_lec)
print("Average age for {} is {}".format("LEC00" + str(i + 1), round(total_age / total_students, 2)))
```
%% Cell type:code id: tags:
``` python
# What are the unique ages for each lecture?
for i in range(len(lectures_of_ages)):
unique_ages = list(set(lectures_of_ages[i]))
print(sorted(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:code id: tags:
``` python
# Of all runners, how many are procrastinators? [][]
count_runners = 0
count_running_procrastinators = 0
for i in range(len(cs220_data)):
current_procrastinator = cs220_data[i][cs220_header.index('Procrastinator')]
current_runner = cs220_data[i][cs220_header.index('Runner')]
if current_runner == "Yes":
count_runners += 1
if current_procrastinator == "Yes":
count_running_procrastinators += 1
print('There are {} runners, of which {} are procrastinators.'.format(count_runners, count_running_procrastinators))
```
%% Cell type:code id: tags:
``` python
# Of all runners, how many are procrastinators? [][]
count_runners = 0
count_running_procrastinators = 0
for i in range(len(cs220_data)):
current_procrastinator = cell(i, 'Procrastinator')
current_runner = cell(i, 'Runner')
if current_runner == "Yes":
count_runners += 1
if current_procrastinator == "Yes":
count_running_procrastinators += 1
print('There are {} runners, of which {} are procrastinators.'.format(count_runners, count_running_procrastinators))
```
%% Cell type:code id: tags:
``` python
# What percentage of 18 to 20-year-olds have their major declared as "Other"? [][]
# Could alternatively have 2 count variables.
all_18_to_20 = []
all_18_to_20_and_other = []
for i in range(len(cs220_data)):
current_age = cs220_data[i][cs220_header.index('Age')]
current_major = cs220_data[i][cs220_header.index('Primary major')]
if current_age == "":
continue
current_age = int(current_age)
if 18 <= current_age <= 20:
all_18_to_20.append(i)
if current_major.startswith("Other"):
all_18_to_20_and_other.append(i)
(len(all_18_to_20_and_other) / len(all_18_to_20)) * 100
```
%% Cell type:code id: tags:
``` python
# What percentage of 18 to 20-year-olds have their major declared as "Other"? cell
# Could alternatively have 2 count variables.
all_18_to_20 = []
all_18_to_20_and_other = []
for i in range(len(cs220_data)):
current_age = cell(i, 'Age')
current_major = cell(i, 'Primary major')
if current_age == None:
continue
current_age = int(current_age)
if 18 <= current_age <= 20:
all_18_to_20.append(i)
if current_major.startswith("Other"):
all_18_to_20_and_other.append(i)
(len(all_18_to_20_and_other) / len(all_18_to_20)) * 100
```
%% Cell type:code id: tags:
``` python
# Does the oldest basil/spinach-loving Business major prefer cats, dogs, or neither? [][]
oldest_idx = None
oldest_age = None
for i in range(len(cs220_data)):
current_age = cs220_data[i][cs220_header.index('Age')]
current_pizza = cs220_data[i][cs220_header.index('Pizza topping')]
current_major = cs220_data[i][cs220_header.index('Primary major')]
if current_age == "":
continue
current_age = int(current_age)
if current_pizza == "basil/spinach" and current_major.startswith("Business"):
if oldest_idx == None or current_age > oldest_age:
oldest_age = current_age
oldest_idx = i
print(cs220_data[oldest_idx][cs220_header.index('Cats or dogs')])
```
%% Cell type:code id: tags:
``` python
# Does the oldest basil/spinach-loving Business major prefer cats, dogs, or neither? cell
oldest_idx = None
oldest_age = None
for i in range(len(cs220_data)):
current_age = cell(i, "Age")
current_pizza = cell(i, "Pizza topping")
current_major = cell(i, "Primary major")
if current_age == None:
continue
current_age = int(current_age)
if current_pizza == "basil/spinach" and current_major.startswith("Business"):
if oldest_idx == None or current_age > oldest_age:
oldest_age = current_age
oldest_idx = i
print(cell(oldest_idx, "Cats or dogs"))
```
%% Cell type:code id: tags:
``` python
```
This diff is collapsed.
This diff is collapsed.
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