Skip to content

Commit 2276737

Browse files
authored
Add files via upload
1 parent 3595542 commit 2276737

25 files changed

Lines changed: 1050 additions & 0 deletions

lesson20/args.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def my_sum(*numbers):
2+
s = 0
3+
for number in numbers:
4+
s += int(number)
5+
return s

lesson20/assert.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def my_sum(sth):
2+
s = 0
3+
for item in sth:
4+
s += item
5+
return s
6+
7+
8+
assert my_sum((1,2,3)) == 6
9+
assert my_sum([1,2,3]) == 6
10+
assert my_sum({1,2,3}) == 6

lesson20/assert2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def my_avg(sth):
2+
assert len(sth)!=0, "iterable is empty!"
3+
s = 0
4+
for item in sth:
5+
s += item
6+
return s/len(sth)
7+
8+
print(my_avg(()))
9+

lesson20/comment.ret.value.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def full_name(first_name, last_name) -> str:
2+
return f"{first_name} {last_name}"
3+
4+
print(full_name("John", "Wick"))

lesson20/documentation.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# source: https://realpython.com/documenting-python-code/
2+
3+
class Animal:
4+
"""
5+
A class used to represent an Animal
6+
7+
...
8+
9+
Attributes
10+
----------
11+
says_str : str
12+
a formatted string to print out what the animal says
13+
name : str
14+
the name of the animal
15+
sound : str
16+
the sound that the animal makes
17+
num_legs : int
18+
the number of legs the animal has (default 4)
19+
20+
Methods
21+
-------
22+
says(sound=None)
23+
Prints the animals name and what sound it makes
24+
"""
25+
26+
says_str = "A {name} says {sound}"
27+
28+
def __init__(self, name, sound, num_legs=4):
29+
"""
30+
Parameters
31+
----------
32+
name : str
33+
The name of the animal
34+
sound : str
35+
The sound the animal makes
36+
num_legs : int, optional
37+
The number of legs the animal (default is 4)
38+
"""
39+
40+
self.name = name
41+
self.sound = sound
42+
self.num_legs = num_legs
43+
44+
def says(self, sound=None):
45+
"""Prints what the animals name is and what sound it makes.
46+
47+
If the argument `sound` isn't passed in, the default Animal
48+
sound is used.
49+
50+
Parameters
51+
----------
52+
sound : str, optional
53+
The sound the animal makes (default is None)
54+
55+
Raises
56+
------
57+
NotImplementedError
58+
If no sound is set for the animal or passed in as a
59+
parameter.
60+
"""
61+
62+
if self.sound is None and sound is None:
63+
raise NotImplementedError("Silent Animals are not supported!")
64+
65+
out_sound = self.sound if sound is None else sound
66+
print(self.says_str.format(name=self.name, sound=out_sound))

lesson20/exercise01.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from waiter import Waiter, Barista
3+
4+
5+
class WaiterTestCase(unittest.TestCase):
6+
def setUp(self):
7+
self.baristaObj = Barista("Bob", 10000)
8+
9+
def test_init(self):
10+
w = Waiter("Tom", 5000)
11+
self.assertEqual(w.full_name, "Tom")
12+
self.assertEqual(w.salary, 5000)
13+
self.assertEqual(w.served_cnt, 0)
14+
15+
def test_serve(self):
16+
w = Waiter("Tom", 5000)
17+
w.serve(50, self.baristaObj)
18+
self.assertEqual(w.served_cnt, 50)

lesson20/exercise02.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from graph import Graph
2+
from queue import Queue
3+
4+
5+
def breadth_first_search(graph, start, finish):
6+
q = Queue()
7+
discovered = [start]
8+
q.enqueue(start)
9+
parent = {}
10+
while len(q) > 0:
11+
v = q.dequeue()
12+
if v == finish:
13+
break
14+
for neighbor in graph.neighbors(v):
15+
if neighbor.descr not in discovered:
16+
discovered += [neighbor.descr]
17+
parent[neighbor.descr] = v
18+
q.enqueue(neighbor.descr)
19+
20+
path = [finish]
21+
while path[0] != start:
22+
path = [parent[path[0]]] + path
23+
24+
print(path)
25+
26+
27+
def main():
28+
facebook_users = Graph()
29+
for user in ["Bob", "Anne", "Elisa", "Diana", "Carl"]:
30+
facebook_users.add_vertex(user)
31+
32+
facebook_users.add_edge("Carl", "Bob")
33+
facebook_users.add_edge("Carl", "Elisa")
34+
facebook_users.add_edge("Carl", "Diana")
35+
facebook_users.add_edge("Diana", "Bob")
36+
facebook_users.add_edge("Diana", "Anne")
37+
facebook_users.add_edge("Elisa", "Anne")
38+
facebook_users.add_edge("Anne", "Bob")
39+
print(facebook_users)
40+
print("\n")
41+
breadth_first_search(facebook_users, "Carl", "Anne")
42+
43+
44+
main()

lesson20/graph.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Node:
2+
def __init__(self, descr="", neighbors=None):
3+
self.descr = descr
4+
if neighbors is None:
5+
self.neighbors = []
6+
else:
7+
self.neighbors = neighbors
8+
9+
10+
class Graph:
11+
def __init__(self):
12+
self.nodes = []
13+
14+
def add_vertex(self, descr="", neighbors=None):
15+
if neighbors is None:
16+
neighbors = []
17+
self.nodes += [Node(descr, neighbors)]
18+
19+
def __index_of(self, descr):
20+
for i in range(len(self.nodes)):
21+
if descr == self.nodes[i].descr:
22+
return i
23+
24+
def add_edge(self, descr1, descr2):
25+
index1 = self.__index_of(descr1)
26+
index2 = self.__index_of(descr2)
27+
self.nodes[index1].neighbors += [self.nodes[index2]]
28+
self.nodes[index2].neighbors += [self.nodes[index1]]
29+
30+
def neighbors(self, descr):
31+
return self.nodes[self.__index_of(descr)].neighbors
32+
33+
def __str__(self):
34+
st = ""
35+
for node in self.nodes:
36+
st += f"\n{node.descr}: "
37+
for neighbor in node.neighbors:
38+
st += f" {neighbor.descr}"
39+
40+
return st

0 commit comments

Comments
 (0)