Skip to content
Snippets Groups Projects
Commit 6bbf74a2 authored by msyamkumar's avatar msyamkumar
Browse files

Adding a lat/long question

parent cda9a0f5
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:72348536 tags: %% Cell type:markdown id:72348536 tags:
# List Practice # List Practice
%% Cell type:code id:2bf3c996 tags: %% Cell type:code id:2bf3c996 tags:
``` python ``` python
import csv import csv
``` ```
%% Cell type:markdown id:b34b84ae tags: %% Cell type:markdown id:b34b84ae tags:
### Warmup 1: min / max ### Warmup 1: min / max
%% Cell type:code id:b89c41e1 tags: %% Cell type:code id:b89c41e1 tags:
``` python ``` python
some_list = [45, -4, 66, 220, 10] some_list = [45, -4, 66, 220, 10]
min_val = None min_val = None
for val in some_list: for val in some_list:
if min_val == None or val < min_val: if min_val == None or val < min_val:
min_val = val min_val = val
print(min_val) print(min_val)
max_val = None max_val = None
for val in some_list: for val in some_list:
if max_val == None or val > max_val: if max_val == None or val > max_val:
max_val = val max_val = val
print(max_val) print(max_val)
``` ```
%% Output %% Output
-4 -4
220 220
%% Cell type:markdown id:59a689b1 tags: %% Cell type:markdown id:59a689b1 tags:
### Warmup 2: median ### Warmup 2: median
%% Cell type:code id:2fd5e101 tags: %% Cell type:code id:2fd5e101 tags:
``` python ``` python
def median(some_items): def median(some_items):
""" """
Returns median of a list passed as argument Returns median of a list passed as argument
""" """
some_items.sort() some_items.sort()
n = len(some_items) n = len(some_items)
if n % 2 == 1: if n % 2 == 1:
return some_items[n // 2] return some_items[n // 2]
else: else:
first_middle = some_items[n//2 - 1] first_middle = some_items[n//2 - 1]
second_middle = some_items[n // 2] second_middle = some_items[n // 2]
median = (first_middle + second_middle) / 2 median = (first_middle + second_middle) / 2
return median return median
nums = [5, 4, 3, 2, 1] nums = [5, 4, 3, 2, 1]
print("Median of", nums, "is" , median(nums)) print("Median of", nums, "is" , median(nums))
nums = [6, 5, 4, 3, 2, 1] nums = [6, 5, 4, 3, 2, 1]
print("Median of", nums, "is" , median(nums)) print("Median of", nums, "is" , median(nums))
``` ```
%% Output %% Output
Median of [1, 2, 3, 4, 5] is 3 Median of [1, 2, 3, 4, 5] is 3
Median of [1, 2, 3, 4, 5, 6] is 3.5 Median of [1, 2, 3, 4, 5, 6] is 3.5
%% Cell type:code id:cf14bf7f tags: %% Cell type:code id:cf14bf7f tags:
``` python ``` python
vals = ["A", "C", "B"] vals = ["A", "C", "B"]
print("Median of", nums, "is" , median(vals)) print("Median of", nums, "is" , median(vals))
vals = ["A", "C", "B", "D"] vals = ["A", "C", "B", "D"]
# print("Median of", nums, "is" , median(vals)) # does not work due to TypeError # print("Median of", nums, "is" , median(vals)) # does not work due to TypeError
``` ```
%% Output %% Output
Median of [1, 2, 3, 4, 5, 6] is B Median of [1, 2, 3, 4, 5, 6] is B
%% Cell type:markdown id:bdbf6f75 tags: %% Cell type:markdown id:bdbf6f75 tags:
### set data structure ### set data structure
- **not a sequence** - **not a sequence**
- no ordering of values: - no ordering of values:
- this implies that you can only store unique values within a `set` - this implies that you can only store unique values within a `set`
- very helpful to find unique values stored in a `list` - very helpful to find unique values stored in a `list`
- easy to convert a `list` to `set` and vice-versa. - easy to convert a `list` to `set` and vice-versa.
- ordering is not guaranteed once we use `set` - ordering is not guaranteed once we use `set`
%% Cell type:code id:52e80a6b tags: %% Cell type:code id:52e80a6b tags:
``` python ``` python
some_set = {10, 20, 30, 30, 40, 50, 10} # use a pair of curly braces to define it some_set = {10, 20, 30, 30, 40, 50, 10} # use a pair of curly braces to define it
some_set some_set
``` ```
%% Output %% Output
{10, 20, 30, 40, 50} {10, 20, 30, 40, 50}
%% Cell type:code id:2587184f tags: %% Cell type:code id:2587184f tags:
``` python ``` python
some_list = [10, 20, 30, 30, 40, 50, 10] # Initialize a list containing duplicate numbers some_list = [10, 20, 30, 30, 40, 50, 10] # Initialize a list containing duplicate numbers
# TODO: to find unique values, convert it into a set # TODO: to find unique values, convert it into a set
print(set(some_list)) print(set(some_list))
# TODO: convert the set back into a list # TODO: convert the set back into a list
print(list(set(some_list))) print(list(set(some_list)))
``` ```
%% Output %% Output
{40, 10, 50, 20, 30} {40, 10, 50, 20, 30}
[40, 10, 50, 20, 30] [40, 10, 50, 20, 30]
%% Cell type:markdown id:8a143e1c tags: %% Cell type:markdown id:8a143e1c tags:
Can you index / slice into a `set`? Can you index / slice into a `set`?
%% Cell type:code id:ce43cb95 tags: %% Cell type:code id:ce43cb95 tags:
``` python ``` python
# some_set[1] # doesn't work - remember set has no order # some_set[1] # doesn't work - remember set has no order
``` ```
%% Cell type:code id:cd6473f8 tags: %% Cell type:code id:cd6473f8 tags:
``` python ``` python
# some_set[1:] # doesn't work - remember set has no order # some_set[1:] # doesn't work - remember set has no order
``` ```
%% Cell type:code id:9d936c1c tags: %% Cell type:code id:9d936c1c tags:
``` python ``` python
# inspired by https://automatetheboringstuff.com/2e/chapter16/ # inspired by https://automatetheboringstuff.com/2e/chapter16/
def process_csv(filename): def process_csv(filename):
# open the file, its a text file utf-8 # open the file, its a text file utf-8
example_file = open(filename, encoding="utf-8") example_file = open(filename, encoding="utf-8")
# prepare it for reading as a CSV object # prepare it for reading as a CSV object
example_reader = csv.reader(example_file) example_reader = csv.reader(example_file)
# use the built-in list function to convert this into a list of lists # use the built-in list function to convert this into a list of lists
example_data = list(example_reader) example_data = list(example_reader)
# close the file to tidy up our workspace # close the file to tidy up our workspace
example_file.close() example_file.close()
# return the list of lists # return the list of lists
return example_data return example_data
``` ```
%% Cell type:markdown id:89621c98 tags: %% Cell type:markdown id:89621c98 tags:
### Student Information Survey data ### Student Information Survey data
%% Cell type:code id:d3c252b4 tags: %% Cell type:code id:d3c252b4 tags:
``` python ``` python
# TODO: call the process_csv function and store the list of lists in cs220_csv # TODO: call the process_csv function and store the list of lists in cs220_csv
cs220_csv = process_csv("cs220_survey_data.csv") cs220_csv = process_csv("cs220_survey_data.csv")
``` ```
%% Cell type:code id:5838ae5f tags: %% Cell type:code id:5838ae5f tags:
``` python ``` python
# Store the header row into cs220_header, using indexing # Store the header row into cs220_header, using indexing
cs220_header = cs220_csv[0] cs220_header = cs220_csv[0]
cs220_header cs220_header
``` ```
%% Output %% Output
['Lecture', ['Lecture',
'Age', 'Age',
'Major', 'Major',
'Zip Code', 'Zip Code',
'Latitude', 'Latitude',
'Longitude', 'Longitude',
'Pizza topping', 'Pizza topping',
'Pet preference', 'Pet preference',
'Runner', 'Runner',
'Sleep habit', 'Sleep habit',
'Procrastinator'] 'Procrastinator']
%% Cell type:code id:66fda88d tags: %% Cell type:code id:66fda88d tags:
``` python ``` python
# TODO: Store all of the data rows into cs220_data, using slicing # TODO: Store all of the data rows into cs220_data, using slicing
cs220_data = cs220_csv[1:] cs220_data = cs220_csv[1:]
# TODO: use slicing to display top 3 rows data # TODO: use slicing to display top 3 rows data
cs220_data[:3] cs220_data[:3]
``` ```
%% Output %% Output
[['LEC001', [['LEC001',
'22', '22',
'Engineering: Biomedical', 'Engineering: Biomedical',
'53703', '53703',
'43.073051', '43.073051',
'-89.40123', '-89.40123',
'none (just cheese)', 'none (just cheese)',
'neither', 'neither',
'No', 'No',
'no preference', 'no preference',
'Maybe'], 'Maybe'],
['LEC006', ['LEC006',
'', '',
'Undecided', 'Undecided',
'53706', '53706',
'43.073051', '43.073051',
'-89.40123', '-89.40123',
'none (just cheese)', 'none (just cheese)',
'neither', 'neither',
'No', 'No',
'no preference', 'no preference',
'Maybe'], 'Maybe'],
['LEC004', ['LEC004',
'18', '18',
'Engineering: Industrial', 'Engineering: Industrial',
'53715', '53715',
'43.073051', '43.073051',
'-89.40123', '-89.40123',
'none (just cheese)', 'none (just cheese)',
'neither', 'neither',
'No', 'No',
'no preference', 'no preference',
'Maybe']] 'Maybe']]
%% Cell type:markdown id:4267fe3e tags: %% Cell type:markdown id:4267fe3e tags:
### What `Pizza topping` does the 13th student prefer? ### What `Pizza topping` does the 13th student prefer?
%% Cell type:code id:4b8dbe8b tags: %% Cell type:code id:4b8dbe8b tags:
``` python ``` python
cs220_data[12][6] # bad example: we hard-coded the column index cs220_data[12][6] # bad example: we hard-coded the column index
``` ```
%% Output %% Output
'pineapple' 'pineapple'
%% Cell type:markdown id:4f125240 tags: %% Cell type:markdown id:4f125240 tags:
What if we decided to add a new column before sleeping habit? Your code will no longer work. What if we decided to add a new column before sleeping habit? Your code will no longer work.
Instead of hard-coding column index, you should use `index` method, to lookup column index from the header variable. This will also make your code so much readable. Instead of hard-coding column index, you should use `index` method, to lookup column index from the header variable. This will also make your code so much readable.
%% Cell type:code id:f2e52e06 tags: %% Cell type:code id:f2e52e06 tags:
``` python ``` python
cs220_data[12][cs220_header.index("Pizza topping")] cs220_data[12][cs220_header.index("Pizza topping")]
``` ```
%% Output %% Output
'pineapple' 'pineapple'
%% Cell type:markdown id:5d298a4c tags: %% Cell type:markdown id:5d298a4c tags:
### What is the Lecture of the 4th student? ### What is the Lecture of the 4th student?
%% Cell type:code id:3617b3de tags: %% Cell type:code id:3617b3de tags:
``` python ``` python
cs220_data[3][cs220_header.index("Lecture")] cs220_data[3][cs220_header.index("Lecture")]
``` ```
%% Output %% Output
'LEC005' 'LEC005'
%% Cell type:markdown id:059de363 tags: %% Cell type:markdown id:059de363 tags:
### What **unique** `age` values are included in the dataset? ### What **unique** `age` values are included in the dataset?
%% Cell type:code id:45909f22 tags: %% Cell type:code id:45909f22 tags:
``` python ``` python
ages = [] ages = []
for row in cs220_data: for row in cs220_data:
age = row[cs220_header.index("Age")] age = row[cs220_header.index("Age")]
if age == '': if age == '':
continue continue
age = int(age) age = int(age)
if age < 0 or age > 118: if age < 0 or age > 118:
continue continue
ages.append(age) ages.append(age)
ages = list(set(ages)) ages = list(set(ages))
ages ages
``` ```
%% Output %% Output
[17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 37, 41, 53] [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 37, 41, 53]
%% Cell type:markdown id:8e18663d tags: %% Cell type:markdown id:8e18663d tags:
### cell function ### cell function
- It would be very helpful to define a cell function, which can handle missing data and type conversions - It would be very helpful to define a cell function, which can handle missing data and type conversions
%% Cell type:code id:bba90038 tags: %% Cell type:code id:bba90038 tags:
``` python ``` python
def cell(row_idx, col_name): def cell(row_idx, col_name):
""" """
Returns the data value (cell) corresponding to the row index and Returns the data value (cell) corresponding to the row index and
the column name of a CSV file. the column name of a CSV file.
""" """
# TODO: get the index of col_name # TODO: get the index of col_name
col_idx = cs220_header.index(col_name) col_idx = cs220_header.index(col_name)
# TODO: get the value of cs220_data at the specified cell # TODO: get the value of cs220_data at the specified cell
val = cs220_data[row_idx][col_idx] val = cs220_data[row_idx][col_idx]
# TODO: handle missing values, by returning None # TODO: handle missing values, by returning None
if val == '': if val == '':
return None return None
# TODO: handle type conversions # TODO: handle type conversions
if col_name in ["Age", 'Zip Code',]: if col_name in ["Age", 'Zip Code',]:
return int(val) return int(val)
elif col_name in ['Latitude', 'Longitude']: elif col_name in ['Latitude', 'Longitude']:
return float(val) return float(val)
return val return val
``` ```
%% Cell type:markdown id:b7c8e726 tags: %% Cell type:markdown id:b7c8e726 tags:
### Function `avg_age_per_lecture(lecture)` ### Function `avg_age_per_lecture(lecture)`
%% Cell type:code id:4894d0c7 tags: %% Cell type:code id:4894d0c7 tags:
``` python ``` python
def avg_age_per_lecture(lecture): def avg_age_per_lecture(lecture):
''' '''
avg_age_per_lecture(lecture) returns the average age of avg_age_per_lecture(lecture) returns the average age of
the students in the given `lecture`; if there are no the students in the given `lecture`; if there are no
students in the given `lecture`, it returns `None` students in the given `lecture`, it returns `None`
''' '''
# To compute average you don't need to actually populate a list. # To compute average you don't need to actually populate a list.
# But here a list will come in handy. It will help you with the None return requirement. # But here a list will come in handy. It will help you with the None return requirement.
ages = [] ages = []
for row_idx in range(len(cs220_data)): for row_idx in range(len(cs220_data)):
curr_lecture = cell(row_idx, "Lecture") curr_lecture = cell(row_idx, "Lecture")
if lecture == curr_lecture: if lecture == curr_lecture:
age = cell(row_idx, "Age") age = cell(row_idx, "Age")
if age != None and age > 0 and age <= 118: if age != None and age > 0 and age <= 118:
ages.append(age) ages.append(age)
if len(ages) > 0: if len(ages) > 0:
return sum(ages) / len(ages) return sum(ages) / len(ages)
else: else:
return None return None
``` ```
%% Cell type:code id:f0a05e42 tags: %% Cell type:code id:f0a05e42 tags:
``` python ``` python
avg_age_per_lecture("LEC002") avg_age_per_lecture("LEC002")
``` ```
%% Output %% Output
19.683615819209038 19.683615819209038
%% Cell type:code id:ec9af3da tags: %% Cell type:code id:ec9af3da tags:
``` python ``` python
print(avg_age_per_lecture("LEC007")) print(avg_age_per_lecture("LEC007"))
``` ```
%% Output %% Output
None None
%% Cell type:markdown id:94548bf4 tags: %% Cell type:markdown id:94548bf4 tags:
### `sort` method versus `sorted` function ### `sort` method versus `sorted` function
- `sort` (and other list methods) have an impact on the original list - `sort` (and other list methods) have an impact on the original list
- `sorted` function returns a new list with expected ordering - `sorted` function returns a new list with expected ordering
- default sorting order is ascending / alphanumeric - default sorting order is ascending / alphanumeric
- `reverse` parameter is applicable for both `sort` method and `sorted` function: - `reverse` parameter is applicable for both `sort` method and `sorted` function:
- enables you to specify descending order by passing argument as `True` - enables you to specify descending order by passing argument as `True`
%% Cell type:code id:c1e555f9 tags: %% Cell type:code id:c1e555f9 tags:
``` python ``` python
some_list = [10, 4, 25, 2, -10] some_list = [10, 4, 25, 2, -10]
``` ```
%% Cell type:code id:152297bb tags: %% Cell type:code id:152297bb tags:
``` python ``` python
# TODO: Invoke sort method # TODO: Invoke sort method
rv = some_list.sort() rv = some_list.sort()
print(some_list) print(some_list)
# What does the sort method return? # What does the sort method return?
# TODO: Capture return value into a variable rv and print the return value. # TODO: Capture return value into a variable rv and print the return value.
print(rv) print(rv)
``` ```
%% Output %% Output
[-10, 2, 4, 10, 25] [-10, 2, 4, 10, 25]
None None
%% Cell type:markdown id:3c0d5e7d tags: %% Cell type:markdown id:3c0d5e7d tags:
`sort` method returns `None` because it sorts the values in the original list `sort` method returns `None` because it sorts the values in the original list
%% Cell type:code id:c06d8976 tags: %% Cell type:code id:c06d8976 tags:
``` python ``` python
# TODO: invoke sorted function and pass some_list as argument # TODO: invoke sorted function and pass some_list as argument
# TODO: capture return value into sorted_some_list # TODO: capture return value into sorted_some_list
sorted_some_list = sorted(some_list) sorted_some_list = sorted(some_list)
# What does the sorted function return? # What does the sorted function return?
# It returns a brand new list with the values in sorted order # It returns a brand new list with the values in sorted order
print(sorted_some_list) print(sorted_some_list)
``` ```
%% Output %% Output
[-10, 2, 4, 10, 25] [-10, 2, 4, 10, 25]
%% Cell type:markdown id:ded0304c tags: %% Cell type:markdown id:ded0304c tags:
TODO: go back to `sort` method call and `sorted` function call and pass keyword argument `reverse = True`. TODO: go back to `sort` method call and `sorted` function call and pass keyword argument `reverse = True`.
%% Cell type:markdown id:3579e061 tags: %% Cell type:markdown id:3579e061 tags:
Can you call `sort` method on a set? Can you call `sort` method on a set?
%% Cell type:code id:14d8a670 tags: %% Cell type:code id:14d8a670 tags:
``` python ``` python
# some_set.sort() # some_set.sort()
# doesn't work: no method named sort associated with type set # doesn't work: no method named sort associated with type set
# you cannot sort a set because of the lack of ordering # you cannot sort a set because of the lack of ordering
``` ```
%% Cell type:markdown id:1fb64b44 tags: %% Cell type:markdown id:1fb64b44 tags:
Can you pass a `set` as argument to `sorted` function? Python is intelligent :) Can you pass a `set` as argument to `sorted` function? Python is intelligent :)
%% Cell type:code id:03b1183f tags: %% Cell type:code id:03b1183f tags:
``` python ``` python
# works because Python converts the set into a list and then sorts the list # works because Python converts the set into a list and then sorts the list
sorted(some_set) sorted(some_set)
``` ```
%% Output %% Output
[10, 20, 30, 40, 50] [10, 20, 30, 40, 50]
%% Cell type:markdown id:efa2869e tags: %% Cell type:markdown id:efa2869e tags:
### Function: `find_majors(phrase)` ### Function: `find_majors(phrase)`
%% Cell type:code id:655f876d tags: %% Cell type:code id:655f876d tags:
``` python ``` python
def find_majors(phrase): def find_majors(phrase):
""" """
find_majors(phrase) returns a list of all the room names that contain the find_majors(phrase) returns a list of all the room names that contain the
substring (case insensitive match) `phrase`. substring (case insensitive match) `phrase`.
""" """
# TODO: initialize the target list here # TODO: initialize the target list here
majors = [] majors = []
# TODO: iterate over row indices # TODO: iterate over row indices
for row_idx in range(len(cs220_data)): for row_idx in range(len(cs220_data)):
major = cell(row_idx, "Major") major = cell(row_idx, "Major")
if phrase.lower() in major.lower(): if phrase.lower() in major.lower():
majors.append(major) majors.append(major)
return majors return majors
``` ```
%% Cell type:markdown id:ed19265f tags: %% Cell type:markdown id:ed19265f tags:
### Find all `major` that contain **either** `"Computer"` **or** `"Science"`. ### Find all `major` that contain **either** `"Computer"` **or** `"Science"`.
Your output **must** be a *list*. The order **does not** matter, but if a `major` contains **both** `"Computer"` and `"Science"`, then the room must be included **only once** in your list. Your output **must** be a *list*. The order **does not** matter, but if a `major` contains **both** `"Computer"` and `"Science"`, then the room must be included **only once** in your list.
%% Cell type:code id:ab656189 tags: %% Cell type:code id:ab656189 tags:
``` python ``` python
computer_majors = find_majors("Computer") computer_majors = find_majors("Computer")
science_majors = find_majors("Science") science_majors = find_majors("Science")
computer_and_science_majors = computer_majors + science_majors computer_and_science_majors = computer_majors + science_majors
# TODO: Now find just the unique values # TODO: Now find just the unique values
computer_and_science_majors = list(set(computer_and_science_majors)) computer_and_science_majors = list(set(computer_and_science_majors))
computer_and_science_majors computer_and_science_majors
``` ```
%% Output %% Output
['Science: Other|Animal and Dairy Science', ['Science: Other|Animal and Dairy Science',
'Mathematics, Data Science', 'Mathematics, Data Science',
'Science: Physics', 'Science: Physics',
'Science: Other|Biophysics PhD', 'Science: Other|Biophysics PhD',
'Science: Other|Environmetal Science', 'Science: Other|Environmetal Science',
'Science: Other|Environmental Science', 'Science: Other|Environmental Science',
'Science: Other|Geoscience', 'Science: Other|Geoscience',
'Data Science', 'Data Science',
'Science: Other|Political Science', 'Science: Other|Political Science',
'Science: Other|Atmospheric and oceanic science', 'Science: Other|Atmospheric and oceanic science',
'Geoscience', 'Geoscience',
'Atmospheric Sciences', 'Atmospheric Sciences',
'Communication Sciences and Disorder', 'Communication Sciences and Disorder',
'Computer Science', 'Computer Science',
'Engineering: Other|Computer engineering', 'Engineering: Other|Computer engineering',
'Engineering: Other|Material Science Engineering', 'Engineering: Other|Material Science Engineering',
'Science: Other|Politcal Science', 'Science: Other|Politcal Science',
'Science: Other|animal sciences', 'Science: Other|animal sciences',
'Science: Chemistry', 'Science: Chemistry',
'Computer Science and Statistics', 'Computer Science and Statistics',
'Science: Other|Atmospheric & Oceanic Sciences', 'Science: Other|Atmospheric & Oceanic Sciences',
'Science: Other|Atmospheric and Oceanic Sciences', 'Science: Other|Atmospheric and Oceanic Sciences',
'Political Science', 'Political Science',
'Life Sciences Communication', 'Life Sciences Communication',
'Science: Biology/Life', 'Science: Biology/Life',
'Science: Other|Personal Finance', 'Science: Other|Personal Finance',
'Engineering: Other|Computer Engineering', 'Engineering: Other|Computer Engineering',
'Science: Other|Psychology', 'Science: Other|Psychology',
'Science: Other', 'Science: Other',
'Science: Other|Science: Genetics and Genomics', 'Science: Other|Science: Genetics and Genomics',
'Science: Other|Environmental science', 'Science: Other|Environmental science',
'Science: Other|Atmospheric and Oceanic Sciences (AOS)', 'Science: Other|Atmospheric and Oceanic Sciences (AOS)',
'Information science', 'Information science',
'Environmental science', 'Environmental science',
'Engineering: Other|Engineering: Computer'] 'Engineering: Other|Engineering: Computer']
%% Cell type:markdown id:64fd0945 tags: %% Cell type:markdown id:64fd0945 tags:
### Order the `major` that contain **either** `"Computer"` **or** `"Science"` using ascending order. ### Order the `major` that contain **either** `"Computer"` **or** `"Science"` using ascending order.
%% Cell type:code id:d4e2e6fc tags: %% Cell type:code id:d4e2e6fc tags:
``` python ``` python
# VERSION 1 # VERSION 1
# Be very careful: if you use sorted, make sure your return value # Be very careful: if you use sorted, make sure your return value
# variable matches with the variable for that project question # variable matches with the variable for that project question
sorted_computer_and_science_majors = sorted(computer_and_science_majors) sorted_computer_and_science_majors = sorted(computer_and_science_majors)
sorted_computer_and_science_majors sorted_computer_and_science_majors
``` ```
%% Output %% Output
['Atmospheric Sciences', ['Atmospheric Sciences',
'Communication Sciences and Disorder', 'Communication Sciences and Disorder',
'Computer Science', 'Computer Science',
'Computer Science and Statistics', 'Computer Science and Statistics',
'Data Science', 'Data Science',
'Engineering: Other|Computer Engineering', 'Engineering: Other|Computer Engineering',
'Engineering: Other|Computer engineering', 'Engineering: Other|Computer engineering',
'Engineering: Other|Engineering: Computer', 'Engineering: Other|Engineering: Computer',
'Engineering: Other|Material Science Engineering', 'Engineering: Other|Material Science Engineering',
'Environmental science', 'Environmental science',
'Geoscience', 'Geoscience',
'Information science', 'Information science',
'Life Sciences Communication', 'Life Sciences Communication',
'Mathematics, Data Science', 'Mathematics, Data Science',
'Political Science', 'Political Science',
'Science: Biology/Life', 'Science: Biology/Life',
'Science: Chemistry', 'Science: Chemistry',
'Science: Other', 'Science: Other',
'Science: Other|Animal and Dairy Science', 'Science: Other|Animal and Dairy Science',
'Science: Other|Atmospheric & Oceanic Sciences', 'Science: Other|Atmospheric & Oceanic Sciences',
'Science: Other|Atmospheric and Oceanic Sciences', 'Science: Other|Atmospheric and Oceanic Sciences',
'Science: Other|Atmospheric and Oceanic Sciences (AOS)', 'Science: Other|Atmospheric and Oceanic Sciences (AOS)',
'Science: Other|Atmospheric and oceanic science', 'Science: Other|Atmospheric and oceanic science',
'Science: Other|Biophysics PhD', 'Science: Other|Biophysics PhD',
'Science: Other|Environmental Science', 'Science: Other|Environmental Science',
'Science: Other|Environmental science', 'Science: Other|Environmental science',
'Science: Other|Environmetal Science', 'Science: Other|Environmetal Science',
'Science: Other|Geoscience', 'Science: Other|Geoscience',
'Science: Other|Personal Finance', 'Science: Other|Personal Finance',
'Science: Other|Politcal Science', 'Science: Other|Politcal Science',
'Science: Other|Political Science', 'Science: Other|Political Science',
'Science: Other|Psychology', 'Science: Other|Psychology',
'Science: Other|Science: Genetics and Genomics', 'Science: Other|Science: Genetics and Genomics',
'Science: Other|animal sciences', 'Science: Other|animal sciences',
'Science: Physics'] 'Science: Physics']
%% Cell type:code id:c28e77ce tags: %% Cell type:code id:c28e77ce tags:
``` python ``` python
# VERSION 2 # VERSION 2
computer_and_science_majors.sort() computer_and_science_majors.sort()
computer_and_science_majors computer_and_science_majors
``` ```
%% Output %% Output
['Atmospheric Sciences', ['Atmospheric Sciences',
'Communication Sciences and Disorder', 'Communication Sciences and Disorder',
'Computer Science', 'Computer Science',
'Computer Science and Statistics', 'Computer Science and Statistics',
'Data Science', 'Data Science',
'Engineering: Other|Computer Engineering', 'Engineering: Other|Computer Engineering',
'Engineering: Other|Computer engineering', 'Engineering: Other|Computer engineering',
'Engineering: Other|Engineering: Computer', 'Engineering: Other|Engineering: Computer',
'Engineering: Other|Material Science Engineering', 'Engineering: Other|Material Science Engineering',
'Environmental science', 'Environmental science',
'Geoscience', 'Geoscience',
'Information science', 'Information science',
'Life Sciences Communication', 'Life Sciences Communication',
'Mathematics, Data Science', 'Mathematics, Data Science',
'Political Science', 'Political Science',
'Science: Biology/Life', 'Science: Biology/Life',
'Science: Chemistry', 'Science: Chemistry',
'Science: Other', 'Science: Other',
'Science: Other|Animal and Dairy Science', 'Science: Other|Animal and Dairy Science',
'Science: Other|Atmospheric & Oceanic Sciences', 'Science: Other|Atmospheric & Oceanic Sciences',
'Science: Other|Atmospheric and Oceanic Sciences', 'Science: Other|Atmospheric and Oceanic Sciences',
'Science: Other|Atmospheric and Oceanic Sciences (AOS)', 'Science: Other|Atmospheric and Oceanic Sciences (AOS)',
'Science: Other|Atmospheric and oceanic science', 'Science: Other|Atmospheric and oceanic science',
'Science: Other|Biophysics PhD', 'Science: Other|Biophysics PhD',
'Science: Other|Environmental Science', 'Science: Other|Environmental Science',
'Science: Other|Environmental science', 'Science: Other|Environmental science',
'Science: Other|Environmetal Science', 'Science: Other|Environmetal Science',
'Science: Other|Geoscience', 'Science: Other|Geoscience',
'Science: Other|Personal Finance', 'Science: Other|Personal Finance',
'Science: Other|Politcal Science', 'Science: Other|Politcal Science',
'Science: Other|Political Science', 'Science: Other|Political Science',
'Science: Other|Psychology', 'Science: Other|Psychology',
'Science: Other|Science: Genetics and Genomics', 'Science: Other|Science: Genetics and Genomics',
'Science: Other|animal sciences', 'Science: Other|animal sciences',
'Science: Physics'] 'Science: Physics']
%% Cell type:markdown id:e354b781 tags: %% Cell type:markdown id:e354b781 tags:
### Order the `major` that contain **either** `"Computer"` **or** `"Science"` using descending order. ### Order the `major` that contain **either** `"Computer"` **or** `"Science"` using descending order.
%% Cell type:code id:ca887135 tags: %% Cell type:code id:ca887135 tags:
``` python ``` python
# VERSION 1 # VERSION 1
# Be very careful: if you use sorted, make sure your return value # Be very careful: if you use sorted, make sure your return value
# variable matches with the variable for that project question # variable matches with the variable for that project question
reverse_sorted_computer_and_science_majors = sorted(computer_and_science_majors, reverse = True) reverse_sorted_computer_and_science_majors = sorted(computer_and_science_majors, reverse = True)
reverse_sorted_computer_and_science_majors reverse_sorted_computer_and_science_majors
``` ```
%% Output %% Output
['Science: Physics', ['Science: Physics',
'Science: Other|animal sciences', 'Science: Other|animal sciences',
'Science: Other|Science: Genetics and Genomics', 'Science: Other|Science: Genetics and Genomics',
'Science: Other|Psychology', 'Science: Other|Psychology',
'Science: Other|Political Science', 'Science: Other|Political Science',
'Science: Other|Politcal Science', 'Science: Other|Politcal Science',
'Science: Other|Personal Finance', 'Science: Other|Personal Finance',
'Science: Other|Geoscience', 'Science: Other|Geoscience',
'Science: Other|Environmetal Science', 'Science: Other|Environmetal Science',
'Science: Other|Environmental science', 'Science: Other|Environmental science',
'Science: Other|Environmental Science', 'Science: Other|Environmental Science',
'Science: Other|Biophysics PhD', 'Science: Other|Biophysics PhD',
'Science: Other|Atmospheric and oceanic science', 'Science: Other|Atmospheric and oceanic science',
'Science: Other|Atmospheric and Oceanic Sciences (AOS)', 'Science: Other|Atmospheric and Oceanic Sciences (AOS)',
'Science: Other|Atmospheric and Oceanic Sciences', 'Science: Other|Atmospheric and Oceanic Sciences',
'Science: Other|Atmospheric & Oceanic Sciences', 'Science: Other|Atmospheric & Oceanic Sciences',
'Science: Other|Animal and Dairy Science', 'Science: Other|Animal and Dairy Science',
'Science: Other', 'Science: Other',
'Science: Chemistry', 'Science: Chemistry',
'Science: Biology/Life', 'Science: Biology/Life',
'Political Science', 'Political Science',
'Mathematics, Data Science', 'Mathematics, Data Science',
'Life Sciences Communication', 'Life Sciences Communication',
'Information science', 'Information science',
'Geoscience', 'Geoscience',
'Environmental science', 'Environmental science',
'Engineering: Other|Material Science Engineering', 'Engineering: Other|Material Science Engineering',
'Engineering: Other|Engineering: Computer', 'Engineering: Other|Engineering: Computer',
'Engineering: Other|Computer engineering', 'Engineering: Other|Computer engineering',
'Engineering: Other|Computer Engineering', 'Engineering: Other|Computer Engineering',
'Data Science', 'Data Science',
'Computer Science and Statistics', 'Computer Science and Statistics',
'Computer Science', 'Computer Science',
'Communication Sciences and Disorder', 'Communication Sciences and Disorder',
'Atmospheric Sciences'] 'Atmospheric Sciences']
%% Cell type:code id:1606075f tags: %% Cell type:code id:1606075f tags:
``` python ``` python
# VERSION 2 # VERSION 2
computer_and_science_majors.sort(reverse = True) computer_and_science_majors.sort(reverse = True)
computer_and_science_majors computer_and_science_majors
``` ```
%% Output %% Output
['Science: Physics', ['Science: Physics',
'Science: Other|animal sciences', 'Science: Other|animal sciences',
'Science: Other|Science: Genetics and Genomics', 'Science: Other|Science: Genetics and Genomics',
'Science: Other|Psychology', 'Science: Other|Psychology',
'Science: Other|Political Science', 'Science: Other|Political Science',
'Science: Other|Politcal Science', 'Science: Other|Politcal Science',
'Science: Other|Personal Finance', 'Science: Other|Personal Finance',
'Science: Other|Geoscience', 'Science: Other|Geoscience',
'Science: Other|Environmetal Science', 'Science: Other|Environmetal Science',
'Science: Other|Environmental science', 'Science: Other|Environmental science',
'Science: Other|Environmental Science', 'Science: Other|Environmental Science',
'Science: Other|Biophysics PhD', 'Science: Other|Biophysics PhD',
'Science: Other|Atmospheric and oceanic science', 'Science: Other|Atmospheric and oceanic science',
'Science: Other|Atmospheric and Oceanic Sciences (AOS)', 'Science: Other|Atmospheric and Oceanic Sciences (AOS)',
'Science: Other|Atmospheric and Oceanic Sciences', 'Science: Other|Atmospheric and Oceanic Sciences',
'Science: Other|Atmospheric & Oceanic Sciences', 'Science: Other|Atmospheric & Oceanic Sciences',
'Science: Other|Animal and Dairy Science', 'Science: Other|Animal and Dairy Science',
'Science: Other', 'Science: Other',
'Science: Chemistry', 'Science: Chemistry',
'Science: Biology/Life', 'Science: Biology/Life',
'Political Science', 'Political Science',
'Mathematics, Data Science', 'Mathematics, Data Science',
'Life Sciences Communication', 'Life Sciences Communication',
'Information science', 'Information science',
'Geoscience', 'Geoscience',
'Environmental science', 'Environmental science',
'Engineering: Other|Material Science Engineering', 'Engineering: Other|Material Science Engineering',
'Engineering: Other|Engineering: Computer', 'Engineering: Other|Engineering: Computer',
'Engineering: Other|Computer engineering', 'Engineering: Other|Computer engineering',
'Engineering: Other|Computer Engineering', 'Engineering: Other|Computer Engineering',
'Data Science', 'Data Science',
'Computer Science and Statistics', 'Computer Science and Statistics',
'Computer Science', 'Computer Science',
'Communication Sciences and Disorder', 'Communication Sciences and Disorder',
'Atmospheric Sciences'] 'Atmospheric Sciences']
%% Cell type:markdown id:c495a293 tags: %% Cell type:markdown id:c495a293 tags:
### For `major` containing `"other"`, extract the details that come after `"|"`. ### For `major` containing `"other"`, extract the details that come after `"|"`.
%% Cell type:code id:ab46c152 tags: %% Cell type:code id:ab46c152 tags:
``` python ``` python
other_majors = find_majors("other") other_majors = find_majors("other")
other_major_details = [] other_major_details = []
for other in other_majors: for other in other_majors:
details = other.split("|") details = other.split("|")
if len(details) > 1: if len(details) > 1:
other_major_details.append(details[1]) other_major_details.append(details[1])
other_major_details other_major_details
``` ```
%% Output %% Output
['Engineering: Computer', ['Engineering: Computer',
'Political Science', 'Political Science',
'Real Estate', 'Real Estate',
'Engineering Physics: Scientific Computing', 'Engineering Physics: Scientific Computing',
'Accounting', 'Accounting',
'business analytics', 'business analytics',
'animal sciences', 'animal sciences',
'Science: Genetics and Genomics', 'Science: Genetics and Genomics',
'Business: Accounting', 'Business: Accounting',
'Computer Engineering', 'Computer Engineering',
'Computer engineering', 'Computer engineering',
'Material Science Engineering', 'Material Science Engineering',
'Environmental Science', 'Environmental Science',
'Chemical Engineering', 'Chemical Engineering',
'Biophysics PhD', 'Biophysics PhD',
'Technology Strategy/ Product Management', 'Technology Strategy/ Product Management',
'Marketing', 'Marketing',
'Consumer Behavior and Marketplace Studies', 'Consumer Behavior and Marketplace Studies',
'Psychology', 'Psychology',
'Civil and Environmental Engineering', 'Civil and Environmental Engineering',
'MHR', 'MHR',
'Personal Finance', 'Personal Finance',
'Real Estate', 'Real Estate',
'Environmental Science', 'Environmental Science',
'Psychology', 'Psychology',
'accounting', 'accounting',
'Environmetal Science', 'Environmetal Science',
'Atmospheric and Oceanic Sciences (AOS)', 'Atmospheric and Oceanic Sciences (AOS)',
'Business Analytics', 'Business Analytics',
'Politcal Science', 'Politcal Science',
'Geoscience', 'Geoscience',
'Marketing', 'Marketing',
'Atmospheric and oceanic science', 'Atmospheric and oceanic science',
'Environmental Science', 'Environmental Science',
'Marketing', 'Marketing',
'Engineering Mechanics', 'Engineering Mechanics',
'Environmental science', 'Environmental science',
'Atmospheric and Oceanic Sciences', 'Atmospheric and Oceanic Sciences',
'Civil- Intelligent Transportation System', 'Civil- Intelligent Transportation System',
'Animal and Dairy Science', 'Animal and Dairy Science',
'Atmospheric & Oceanic Sciences', 'Atmospheric & Oceanic Sciences',
'Accounting', 'Accounting',
'Environmental Science'] 'Environmental Science']
%% Cell type:markdown id:31a381fe tags: %% Cell type:markdown id:31a381fe tags:
## Self-practice ## Self-practice
%% Cell type:markdown id:fd43b7b3 tags:
### Function: `find_fav_locations_within(lat_min, lat_max, long_min, long_max)`
%% Cell type:code id:a403a92c tags:
``` python
def find_fav_locations_within(lat_min, lat_max, long_min, long_max):
"""
find_prices_within(lat_min, lat_max, long_min, long_max) returns a nested list.
First inner list contains latitudes of favourite places within the geographical
location between and including
the latitudes lat_min and lat_max and longitudes long_min and long_max.
Second inner list contains longitudes of favourite places within the geographical
location between and including
the latitudes lat_min and lat_max and longitudes long_min and long_max.
"""
pass
```
%% Cell type:markdown id:f2699919 tags:
### What are the favourite places within United States?
```
top = 49.3457868 # north lat
bottom = 24.7433195 # south lat
left = -124.7844079 # west long
right = -66.9513812 # east long
```
%% Cell type:markdown id:8ac26620 tags: %% Cell type:markdown id:8ac26620 tags:
### How many students are both a procrastinator and a pet owner? ### How many students are both a procrastinator and a pet owner?
%% Cell type:markdown id:172141ea tags: %% Cell type:markdown id:172141ea tags:
### What percentage of 18-year-olds have their major declared as "Other"? ### What percentage of 18-year-olds have their major declared as "Other"?
%% Cell type:markdown id:d9a7a2b1 tags: %% Cell type:markdown id:d9a7a2b1 tags:
### How old is the oldest basil/spinach-loving Business major? ### How old is the oldest basil/spinach-loving Business major?
%% Cell type:code id:5fcc04f2 tags: %% Cell type:code id:5fcc04f2 tags:
``` python ``` python
``` ```
......
%% Cell type:markdown id:72348536 tags: %% Cell type:markdown id:72348536 tags:
# List Practice # List Practice
%% Cell type:code id:d21a94b5 tags: %% Cell type:code id:d21a94b5 tags:
``` python ``` python
import csv import csv
``` ```
%% Cell type:markdown id:cd8a434c tags: %% Cell type:markdown id:cd8a434c tags:
### Warmup 1: min / max ### Warmup 1: min / max
%% Cell type:code id:baa730ba tags: %% Cell type:code id:baa730ba tags:
``` python ``` python
some_list = [45, -4, 66, 220, 10] some_list = [45, -4, 66, 220, 10]
min_val = None min_val = None
for val in some_list: for val in some_list:
if min_val == None or val < min_val: if min_val == None or val < min_val:
min_val = val min_val = val
print(min_val) print(min_val)
max_val = None max_val = None
for val in some_list: for val in some_list:
if max_val == None or val > max_val: if max_val == None or val > max_val:
max_val = val max_val = val
print(max_val) print(max_val)
``` ```
%% Cell type:markdown id:3502c700 tags: %% Cell type:markdown id:3502c700 tags:
### Warmup 2: median ### Warmup 2: median
%% Cell type:code id:414ae09e tags: %% Cell type:code id:414ae09e tags:
``` python ``` python
def median(some_items): def median(some_items):
""" """
Returns median of a list passed as argument Returns median of a list passed as argument
""" """
pass pass
nums = [5, 4, 3, 2, 1] nums = [5, 4, 3, 2, 1]
print("Median of", nums, "is" , median(nums)) print("Median of", nums, "is" , median(nums))
nums = [6, 5, 4, 3, 2, 1] nums = [6, 5, 4, 3, 2, 1]
print("Median of", nums, "is" , median(nums)) print("Median of", nums, "is" , median(nums))
``` ```
%% Cell type:code id:73fa337e tags: %% Cell type:code id:73fa337e tags:
``` python ``` python
vals = ["A", "C", "B"] vals = ["A", "C", "B"]
print("Median of", nums, "is" , median(vals)) print("Median of", nums, "is" , median(vals))
vals = ["A", "C", "B", "D"] vals = ["A", "C", "B", "D"]
# print("Median of", nums, "is" , median(vals)) # does not work due to TypeError # print("Median of", nums, "is" , median(vals)) # does not work due to TypeError
``` ```
%% Cell type:markdown id:050fd57c tags: %% Cell type:markdown id:050fd57c tags:
### set data structure ### set data structure
- **not a sequence** - **not a sequence**
- no ordering of values: - no ordering of values:
- this implies that you can only store unique values within a `set` - this implies that you can only store unique values within a `set`
- very helpful to find unique values stored in a `list` - very helpful to find unique values stored in a `list`
- easy to convert a `list` to `set` and vice-versa. - easy to convert a `list` to `set` and vice-versa.
- ordering is not guaranteed once we use `set` - ordering is not guaranteed once we use `set`
%% Cell type:code id:7d4a693f tags: %% Cell type:code id:7d4a693f tags:
``` python ``` python
some_set = {10, 20, 30, 30, 40, 50, 10} # use a pair of curly braces to define it some_set = {10, 20, 30, 30, 40, 50, 10} # use a pair of curly braces to define it
some_set some_set
``` ```
%% Cell type:code id:baef596c tags: %% Cell type:code id:baef596c tags:
``` python ``` python
some_list = [10, 20, 30, 30, 40, 50, 10] # Initialize a list containing duplicate numbers some_list = [10, 20, 30, 30, 40, 50, 10] # Initialize a list containing duplicate numbers
# TODO: to find unique values, convert it into a set # TODO: to find unique values, convert it into a set
print(set(some_list)) print(set(some_list))
# TODO: convert the set back into a list # TODO: convert the set back into a list
print(list(set(some_list))) print(list(set(some_list)))
``` ```
%% Cell type:markdown id:2be52d13 tags: %% Cell type:markdown id:2be52d13 tags:
Can you index / slice into a `set`? Can you index / slice into a `set`?
%% Cell type:code id:f622a5eb tags: %% Cell type:code id:f622a5eb tags:
``` python ``` python
some_set[1] # doesn't work - remember set has no order some_set[1] # doesn't work - remember set has no order
``` ```
%% Cell type:code id:e679d3a7 tags: %% Cell type:code id:e679d3a7 tags:
``` python ``` python
some_set[1:] # doesn't work - remember set has no order some_set[1:] # doesn't work - remember set has no order
``` ```
%% Cell type:code id:9d936c1c tags: %% Cell type:code id:9d936c1c tags:
``` python ``` python
# inspired by https://automatetheboringstuff.com/2e/chapter16/ # inspired by https://automatetheboringstuff.com/2e/chapter16/
def process_csv(filename): def process_csv(filename):
# open the file, its a text file utf-8 # open the file, its a text file utf-8
example_file = open(filename, encoding="utf-8") example_file = open(filename, encoding="utf-8")
# prepare it for reading as a CSV object # prepare it for reading as a CSV object
example_reader = csv.reader(example_file) example_reader = csv.reader(example_file)
# use the built-in list function to convert this into a list of lists # use the built-in list function to convert this into a list of lists
example_data = list(example_reader) example_data = list(example_reader)
# close the file to tidy up our workspace # close the file to tidy up our workspace
example_file.close() example_file.close()
# return the list of lists # return the list of lists
return example_data return example_data
``` ```
%% Cell type:markdown id:89621c98 tags: %% Cell type:markdown id:89621c98 tags:
### Student Information Survey data ### Student Information Survey data
%% Cell type:code id:d3c252b4 tags: %% Cell type:code id:d3c252b4 tags:
``` python ``` python
# TODO: call the process_csv function and store the list of lists in cs220_csv # TODO: call the process_csv function and store the list of lists in cs220_csv
cs220_csv = process_csv(???) cs220_csv = process_csv(???)
``` ```
%% Cell type:code id:5838ae5f tags: %% Cell type:code id:5838ae5f tags:
``` python ``` python
# Store the header row into cs220_header, using indexing # Store the header row into cs220_header, using indexing
cs220_header = ??? cs220_header = ???
cs220_header cs220_header
``` ```
%% Cell type:code id:66fda88d tags: %% Cell type:code id:66fda88d tags:
``` python ``` python
# TODO: Store all of the data rows into cs220_data, using slicing # TODO: Store all of the data rows into cs220_data, using slicing
cs220_data = ??? cs220_data = ???
# TODO: use slicing to display top 3 rows data # TODO: use slicing to display top 3 rows data
cs220_data??? cs220_data???
``` ```
%% Cell type:markdown id:4267fe3e tags: %% Cell type:markdown id:4267fe3e tags:
### What `Pizza topping` does the 13th student prefer? ### What `Pizza topping` does the 13th student prefer?
%% Cell type:code id:4b8dbe8b tags: %% Cell type:code id:4b8dbe8b tags:
``` python ``` python
# bad example: we hard-coded the column index # bad example: we hard-coded the column index
``` ```
%% Cell type:markdown id:4f125240 tags: %% Cell type:markdown id:4f125240 tags:
What if we decided to add a new column before sleeping habit? Your code will no longer work. What if we decided to add a new column before sleeping habit? Your code will no longer work.
Instead of hard-coding column index, you should use `index` method, to lookup column index from the header variable. This will also make your code so much readable. Instead of hard-coding column index, you should use `index` method, to lookup column index from the header variable. This will also make your code so much readable.
%% Cell type:code id:f2e52e06 tags: %% Cell type:code id:f2e52e06 tags:
``` python ``` python
``` ```
%% Cell type:markdown id:5d298a4c tags: %% Cell type:markdown id:5d298a4c tags:
### What is the Lecture of the 4th student? ### What is the Lecture of the 4th student?
%% Cell type:code id:3617b3de tags: %% Cell type:code id:3617b3de tags:
``` python ``` python
``` ```
%% Cell type:markdown id:059de363 tags: %% Cell type:markdown id:059de363 tags:
### What **unique** `age` values are included in the dataset? ### What **unique** `age` values are included in the dataset?
%% Cell type:code id:45909f22 tags: %% Cell type:code id:45909f22 tags:
``` python ``` python
``` ```
%% Cell type:markdown id:8e18663d tags: %% Cell type:markdown id:8e18663d tags:
### cell function ### cell function
- It would be very helpful to define a cell function, which can handle missing data and type conversions - It would be very helpful to define a cell function, which can handle missing data and type conversions
%% Cell type:code id:bba90038 tags: %% Cell type:code id:bba90038 tags:
``` python ``` python
def cell(row_idx, col_name): def cell(row_idx, col_name):
""" """
Returns the data value (cell) corresponding to the row index and Returns the data value (cell) corresponding to the row index and
the column name of a CSV file. the column name of a CSV file.
""" """
# TODO: get the index of col_name # TODO: get the index of col_name
# TODO: get the value of cs220_data at the specified cell # TODO: get the value of cs220_data at the specified cell
# TODO: handle missing values, by returning None # TODO: handle missing values, by returning None
# TODO: handle type conversions # TODO: handle type conversions
return val return val
``` ```
%% Cell type:markdown id:b7c8e726 tags: %% Cell type:markdown id:b7c8e726 tags:
### Function `avg_age_per_lecture(lecture)` ### Function `avg_age_per_lecture(lecture)`
%% Cell type:code id:fa5598e0 tags: %% Cell type:code id:fa5598e0 tags:
``` python ``` python
def avg_age_per_lecture(lecture): def avg_age_per_lecture(lecture):
''' '''
avg_age_per_lecture(lecture) returns the average age of avg_age_per_lecture(lecture) returns the average age of
the students in the given `lecture`; if there are no the students in the given `lecture`; if there are no
students in the given `lecture`, it returns `None` students in the given `lecture`, it returns `None`
''' '''
# To compute average you don't need to actually populate a list. # To compute average you don't need to actually populate a list.
# But here a list will come in handy. It will help you with the None return requirement. # But here a list will come in handy. It will help you with the None return requirement.
pass pass
``` ```
%% Cell type:code id:f0a05e42 tags: %% Cell type:code id:f0a05e42 tags:
``` python ``` python
avg_age_per_lecture("LEC002") avg_age_per_lecture("LEC002")
``` ```
%% Cell type:code id:9f2c7e6e tags: %% Cell type:code id:9f2c7e6e tags:
``` python ``` python
print(avg_age_per_lecture("LEC007")) print(avg_age_per_lecture("LEC007"))
``` ```
%% Cell type:markdown id:94548bf4 tags: %% Cell type:markdown id:94548bf4 tags:
### `sort` method versus `sorted` function ### `sort` method versus `sorted` function
- `sort` (and other list methods) have an impact on the original list - `sort` (and other list methods) have an impact on the original list
- `sorted` function returns a new list with expected ordering - `sorted` function returns a new list with expected ordering
- default sorting order is ascending / alphanumeric - default sorting order is ascending / alphanumeric
- `reverse` parameter is applicable for both `sort` method and `sorted` function: - `reverse` parameter is applicable for both `sort` method and `sorted` function:
- enables you to specify descending order by passing argument as `True` - enables you to specify descending order by passing argument as `True`
%% Cell type:code id:c1e555f9 tags: %% Cell type:code id:c1e555f9 tags:
``` python ``` python
some_list = [10, 4, 25, 2, -10] some_list = [10, 4, 25, 2, -10]
``` ```
%% Cell type:code id:152297bb tags: %% Cell type:code id:152297bb tags:
``` python ``` python
# TODO: Invoke sort method # TODO: Invoke sort method
rv = ??? rv = ???
print(some_list) print(some_list)
# What does the sort method return? # What does the sort method return?
# TODO: Capture return value into a variable rv and print the return value. # TODO: Capture return value into a variable rv and print the return value.
print(rv) print(rv)
``` ```
%% Cell type:markdown id:3c0d5e7d tags: %% Cell type:markdown id:3c0d5e7d tags:
`sort` method returns `None` because it sorts the values in the original list `sort` method returns `None` because it sorts the values in the original list
%% Cell type:code id:c06d8976 tags: %% Cell type:code id:c06d8976 tags:
``` python ``` python
# TODO: invoke sorted function and pass some_list as argument # TODO: invoke sorted function and pass some_list as argument
# TODO: capture return value into sorted_some_list # TODO: capture return value into sorted_some_list
??? ???
# What does the sorted function return? # What does the sorted function return?
# It returns a brand new list with the values in sorted order # It returns a brand new list with the values in sorted order
print(sorted_some_list) print(sorted_some_list)
``` ```
%% Cell type:markdown id:ded0304c tags: %% Cell type:markdown id:ded0304c tags:
TODO: go back to `sort` method call and `sorted` function call and pass keyword argument `reverse = True`. TODO: go back to `sort` method call and `sorted` function call and pass keyword argument `reverse = True`.
%% Cell type:markdown id:35894ef5 tags: %% Cell type:markdown id:35894ef5 tags:
Can you call `sort` method on a set? Can you call `sort` method on a set?
%% Cell type:code id:fc08879e tags: %% Cell type:code id:fc08879e tags:
``` python ``` python
some_set.sort() some_set.sort()
# doesn't work: no method named sort associated with type set # doesn't work: no method named sort associated with type set
# you cannot sort a set because of the lack of ordering # you cannot sort a set because of the lack of ordering
``` ```
%% Cell type:markdown id:99161c42 tags: %% Cell type:markdown id:99161c42 tags:
Can you pass a `set` as argument to `sorted` function? Python is intelligent :) Can you pass a `set` as argument to `sorted` function? Python is intelligent :)
%% Cell type:code id:2549df29 tags: %% Cell type:code id:2549df29 tags:
``` python ``` python
# works because Python converts the set into a list and then sorts the list # works because Python converts the set into a list and then sorts the list
sorted(some_set) sorted(some_set)
``` ```
%% Cell type:markdown id:5c7f3489 tags: %% Cell type:markdown id:5c7f3489 tags:
### Function: `find_majors(phrase)` ### Function: `find_majors(phrase)`
%% Cell type:code id:b6adbfe0 tags: %% Cell type:code id:b6adbfe0 tags:
``` python ``` python
def find_majors(phrase): def find_majors(phrase):
""" """
find_majors(phrase) returns a list of all the room names that contain the find_majors(phrase) returns a list of all the room names that contain the
substring (case insensitive match) `phrase`. substring (case insensitive match) `phrase`.
""" """
# TODO: initialize the target list here # TODO: initialize the target list here
# TODO: iterate over row indices # TODO: iterate over row indices
for row_idx in range(len(cs220_data)): for row_idx in range(len(cs220_data)):
major = cell(row_idx, "Major") major = cell(row_idx, "Major")
# TODO: write the actual logic here # TODO: write the actual logic here
return majors return majors
``` ```
%% Cell type:markdown id:1b7f671f tags: %% Cell type:markdown id:1b7f671f tags:
### Find all `major` that contain **either** `"Computer"` **or** `"Science"`. ### Find all `major` that contain **either** `"Computer"` **or** `"Science"`.
Your output **must** be a *list*. The order **does not** matter, but if a `major` contains **both** `"Computer"` and `"Science"`, then the room must be included **only once** in your list. Your output **must** be a *list*. The order **does not** matter, but if a `major` contains **both** `"Computer"` and `"Science"`, then the room must be included **only once** in your list.
%% Cell type:code id:ed895a3b tags: %% Cell type:code id:ed895a3b tags:
``` python ``` python
computer_majors = ??? computer_majors = ???
science_majors = ??? science_majors = ???
computer_and_science_majors = ??? computer_and_science_majors = ???
# TODO: Now find just the unique values # TODO: Now find just the unique values
computer_and_science_majors = ??? computer_and_science_majors = ???
computer_and_science_majors computer_and_science_majors
``` ```
%% Cell type:markdown id:64fd0945 tags: %% Cell type:markdown id:64fd0945 tags:
### Order the `major` that contain **either** `"Computer"` **or** `"Science"` using ascending order. ### Order the `major` that contain **either** `"Computer"` **or** `"Science"` using ascending order.
%% Cell type:code id:efcdf514 tags: %% Cell type:code id:efcdf514 tags:
``` python ``` python
# VERSION 1 # VERSION 1
# Be very careful: if you use sorted, make sure your return value # Be very careful: if you use sorted, make sure your return value
# variable matches with the variable for that project question # variable matches with the variable for that project question
sorted_computer_and_science_majors = sorted(computer_and_science_majors) sorted_computer_and_science_majors = sorted(computer_and_science_majors)
sorted_computer_and_science_majors sorted_computer_and_science_majors
``` ```
%% Cell type:code id:c28e77ce tags: %% Cell type:code id:c28e77ce tags:
``` python ``` python
# VERSION 2 # VERSION 2
computer_and_science_majors.sort() computer_and_science_majors.sort()
computer_and_science_majors computer_and_science_majors
``` ```
%% Cell type:markdown id:e354b781 tags: %% Cell type:markdown id:e354b781 tags:
### Order the `major` that contain **either** `"Computer"` **or** `"Science"` using descending order. ### Order the `major` that contain **either** `"Computer"` **or** `"Science"` using descending order.
%% Cell type:code id:ca887135 tags: %% Cell type:code id:ca887135 tags:
``` python ``` python
# VERSION 1 # VERSION 1
# Be very careful: if you use sorted, make sure your return value # Be very careful: if you use sorted, make sure your return value
# variable matches with the variable for that project question # variable matches with the variable for that project question
reverse_sorted_computer_and_science_majors = sorted(computer_and_science_majors, reverse = ???) reverse_sorted_computer_and_science_majors = sorted(computer_and_science_majors, reverse = ???)
reverse_sorted_computer_and_science_majors reverse_sorted_computer_and_science_majors
``` ```
%% Cell type:code id:b6c61532 tags: %% Cell type:code id:b6c61532 tags:
``` python ``` python
# VERSION 2 # VERSION 2
computer_and_science_majors.sort(reverse = ???) computer_and_science_majors.sort(reverse = ???)
computer_and_science_majors computer_and_science_majors
``` ```
%% Cell type:markdown id:2862160c tags: %% Cell type:markdown id:2862160c tags:
### For `major` containing `"other"`, extract the details that come after `"|"`. ### For `major` containing `"other"`, extract the details that come after `"|"`.
%% Cell type:code id:600fae6c tags: %% Cell type:code id:600fae6c tags:
``` python ``` python
other_majors = find_majors("other") other_majors = find_majors("other")
other_major_details = [] other_major_details = []
for other in other_majors: for other in other_majors:
print(other) print(other)
# TODO: complete the rest of the logic # TODO: complete the rest of the logic
other_major_details other_major_details
``` ```
%% Cell type:markdown id:31a381fe tags: %% Cell type:markdown id:31a381fe tags:
## Self-practice ## Self-practice
%% Cell type:markdown id:fe5b9303 tags:
### Function: `find_fav_locations_within(lat_min, lat_max, long_min, long_max)`
%% Cell type:code id:f8443ad2 tags:
``` python
def find_fav_locations_within(lat_min, lat_max, long_min, long_max):
"""
find_prices_within(lat_min, lat_max, long_min, long_max) returns a nested list.
First inner list contains latitudes of favourite places within the geographical
location between and including
the latitudes lat_min and lat_max and longitudes long_min and long_max.
Second inner list contains longitudes of favourite places within the geographical
location between and including
the latitudes lat_min and lat_max and longitudes long_min and long_max.
"""
pass
```
%% Cell type:markdown id:4e0d63eb tags:
### What are the favourite places within United States?
```
top = 49.3457868 # north lat
bottom = 24.7433195 # south lat
left = -124.7844079 # west long
right = -66.9513812 # east long
```
%% Cell type:markdown id:8ac26620 tags: %% Cell type:markdown id:8ac26620 tags:
### How many students are both a procrastinator and a pet owner? ### How many students are both a procrastinator and a pet owner?
%% Cell type:markdown id:172141ea tags: %% Cell type:markdown id:172141ea tags:
### What percentage of 18-year-olds have their major declared as "Other"? ### What percentage of 18-year-olds have their major declared as "Other"?
%% Cell type:markdown id:d9a7a2b1 tags: %% Cell type:markdown id:d9a7a2b1 tags:
### How old is the oldest basil/spinach-loving Business major? ### How old is the oldest basil/spinach-loving Business major?
%% Cell type:code id:5fcc04f2 tags: %% Cell type:code id:5fcc04f2 tags:
``` python ``` 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