Skip to content

Commit 2470600

Browse files
Merge branch 'main' into zach-lab11-contact-list
2 parents 75061c3 + d4b1665 commit 2470600

29 files changed

+1758
-9
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Reader:
2+
3+
def __init__(self, book_file):
4+
self.book_file = book_file
5+
self.text = ''
6+
7+
def load(self):
8+
"""
9+
1) open 'file' with option 'r' for read
10+
2) get the text from the file
11+
"""
12+
with open(self.book_file, "r") as f_read:
13+
self.text = f_read.read()
14+
15+
return self.text
16+
17+
def edit(self, new_text):
18+
"""
19+
1) open file with open 'w' for write
20+
2) take in additional text & concat onto existing file
21+
3) save to the file
22+
"""
23+
with open(self.book_file, "a") as f_write:
24+
f_write.write(new_text)
25+
26+
def replace(self, new_text):
27+
with open(self.book_file, "w") as f_write:
28+
f_write.write(new_text)
29+
30+
def print(self):
31+
"""
32+
print the text
33+
"""
34+
35+
36+
def main():
37+
text = Reader('/Users/sb/Desktop/class_HB2/1 Python/docs/class_code_examples/fileIO/book.txt') # create an instance of our class
38+
text.load()
39+
while True:
40+
command = input('Enter a command: ')
41+
if command == 'save':
42+
new_text = input('add new text: ')
43+
text.save(new_text)
44+
elif command == 'print':
45+
print('heeey')
46+
else:
47+
print('Command not recognized')
48+
49+
50+
if __name__ == "__main__":
51+
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
David 5551234
2+
Alice 6662345
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name,phone number,state,zipe code
2+
Sarah Beth,123432112,VA,22906
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# Lab: LCR Simulator
3+
4+
LCR is a dice game, one of pure chance. Therefore, we can write a simulator to avoid wasting the time playing it ourselves. Description from [wikipedia](https://en.wikipedia.org/wiki/LCR_(dice_game)):
5+
6+
7+
> Each player receives at least 3 chips. Players take it in turn to roll three six-sided dice, each of which is marked with "L", "C", "R" on one side, and a single dot on the three remaining sides. For each "L" or "R" thrown, the player must pass one chip to the player to his left or right, respectively. A "C" indicates a chip to the center (pot). A dot has no effect.
8+
9+
>If a player has fewer than three chips left, he/she is still in the game but his number of chips is the number of dice he/she rolls on his/her turn, rather than rolling all three. When a player has zero chips, he/she passes the dice on his turn, but may receive chips from others and take his next turn accordingly. The winner is the last player with chips left. He/she does not roll the dice, and wins the center pot. If he/she chooses to play another round, he/she does not roll 3, he/she keeps his pot and plays with that.
10+
11+
12+
When the program starts, it should ask for the names of the players, until the user enters 'done'. Then it should run the simulation, tell the user who won, and ask the player whether they'd like to play again.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
# Lab: Arbitrary Precision Arithmetic
3+
4+
[Arbitrary Precision Arithmetic](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic) is a way of performing arithmetic with greater precision than that offered by ordinary ints and floats. One python library for this is [mpmath](http://mpmath.org/).
5+
6+
## Addition
7+
8+
Remember doing [long addition](https://en.wikipedia.org/wiki/Elementary_arithmetic#Example) in elementary school? You could represent an 'big int' as a list of ints. To add them, loop over the digits from 'right' to 'left', keeping track of the 'carry'. Also be wary of numbers with differing number of digits. Prompt the user for each number and print out their sum.
9+
10+
```
11+
What is the first number? 23422340923410340239482304
12+
What is the second number? 303003025050020203492
13+
23422340923410340239482304 + 303003025050020203492 = 23422643926435390259685796
14+
```
15+
16+
17+
## Multiplication
18+
19+
A simple (but inefficient) way of implementing multiplication is through repeated addition. For example, 564*6 = 564+564+564+564+564+564. A more efficient way would be [long multiplication](http://mathworld.wolfram.com/LongMultiplication.html).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# Crime Data
3+
4+
Open one of the crime data csv's in the data folder, and derive the following statistics.
5+
6+
- The specific most common crime type.
7+
- The rarest crimes.
8+
- The year with the most crime.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Lab: Road Trip
2+
3+
We've mapped what cities are directly connected by roads and stored them in a dictionary:
4+
5+
```python
6+
city_to_accessible_cities = {
7+
'Boston': {'New York', 'Albany', 'Portland'},
8+
'New York': {'Boston', 'Albany', 'Philadelphia'},
9+
'Albany': {'Boston', 'New York', 'Portland'},
10+
'Portland': {'Boston', 'Albany'},
11+
'Philadelphia': {'New York'}
12+
}
13+
```
14+
15+
Traveling from one city to an adjacent one is called a **hop**. Let the user enter a city.
16+
Print out all the cities connected to that city. Then print out all cities that could be reached through two hops.
17+
18+
19+
20+
## Version 2 (optional)
21+
22+
Let the user enter a city and a number of hops. Print out all cities that could be reached through that number of hops.
23+
24+
We've also mapped the travel time of each hop. When the user enters a city and a number of hops, print out the shortest travel times to each city. Paths with fewer hops are not necessarily those with the shorter travel times.
25+
26+
```python
27+
city_to_accessible_cities_with_travel_time = {
28+
'Boston': {'New York': 4, 'Albany': 6, 'Portland': 3},
29+
'New York': {'Boston': 4, 'Albany': 5, 'Philadelphia': 9},
30+
'Albany': {'Boston': 6, 'New York': 5, 'Portland': 7},
31+
'Portland': {'Boston': 3, 'Albany': 7},
32+
'Philadelphia': {'New York': 9},
33+
}
34+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# Lab: Sock Sorter
3+
4+
You've just finished laundry and your expansive sock collection is in complete disorder. Sort your socks into pairs and pull out any loners.
5+
6+
1) Generate a list of 100 random socks, randomly chosen from a set of types: `sock_types = ['ankle', 'crew', 'calf', 'thigh']`
7+
8+
2) Find the number of duplicates and loners for each sock type. Hint: dictionaries are helpful here.
9+
10+
11+
## Version 2
12+
13+
Now you have a mix of types **and** colors. Represent socks using tuples containing one color and one type `('black', 'crew')`. Randomly generate these, and then group them into pairs.
14+
15+
`sock_colors = ['black', 'white', 'blue']`
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
# Lab: Tree
3+
4+
A tree is a type of data structure, one that represents a hierarchy. Try running the code below and implement the following functions.
5+
6+
- `count_leaves(node)` returns the number of leaves
7+
- `rename_leaves(node, to_find, to_replace)` finds leaves with the name `to_find` and replaces their name with `to_replace`.
8+
9+
10+
11+
```python
12+
import random
13+
14+
names = ['virginia', 'christine', 'carl', 'lillian']
15+
16+
17+
def generate_tree(depth):
18+
n_children = int(random.random()*10/depth)
19+
if n_children == 0:
20+
return {'type': 'leaf', 'name': random.choice(names)}
21+
branch = {'type': 'branch', 'children': []}
22+
for i in range(n_children):
23+
child = generate_tree(depth+1)
24+
branch['children'].append(child)
25+
return branch
26+
27+
28+
def print_node(node, indentation):
29+
if node['type'] == 'leaf':
30+
print(indentation + node['name'])
31+
else:
32+
print(indentation + '-')
33+
for i in range(len(node['children'])):
34+
print_node(node['children'][i], indentation + '\t')
35+
36+
37+
# count all trees and branches
38+
def count_nodes(node):
39+
if node['type'] == 'leaf':
40+
return 1
41+
r = 1
42+
for i in range(len(node['children'])):
43+
r += count_nodes(node['children'][i])
44+
return r
45+
46+
47+
root = generate_tree(1)
48+
print_node(root, '')
49+
print(count_nodes(root))
50+
```
51+
52+
53+
54+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
3+
# Practice 1: Fundamentals
4+
5+
For each practice problem, write a function that returns a value (not just prints it). You can then call the function a couple times to test it, comment those calls out, and move on to the next problem. An example of this format is given below.
6+
7+
```python
8+
def add(a, b):
9+
return a + b
10+
#print(add(5, 2))
11+
#print(add(8, 1))
12+
```
13+
14+
## Problem 1
15+
16+
Write a function that tells whether a number is even or odd (hint, compare a/2 and a//2, or use a%2)
17+
18+
```python
19+
def is_even(a):
20+
...
21+
is_even(5) → False
22+
is_even(6) → True
23+
```
24+
25+
26+
## Problem 2
27+
28+
Write a function that takes two ints, a and b, and returns True if one is positive and the other is negative.
29+
30+
```python
31+
def opposite(a, b):
32+
...
33+
opposite(10, -1) → True
34+
opposite(2, 3) → False
35+
opposite(-1, -1) → False
36+
```
37+
38+
## Problem 3
39+
40+
Write a function that returns True if a number within 10 of 100.
41+
42+
```python
43+
def near_100(num):
44+
...
45+
near_100(50) → False
46+
near_100(99) → True
47+
near_100(105) → True
48+
```
49+
50+
## Problem 4
51+
52+
Write a function that returns the maximum of 3 parameters.
53+
54+
```python
55+
def maximum_of_three(a, b, c):
56+
...
57+
maximum_of_three(5,6,2) → 6
58+
maximum_of_three(-4,3,10) → 10
59+
```
60+
61+
## Problem 5
62+
63+
Print out the powers of 2 from 2^0 to 2^20
64+
65+
`1, 2, 4, 8, 16, 32, ...`
66+
67+
68+
69+

0 commit comments

Comments
 (0)