Skip to content

Commit 9ddaccd

Browse files
committed
updated part11
1 parent 69a4201 commit 9ddaccd

11 files changed

+244
-0
lines changed

part11/07_lottery_numbers.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
class LotteryNumbers:
3+
def __init__(self, week_number: int, seven_numbers: list):
4+
self.week_number = week_number
5+
self.seven_numbers = seven_numbers
6+
7+
def number_of_hits(self, numbers: list):
8+
return sum([1 for n in numbers if n in self.seven_numbers])
9+
10+
def hits_in_place(self, numbers: list):
11+
return [i if i in self.seven_numbers else -1 for i in numbers]

part11/08_filter_forbidden.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
def filter_forbidden(string: str, forbidden: str):
3+
return ''.join([c for c in string if c not in forbidden])
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
class ShoppingList:
3+
def __init__(self):
4+
self.products = []
5+
6+
def number_of_items(self):
7+
return len(self.products)
8+
9+
def add(self, product: str, number: int):
10+
self.products.append((product, number))
11+
12+
def __iter__(self):
13+
self.n = 0
14+
return self
15+
16+
def __next__(self):
17+
if self.n < len(self.products):
18+
product = self.products[self.n]
19+
self.n += 1
20+
return product
21+
else:
22+
raise StopIteration
23+
24+
25+
def products_in_shopping_list(shopping_list: ShoppingList, amount: int):
26+
return [name[0] for name in shopping_list if name[1] >= amount]
27+
28+
29+
if __name__ == '__main__':
30+
my_list = ShoppingList()
31+
my_list.add("bananas", 10)
32+
my_list.add("apples", 5)
33+
my_list.add("alcohol free beer", 24)
34+
my_list.add("pineapple", 1)
35+
36+
print("the shopping list contains at least 8 of the following items:")
37+
for product in products_in_shopping_list(my_list, 8):
38+
print(product)

part11/10_cheaper_properties.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
class RealProperty:
3+
def __init__(self, rooms: int , square_meters: int, price_per_sqm: int, description: str):
4+
self.rooms = rooms
5+
self.square_meters = square_meters
6+
self.price_per_sqm = price_per_sqm
7+
self.description = description
8+
9+
def bigger(self, compared_to):
10+
return self.square_meters > compared_to.square_meters
11+
12+
def price_difference(self, compared_to):
13+
# Function abs returns absolute value
14+
difference = abs((self.price_per_sqm * self.square_meters) - (compared_to.price_per_sqm * compared_to.square_meters))
15+
return difference
16+
17+
def more_expensive(self, compared_to):
18+
difference = (self.price_per_sqm * self.square_meters) - (compared_to.price_per_sqm * compared_to.square_meters)
19+
return difference > 0
20+
21+
def __repr__(self):
22+
return (f'RealProperty(rooms = {self.rooms}, square_meters = {self.square_meters}, ' +
23+
f'price_per_sqm = {self.price_per_sqm}, description = {self.description})')
24+
25+
26+
def cheaper_properties(properties: list, reference: RealProperty):
27+
return [(property, property.price_difference(reference)) for property in properties if not property.more_expensive(reference) and property.price_difference(reference) != 0]
28+
29+
30+
if __name__ == '__main__':
31+
a1 = RealProperty(1, 16, 5500, "Central studio")
32+
a2 = RealProperty(2, 38, 4200, "Two bedrooms downtown")
33+
a3 = RealProperty(3, 78, 2500, "Three bedrooms in the suburbs")
34+
a4 = RealProperty(6, 215, 500, "Farm in the middle of nowhere")
35+
a5 = RealProperty(4, 105, 1700, "Loft in a small town")
36+
a6 = RealProperty(25, 1200, 2500, "Countryside mansion")
37+
38+
properties = [a1, a2, a3, a4, a5, a6]
39+
40+
print(f"cheaper options when compared to {a3.description}:")
41+
for item in cheaper_properties(properties, a3):
42+
print(f"{item[0].description:35} price difference {item[1]} euros")

part11/11_lengths_of_strings.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def lengths(strings: list):
2+
return {string : len(string) for string in strings}

part11/12_most_common_words.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import string
2+
3+
4+
def most_common_words(filename: str, lower_limit: int):
5+
data = open(filename).read().replace('\n', ' ').replace('.', '').replace(',', ' ').replace(' ', ' ')
6+
7+
words = data.split(' ')
8+
9+
return {word : words.count(word) for word in words if words.count(word) >= lower_limit}
10+
11+
12+
if __name__ == '__main__':
13+
print(most_common_words('programming.txt', 4))

part11/13_add_numbers_to_list.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
def add_numbers_to_list(numbers: list):
3+
while len(numbers) % 5 != 0:
4+
numbers.append(numbers[-1] + 1)
5+
add_numbers_to_list(numbers)
6+
7+
8+
if __name__ == '__main__':
9+
numbers = [1, 2]
10+
add_numbers_to_list(numbers)
11+
print(numbers)
12+
13+

part11/14_recursive_sum.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
def recursive_sum(number: int):
3+
# if the number is 1, there is nothing else to add
4+
if number <= 1:
5+
return number
6+
return number + recursive_sum(number - 1)

part11/15_balanced_brackets.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
def balanced_brackets(my_string: str):
3+
new_str = ''
4+
for char in my_string:
5+
if char in '[]()':
6+
new_str += char
7+
if len(new_str) == 1:
8+
return False
9+
elif len(new_str) == 0:
10+
return True
11+
if new_str[0] == '(':
12+
if not new_str[-1] == ')':
13+
return False
14+
elif new_str[0] == '[':
15+
if not new_str[-1] == ']':
16+
return False
17+
18+
# remove first and last character
19+
return balanced_brackets(new_str[1:-1])
20+
21+
22+
if __name__ == '__main__':
23+
ok = balanced_brackets("([([])])")
24+
print(ok)
25+
26+
ok = balanced_brackets("(python version [3.7]) please use this one!")
27+
print(ok)
28+
29+
# this is no good, the closing bracket doesn't match
30+
ok = balanced_brackets("(()]")
31+
print(ok)
32+
33+
# different types of brackets are mismatched
34+
ok = balanced_brackets("([bad egg)]")
35+
print(ok)
36+
37+
ok = balanced_brackets("(x)y)")
38+
print(ok)

part11/16_greatest_node.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
class Node:
3+
""" Class is modeling single node in binary tree """
4+
def __init__(self, value, left_child: 'Node' = None, right_child: 'Node' = None):
5+
self.value = value
6+
self.left_child = left_child
7+
self.right_child = right_child
8+
9+
10+
def greatest_node(root: Node):
11+
# Initialize the greatest value with the value of the current node
12+
greatest = root.value
13+
14+
# Base case: If the node has no children, return its value
15+
if root.left_child is None and root.right_child is None:
16+
return root.value
17+
18+
# If the node has a left child, find the greatest value in the left subtree
19+
if root.left_child is not None:
20+
left_max = greatest_node(root.left_child)
21+
# Update the greatest value if a greater value is found in the left subtree
22+
greatest = max(greatest, left_max)
23+
24+
# If the node has a right child, find the greatest value in the right subtree
25+
if root.right_child is not None:
26+
right_max = greatest_node(root.right_child)
27+
# Update the greatest value if a greater value is found in the right subtree
28+
greatest = max(greatest, right_max)
29+
30+
return greatest
31+
32+
33+
if __name__ == "__main__":
34+
tree = Node(2)
35+
36+
tree.left_child = Node(3)
37+
tree.left_child.left_child = Node(5)
38+
tree.left_child.right_child = Node(8)
39+
40+
tree.right_child = Node(4)
41+
tree.right_child.right_child = Node(11)
42+
43+
print(greatest_node(tree))

part11/17_bosses_and_subordinates.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
class Employee:
3+
def __init__(self, name: str):
4+
self.name = name
5+
self.subordinates = []
6+
7+
def add_subordinate(self, employee: 'Employee'):
8+
self.subordinates.append(employee)
9+
10+
11+
def count_subordinates(employee: Employee):
12+
value = len(employee.subordinates)
13+
14+
if employee.subordinates != []:
15+
for sub in employee.subordinates:
16+
value += count_subordinates(sub)
17+
18+
return value
19+
20+
21+
if __name__ == '__main__':
22+
t1 = Employee("Sally")
23+
t2 = Employee("Eric")
24+
t3 = Employee("Matthew")
25+
t4 = Employee("Emily")
26+
t5 = Employee("Adele")
27+
t6 = Employee("Claire")
28+
t1.add_subordinate(t4)
29+
t1.add_subordinate(t6)
30+
t4.add_subordinate(t2)
31+
t4.add_subordinate(t3)
32+
t4.add_subordinate(t5)
33+
print(count_subordinates(t1))
34+
print(count_subordinates(t4))
35+
print(count_subordinates(t5))

0 commit comments

Comments
 (0)