Skip to content
Snippets Groups Projects
Commit e90102d0 authored by Ashwin Maran's avatar Ashwin Maran
Browse files

Upload New File

parent e6bb6e92
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
## Warmup 1: Jupyter Notebook stuck in `[*]`?
Press the Stop button! It's probably waiting for input. Otherwise, **restart** the kernel! (and run the cells)
%% Cell type:code id: tags:
``` python
x = int(input("Type in a number: "))
print("The number is " + str(x), x + 1, x + 2, x + 3, x + 4, sep=" then ", end="!\n")
```
%% Output
Type in a number: 220
The number is 220 then 221 then 222 then 223 then 224!
%% Cell type:markdown id: tags:
## Warmup 2: Write a function that prints the factorial of parameter num
%% Cell type:code id: tags:
``` python
def do_factorial(num):
factorial = 1
while num != 1:
factorial = factorial * num
num -= 1
return factorial
```
%% Cell type:code id: tags:
``` python
print(do_factorial(1))
print(do_factorial(2))
print(do_factorial(3))
print(do_factorial(4))
```
%% Output
1
2
6
24
%% Cell type:markdown id: tags:
## Warmup 3: Complete the code to print a treasure map
The map should be a grid of size `width` and `height` with `symbol` everywhere, expect at the location: `treasure_row`, `treasure_col`, where an `'X'` is placed.
%% Cell type:code id: tags:
``` python
def print_treasure_map(symbol='-', height=4, width=4, treasure_row=2, treasure_col=2):
i = 0
while i < height: # TODO: Complete the loop condition for printing out each row.
j = 0
while j < width:
if i == treasure_row and j == treasure_col: # TODO: Complete the if condition for checking if we print an X or the symbol.
print('X', end="")
else:
print(symbol, end="")
j += 1 # TODO: Complete the statement so we do not run into an infinite loop.
print()
i += 1
print_treasure_map()
# print_treasure_map(width=10, height=10)
# print_treasure_map('#', 7, 4, treasure_row=0, treasure_col=1)
# print_treasure_map('.', 5, 8, 3, 6)
```
%% Output
----
----
--X-
----
%% Cell type:markdown id: tags:
# CS220: Lecture 12
## Learning Objectives
After this lecture you will be able to...
- 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
- Use break and continue in for loops when processing 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
%% Cell type:code id: tags:
``` python
import project
```
%% Cell type:code id: 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_cats_or_dogs(idx)
get_cats_or_dogs(idx) returns whether student in row idx likes cats or dogs
get_lecture(idx)
get_lecture(idx) returns the lecture of the student in row idx
get_other_majors(idx)
get_other_majors(idx) returns the secondary major of the student in row idx
get_pizza_topping(idx)
get_pizza_topping(idx) returns the preferred pizza toppings of the student in row idx
get_primary_major(idx)
get_primary_major(idx) returns the primary major 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_section(idx)
get_lecture(idx) returns the section of the student in row idx
get_sleep_habit(idx)
get_sleep_habit(idx) returns the sleep habit of the student in row idx
get_song(idx)
get_procrastinator(idx) returns the student in row idx favorite song
get_zip_code(idx)
get_zip_code(idx) returns the residential zip code of the student in row idx
DATA
__student__ = [{'Age': '19', 'Cats or dogs': 'cat', 'Latitude': '44.25...
FILE
c:\college\uw-madison\spring 2024\cs220\lecture code\12_iteration_practice\project.py
%% Cell type:code id: tags:
``` python
# Get the total # of responses
project.count()
```
%% Output
1019
%% Cell type:code id: tags:
``` python
# Get the first student's primary major.
# With indices, we always start from 0!
project.get_primary_major(0)
```
%% Output
'Other (please provide details below).'
%% Cell type:markdown id: tags:
## Example 1: Print and Break
%% Cell type:code id: tags:
``` python
for i in range(project.count()):
print (i, project.get_primary_major(i))
if i == 10:
break
```
%% Output
0 Other (please provide details below).
1 Engineering: Biomedical
2 Computer Science
3 Engineering: Other
4 Data Science
5 Engineering: Biomedical
6 Mathematics/AMEP
7 Engineering: Mechanical
8 Other (please provide details below).
9 Other (please provide details below).
10 Business: Information Systems
%% Cell type:code id: tags:
``` python
# TODO: Write the same code as above using a while loop!
i = 0
while i <= 10:
print(i, project.get_primary_major(i))
i += 1
```
%% Output
0 Other (please provide details below).
1 Engineering: Biomedical
2 Computer Science
3 Engineering: Other
4 Data Science
5 Engineering: Biomedical
6 Mathematics/AMEP
7 Engineering: Mechanical
8 Other (please provide details below).
9 Other (please provide details below).
10 Business: Information Systems
%% Cell type:markdown id: tags:
## Example 2: How many students are not in Computer Science?
%% Cell type:code id: tags:
``` python
non_cs = 0
for i in range(project.count()):
major = project.get_primary_major(i)
if major == "Computer Science":
continue
non_cs += 1
print(non_cs, "out of", project.count(), "are non-cs!")
```
%% Output
942 out of 1019 are non-cs!
%% Cell type:markdown id: tags:
## Example 3: How many students are in Data Science or Statistics?
%% Cell type:code id: tags:
``` python
ds_or_stats = 0
for i in range(project.count()):
major = project.get_primary_major(i)
if major == "Data Science" or major == "Statistics":
ds_or_stats += 1
print(ds_or_stats, "out of", project.count(), "are Data Science or Statistics majors!")
# BONUS: Can you express this as a percentage?
print(str(round(ds_or_stats/project.count()*100, 2)) + "% of students are Data Science or Statistics majors!")
# BONUS+: ...rounded to 2 decimal places?
```
%% Output
200 out of 1019 are Data Science or Statistics majors!
19.63% of students are Data Science or Statistics majors!
%% Cell type:markdown id: tags:
## Example 4: How many early birds are there below the age of 21?
%% Cell type:code id: tags:
``` python
early_birds = 0
total_students = 0
for i in range(project.count()):
sleep_habit = project.get_sleep_habit(i)
age = project.get_age(i)
if age != "":
age = int(age)
if age < 21: # TODO Complete this condition!
total_students += 1
if sleep_habit == "early bird":
early_birds += 1
print("There are", early_birds, "early birds below the age of 21.")
print("There are", total_students, "total students below the age of 21.")
```
%% Output
There are 190 early birds below the age of 21.
There are 791 total students below the age of 21.
%% Cell type:markdown id: tags:
## Example 5: What percentage of 20-year-olds are early birds?
**Bonus:** How can we generalize our code to 'What percentage of x-year-olds are early birds?'
%% Cell type:code id: tags:
``` python
def print_early_birds(target_age):
early_birds = 0
total_students = 0
for i in range(project.count()):
sleep_habit = project.get_sleep_habit(i)
age = project.get_age(i)
if age != "":
age = int(age)
if age == target_age:
total_students += 1
if sleep_habit == "early bird":
early_birds += 1
print("There are", early_birds, "early birds that are 20.")
print("There are", total_students, "total students that are 20.")
per_early = round((early_birds / total_students) * 100, 2)
print(str(per_early) + "% of " + str(target_age) + " year olds are early birds")
print_early_birds(20)
```
%% Output
There are 47 early birds that are 20.
There are 165 total students that are 20.
28.48% of 20 year olds are early birds
%% Cell type:markdown id: tags:
## Example 6: What is the age of the oldest student?
%% Cell type:code id: tags:
``` python
# Python reserved keyword `max` should not be used as a variable name
max_age = 0
for i in range(project.count()):
if project.get_age(i) == "":
continue
student_age = int(project.get_age(i))
if student_age != "" and student_age > max_age:
max_age = student_age
print("The oldest student is", max_age)
```
%% Output
The oldest student is 40
%% Cell type:markdown id: tags:
## Example 7: What is the age of the youngest student?
%% Cell type:code id: tags:
``` python
min_age = 100
for i in range(project.count()):
if project.get_age(i) == "":
continue
student_age = int(project.get_age(i))
if student_age != "" and student_age < min_age:
min_age = student_age
print("The youngest student is", min_age)
```
%% Output
The youngest student is 17
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