Skip to content
Snippets Groups Projects
Commit f08c4664 authored by Andy Kuemmel's avatar Andy Kuemmel
Browse files

Update...

Update f22/andy_lec_notes/lec12_Oct03_IterationAlgorithms/lec12_Oct03_IterationAlgorithms_completed.ipynb
parent 438d8ad5
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:6a76ef95 tags:
# Iteration Algorithms
Thanks to CS 220 campus students for their data and to Meena for putting together the project.py file
%% Cell type:markdown id:103da70b tags:
## Learning Objectives
- Iterate through a dataset using for idx in range(project.count())
- Compute the frequency of data that meets a certain criteria
- Find the maximum or minimum value of a numeric column in a dataset
- Handle missing numeric values when computing a maximum / minimum
- Use the index of a maximum or minimum to access other information about that data item
- Use break and continue in for loops when processing a dataset
%% Cell type:code id:28961628 tags:
``` python
import project
```
%% Cell type:code id:d1dca7ae tags:
``` python
# TODO: inspect the project module's documentation by using help
help(project)
```
%% Output
Help on module project:
NAME
project
FUNCTIONS
__init__()
count()
This function will return the number of records in the dataset
get_age(idx)
get_age(idx) returns the age of the student in row idx
get_latitude(idx)
get_latitude(idx) returns the latitude of the student's favourite place in row idx
get_lecture(idx)
get_lecture(idx) returns the lecture of the student in row idx
get_longitude(idx)
get_longitude(idx) returns the longitude of the student's favourite place in row idx
get_major(idx)
get_major(idx) returns the major of the student in row idx
get_pet_owner(idx)
get_pet_owner(idx) returns the pet preference of student in row idx
get_piazza_topping(idx)
get_piazza_topping(idx) returns the preferred pizza toppings of the student in row idx
get_procrastinator(idx)
get_procrastinator(idx) returns whether student in row idx is a procrastinator
get_runner(idx)
get_runner(idx) returns whether student in row idx is a runner
get_sleep_habit(idx)
get_sleep_habit(idx) returns the sleep habit of the student in row idx
get_zip_code(idx)
get_zip_code(idx) returns the residential zip code of the student in row idx
DATA
__student__ = [{'Age': '22', 'Latitude': '43.073051', 'Lecture': 'LEC0...
FILE
/Users/andrewkuemmel/cs220-f22/lectures/lec12_Oct03_IterationAlgorithms/project.py
%% Cell type:markdown id:7fb78f6b tags:
### How many students does the dataset have?
%% Cell type:code id:d67a080f tags:
``` python
project.count()
```
%% Output
992
%% Cell type:markdown id:3c97d494 tags:
### What is the age of the student at index 10?
%% Cell type:code id:bde8dc35 tags:
``` python
id_10_age = project.get_age(10)
id_10_age
```
%% Output
'21'
%% Cell type:code id:b0f87a2c tags:
``` python
# TODO: what is the return value type of the get_age function?
type(id_10_age)
# in a CSV file, all data stored as a string
```
%% Output
str
%% Cell type:markdown id:37898141 tags:
### What is the lecture number of the student at index 20?
%% Cell type:code id:ba993090 tags:
``` python
project.get_lecture(20)
```
%% Output
'LEC001'
%% Cell type:markdown id:6e3e3c0a tags:
### What are the age and pizza choices of the first 10 students?
%% Cell type:code id:46ec7632 tags:
``` python
for i in range(10):
print(project.get_age(i), project.get_piazza_topping(i))
```
%% Output
22 none (just cheese)
none (just cheese)
18 none (just cheese)
none (just cheese)
none (just cheese)
18 none (just cheese)
none (just cheese)
18 pineapple
18 none (just cheese)
18 pepperoni
%% Cell type:markdown id:af3d9d8c tags:
### How many current lecture (example: LEC001) students are in the dataset?
%% Cell type:code id:e024c488 tags:
``` python
frequency = 0
for i in range(project.count()):
if project.get_lecture(i) == "LEC001":
frequency += 1
print(frequency)
```
%% Output
195
%% Cell type:markdown id:b9ff6434 tags:
### What is the age of the oldest student in the course?
%% Cell type:code id:38bd778a tags:
``` python
# find the maximum
# check for missing data
# check for data out of range
max_age = 0
for i in range(project.count()):
if project.get_age(i) == '':
continue
age = int(project.get_age(i))
if age < 0 or age > 100:
continue
if age > max_age:
max_age = age
print(max_age)
```
%% Output
53
%% Cell type:markdown id:b40a32fb tags:
### What is the age of the youngest student in current lecture (example: LEC001)?
- use similar algorithm as above question
%% Cell type:code id:ea77e0cd tags:
``` python
min_age = int(project.get_age(0) ) #assume first student is youngest,and has age
for i in range(project.count()):
if project.get_lecture != "LEC001":
continue
if project.get_age(i) == '':
continue
age = int(project.get_age(i))
if age < 0 or age > 100:
continue
if age < min_age:
min_age = age
print(min_age)
```
%% Output
22
%% Cell type:markdown id:296c2a49 tags:
### What is the average age of students enrolled in CS220 / CS319?
%% Cell type:code id:8b7c8367 tags:
``` python
total_age = 0
num_students_with_age = 0
for i in range(project.count()):
if project.get_age(i) == '':
continue
age = int(project.get_age(i))
if age < 0 or age > 100:
continue
total_age += age
num_students_with_age += 1
average_age = total_age / num_students_with_age
average_age
```
%% Output
19.608180839612487
%% Cell type:markdown id:48f1c791 tags:
### What major is the youngest student in current lecture (example: LEC001) planning to declare?
- now, we need to find some other detail about the youngest student
- often, you'll have to keep track of ID of the max or min, so that you can retrive other details about that data entry
%% Cell type:code id:a524873b tags:
``` python
# we will do this in lecture on Wednesday
```
%% Output
17
%% Cell type:markdown id:5294702a tags:
### Considering current lecture students (example: LEC001), what is the age of the first student residing at zip code 53715?
%% Cell type:code id:fada2a40 tags:
``` python
for i in range(project.count()):
# put a print statement here
if project.get_lecture(i) == "LEC002" and project.get_zip_code(i) == "53715":
print(project.get_age(i), type(project.get_age(i)))
break
```
%% Output
19 <class 'str'>
%% Cell type:code id:772f7a58 tags:
``` python
help(project)
```
%% Output
Help on module project:
NAME
project
FUNCTIONS
__init__()
count()
This function will return the number of records in the dataset
get_age(idx)
get_age(idx) returns the age of the student in row idx
get_latitude(idx)
get_latitude(idx) returns the latitude of the student's favourite place in row idx
get_lecture(idx)
get_lecture(idx) returns the lecture of the student in row idx
get_longitude(idx)
get_longitude(idx) returns the longitude of the student's favourite place in row idx
get_major(idx)
get_major(idx) returns the major of the student in row idx
get_pet_owner(idx)
get_pet_owner(idx) returns the pet preference of student in row idx
get_piazza_topping(idx)
get_piazza_topping(idx) returns the preferred pizza toppings of the student in row idx
get_procrastinator(idx)
get_procrastinator(idx) returns whether student in row idx is a procrastinator
get_runner(idx)
get_runner(idx) returns whether student in row idx is a runner
get_sleep_habit(idx)
get_sleep_habit(idx) returns the sleep habit of the student in row idx
get_zip_code(idx)
get_zip_code(idx) returns the residential zip code of the student in row idx
DATA
__student__ = [{'Age': '22', 'Latitude': '43.073051', 'Lecture': 'LEC0...
FILE
/Users/andrewkuemmel/cs220-f22/lectures/lec12_Oct03_IterationAlgorithms/project.py
%% Cell type:markdown id:68793d99 tags:
## Self-practice
%% Cell type:markdown id:2eeed867 tags:
### How many current lecture (example: LEC001) students are runners?
%% Cell type:markdown id:1ea57e12 tags:
### How many current lecture (example: LEC001) students are procrastinators?
%% Cell type:markdown id:cf0ac7c8 tags:
### How many current lecture (example: LEC001) students own or have owned a pet?
%% Cell type:markdown id:ffd5e10f tags:
### What sleep habit does the youngest student in current lecture (example: LEC001) have?
- try to solve this from scratch, instead of copy-pasting code to find mimimum age
%% Cell type:markdown id:f255b95a tags:
### What sleep habit does the oldest student in current lecture (example: LEC001) have?
- try to solve this from scratch, instead of copy-pasting code to find mimimum age
%% Cell type:markdown id:70a8ac57 tags:
### What is the minimum latitude (& corresponding longitude) of a student's place of interest?
- What place is this -> try to enter the lat, long on Google maps?
%% Cell type:markdown id:581ea197 tags:
### What is the maximum latitude (& corresponding longitude) of a student's place of interest?
- What place is this -> try to enter the lat, long on Google maps?
%% Cell type:code id:fcee0994 tags:
``` python
```
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