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

Upload New File

parent b44c7e1b
No related branches found
No related tags found
No related merge requests found
%% 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
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