Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs220-lecture-material
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CDIS
Computer Sciences
Courses
CS220
cs220-lecture-material
Commits
78071a8a
Commit
78071a8a
authored
1 year ago
by
Ashwin Maran
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
b44c7e1b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
s24/AmFam_Ashwin/16_List_Practice/Lec_16_List_Practice_Worksheet.ipynb
+261
-0
261 additions, 0 deletions
...win/16_List_Practice/Lec_16_List_Practice_Worksheet.ipynb
with
261 additions
and
0 deletions
s24/AmFam_Ashwin/16_List_Practice/Lec_16_List_Practice_Worksheet.ipynb
0 → 100644
+
261
−
0
View file @
78071a8a
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lecture 16 Worksheet Solutions\n",
"\n",
"You should do the worksheet by hand, then check your work."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 1:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"nums = [100, 2, 3, 40, 99]\n",
"words = [\"three\", \"two\", \"one\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Solutions:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"99\n",
"[2, 3]\n",
"two\n",
"w\n",
"www\n",
"\n",
"1\n",
"2\n",
"[100, 'three']\n",
"three,two,one\n",
"e,t\n"
]
}
],
"source": [
"print(nums[-1])\n",
"print(nums[1:3])\n",
"print(words[1])\n",
"print(words[1][1])\n",
"print(words[1][-2] * nums[2])\n",
"print()\n",
"print(words.index(\"two\"))\n",
"print(nums[words.index(\"two\")])\n",
"print(nums[:1] + words[:1])\n",
"print(\",\".join(words))\n",
"print((\",\".join(words))[4:7])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 2:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"rows = [[\"x\", \"y\",\"name\"], [3,4,\"Alice\"], [9,1,\"Bob\"], [-3,4,\"Cindy\"]]\n",
"header = rows[0]\n",
"data = rows[1:]\n",
"X = 0\n",
"Y = 1\n",
"NAME = 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Solutions:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"3\n",
"3\n",
"Alice\n",
"Bob\n",
"\n",
"2\n",
"Cindy\n",
"3.0\n",
"5.0\n",
"Alice\n"
]
}
],
"source": [
"print(len(rows))\n",
"print(len(data))\n",
"print(len(header))\n",
"print(rows[1][-1])\n",
"print(data[1][-1])\n",
"print()\n",
"print(header.index(\"name\"))\n",
"print(data[-1][header.index(\"name\")])\n",
"print((data[0][X] + data[1][X] + data[2][X]) / 3)\n",
"print((data[-1][X] ** 2 + data[-1][Y] ** 2) ** 0.5)\n",
"print(min(data[0][NAME], data[1][NAME], data[2][NAME]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 3:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"rows = [ [\"Food Science\", \"24000\", \"0.049188446\", \"62000\"],\n",
" [\"CS\", \"783000\", \"0.049518657\", \"78000\"],\n",
" [\"Microbiology\", \"70000\", \"0.050880749\", \"60000\"],\n",
" [\"Math\", \"433000\", \"0.05293608\", \"66000\"] ]\n",
"hd = [\"major\", \"students\", \"unemployed\", \"salary\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Solutions:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CS\n",
"433000\n",
"True\n",
"2400070000\n"
]
}
],
"source": [
"print(rows[1][0])\n",
"print(rows[3][hd.index(\"students\")])\n",
"print(len(hd) == len(rows[1]))\n",
"print(rows[0][1] + rows[2][1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 4:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"rows = [ [\"city\", \"state\", \"y14\", \"y15\"],\n",
" [\"Chicago\", \"Illinois\", \"411\", \"478\"],\n",
" [\"Milwaukee\", \"Wisconsin\", \"90\", \"145\"],\n",
" [\"Detroit\", \"Michigan\", \"298\", \"295\"] ]\n",
"hd = rows[0]\n",
"rows = rows[1:] # this removes the header and stores the result in rows"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Solutions:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Chicago\n",
"411\n",
"False\n",
"Detroit, Michigan\n"
]
}
],
"source": [
"print(rows[0][hd.index(\"city\")])\n",
"print(rows[0][hd.index(\"y14\")])\n",
"print(rows[2][hd.index(\"y14\")] < rows[2][hd.index(\"y15\")])\n",
"print(\", \".join(rows[-1][:2]))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% Cell type:markdown id: tags:
# Lecture 16 Worksheet Solutions
You should do the worksheet by hand, then check your work.
%% Cell type:markdown id: tags:
## Problem 1:
%% Cell type:code id: tags:
```
python
nums
=
[
100
,
2
,
3
,
40
,
99
]
words
=
[
"
three
"
,
"
two
"
,
"
one
"
]
```
%% Cell type:markdown id: tags:
#### Solutions:
%% Cell type:code id: tags:
```
python
print
(
nums
[
-
1
])
print
(
nums
[
1
:
3
])
print
(
words
[
1
])
print
(
words
[
1
][
1
])
print
(
words
[
1
][
-
2
]
*
nums
[
2
])
print
()
print
(
words
.
index
(
"
two
"
))
print
(
nums
[
words
.
index
(
"
two
"
)])
print
(
nums
[:
1
]
+
words
[:
1
])
print
(
"
,
"
.
join
(
words
))
print
((
"
,
"
.
join
(
words
))[
4
:
7
])
```
%% Output
99
[2, 3]
two
w
www
1
2
[100, 'three']
three,two,one
e,t
%% Cell type:markdown id: tags:
## Problem 2:
%% Cell type:code id: tags:
```
python
rows
=
[[
"
x
"
,
"
y
"
,
"
name
"
],
[
3
,
4
,
"
Alice
"
],
[
9
,
1
,
"
Bob
"
],
[
-
3
,
4
,
"
Cindy
"
]]
header
=
rows
[
0
]
data
=
rows
[
1
:]
X
=
0
Y
=
1
NAME
=
2
```
%% Cell type:markdown id: tags:
#### Solutions:
%% Cell type:code id: tags:
```
python
print
(
len
(
rows
))
print
(
len
(
data
))
print
(
len
(
header
))
print
(
rows
[
1
][
-
1
])
print
(
data
[
1
][
-
1
])
print
()
print
(
header
.
index
(
"
name
"
))
print
(
data
[
-
1
][
header
.
index
(
"
name
"
)])
print
((
data
[
0
][
X
]
+
data
[
1
][
X
]
+
data
[
2
][
X
])
/
3
)
print
((
data
[
-
1
][
X
]
**
2
+
data
[
-
1
][
Y
]
**
2
)
**
0.5
)
print
(
min
(
data
[
0
][
NAME
],
data
[
1
][
NAME
],
data
[
2
][
NAME
]))
```
%% Output
4
3
3
Alice
Bob
2
Cindy
3.0
5.0
Alice
%% Cell type:markdown id: tags:
## Problem 3:
%% Cell type:code id: tags:
```
python
rows
=
[
[
"
Food Science
"
,
"
24000
"
,
"
0.049188446
"
,
"
62000
"
],
[
"
CS
"
,
"
783000
"
,
"
0.049518657
"
,
"
78000
"
],
[
"
Microbiology
"
,
"
70000
"
,
"
0.050880749
"
,
"
60000
"
],
[
"
Math
"
,
"
433000
"
,
"
0.05293608
"
,
"
66000
"
]
]
hd
=
[
"
major
"
,
"
students
"
,
"
unemployed
"
,
"
salary
"
]
```
%% Cell type:markdown id: tags:
#### Solutions:
%% Cell type:code id: tags:
```
python
print
(
rows
[
1
][
0
])
print
(
rows
[
3
][
hd
.
index
(
"
students
"
)])
print
(
len
(
hd
)
==
len
(
rows
[
1
]))
print
(
rows
[
0
][
1
]
+
rows
[
2
][
1
])
```
%% Output
CS
433000
True
2400070000
%% Cell type:markdown id: tags:
## Problem 4:
%% Cell type:code id: tags:
```
python
rows
=
[
[
"
city
"
,
"
state
"
,
"
y14
"
,
"
y15
"
],
[
"
Chicago
"
,
"
Illinois
"
,
"
411
"
,
"
478
"
],
[
"
Milwaukee
"
,
"
Wisconsin
"
,
"
90
"
,
"
145
"
],
[
"
Detroit
"
,
"
Michigan
"
,
"
298
"
,
"
295
"
]
]
hd
=
rows
[
0
]
rows
=
rows
[
1
:]
# this removes the header and stores the result in rows
```
%% Cell type:markdown id: tags:
#### Solutions:
%% Cell type:code id: tags:
```
python
print
(
rows
[
0
][
hd
.
index
(
"
city
"
)])
print
(
rows
[
0
][
hd
.
index
(
"
y14
"
)])
print
(
rows
[
2
][
hd
.
index
(
"
y14
"
)]
<
rows
[
2
][
hd
.
index
(
"
y15
"
)])
print
(
"
,
"
.
join
(
rows
[
-
1
][:
2
]))
```
%% Output
Chicago
411
False
Detroit, Michigan
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment