" find_prices_within(lat_min, lat_max, long_min, long_max) returns a nested list.\n",
" First inner list contains latitudes of favourite places within the geographical \n",
" location between and including\n",
" the latitudes lat_min and lat_max and longitudes long_min and long_max.\n",
" Second inner list contains longitudes of favourite places within the geographical \n",
" location between and including\n",
" the latitudes lat_min and lat_max and longitudes long_min and long_max.\n",
" \"\"\"\n",
" pass"
]
},
{
"cell_type": "markdown",
"id": "f2699919",
"metadata": {},
"source": [
"### What are the favourite places within United States?\n",
"\n",
"```\n",
"top = 49.3457868 # north lat\n",
"bottom = 24.7433195 # south lat\n",
"left = -124.7844079 # west long\n",
"right = -66.9513812 # east long\n",
"```"
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"id": "8ac26620",
"id": "8ac26620",
...
...
%% 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
importcsv
importcsv
```
```
%% 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
forvalinsome_list:
forvalinsome_list:
ifmin_val==Noneorval<min_val:
ifmin_val==Noneorval<min_val:
min_val=val
min_val=val
print(min_val)
print(min_val)
max_val=None
max_val=None
forvalinsome_list:
forvalinsome_list:
ifmax_val==Noneorval>max_val:
ifmax_val==Noneorval>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
defmedian(some_items):
defmedian(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)
ifn%2==1:
ifn%2==1:
returnsome_items[n//2]
returnsome_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
returnmedian
returnmedian
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/
defprocess_csv(filename):
defprocess_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
returnexample_data
returnexample_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.
- 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
defcell(row_idx,col_name):
defcell(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
ifval=='':
ifval=='':
returnNone
returnNone
# TODO: handle type conversions
# TODO: handle type conversions
ifcol_namein["Age",'Zip Code',]:
ifcol_namein["Age",'Zip Code',]:
returnint(val)
returnint(val)
elifcol_namein['Latitude','Longitude']:
elifcol_namein['Latitude','Longitude']:
returnfloat(val)
returnfloat(val)
returnval
returnval
```
```
%% 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
defavg_age_per_lecture(lecture):
defavg_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=[]
forrow_idxinrange(len(cs220_data)):
forrow_idxinrange(len(cs220_data)):
curr_lecture=cell(row_idx,"Lecture")
curr_lecture=cell(row_idx,"Lecture")
iflecture==curr_lecture:
iflecture==curr_lecture:
age=cell(row_idx,"Age")
age=cell(row_idx,"Age")
ifage!=Noneandage>0andage<=118:
ifage!=Noneandage>0andage<=118:
ages.append(age)
ages.append(age)
iflen(ages)>0:
iflen(ages)>0:
returnsum(ages)/len(ages)
returnsum(ages)/len(ages)
else:
else:
returnNone
returnNone
```
```
%% 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
deffind_majors(phrase):
deffind_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
forrow_idxinrange(len(cs220_data)):
forrow_idxinrange(len(cs220_data)):
major=cell(row_idx,"Major")
major=cell(row_idx,"Major")
ifphrase.lower()inmajor.lower():
ifphrase.lower()inmajor.lower():
majors.append(major)
majors.append(major)
returnmajors
returnmajors
```
```
%% 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.
" find_prices_within(lat_min, lat_max, long_min, long_max) returns a nested list.\n",
" First inner list contains latitudes of favourite places within the geographical \n",
" location between and including\n",
" the latitudes lat_min and lat_max and longitudes long_min and long_max.\n",
" Second inner list contains longitudes of favourite places within the geographical \n",
" location between and including\n",
" the latitudes lat_min and lat_max and longitudes long_min and long_max.\n",
" \"\"\"\n",
" pass"
]
},
{
"cell_type": "markdown",
"id": "4e0d63eb",
"metadata": {},
"source": [
"### What are the favourite places within United States?\n",
"\n",
"```\n",
"top = 49.3457868 # north lat\n",
"bottom = 24.7433195 # south lat\n",
"left = -124.7844079 # west long\n",
"right = -66.9513812 # east long\n",
"```"
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"id": "8ac26620",
"id": "8ac26620",
...
...
%% 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
importcsv
importcsv
```
```
%% 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
forvalinsome_list:
forvalinsome_list:
ifmin_val==Noneorval<min_val:
ifmin_val==Noneorval<min_val:
min_val=val
min_val=val
print(min_val)
print(min_val)
max_val=None
max_val=None
forvalinsome_list:
forvalinsome_list:
ifmax_val==Noneorval>max_val:
ifmax_val==Noneorval>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
defmedian(some_items):
defmedian(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/
defprocess_csv(filename):
defprocess_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
returnexample_data
returnexample_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
```
```
%%Celltype:markdownid:5d298a4ctags:
%%Celltype:markdownid:5d298a4ctags:
### What is the Lecture of the 4th student?
### What is the Lecture of the 4th student?
%%Celltype:codeid:3617b3detags:
%%Celltype:codeid:3617b3detags:
``` 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