Skip to content

Commit d618ca7

Browse files
author
Shehab Abdel-Salam
committed
Finalise remaining exercises
1 parent 723d59a commit d618ca7

File tree

11 files changed

+52
-87
lines changed

11 files changed

+52
-87
lines changed

chapters/chapter08_files/exercises/exercise_60.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313

1414

1515
def course_grades_summary(file_name: str) -> list[tuple[str, float]]:
16-
with open(file_name) as file:
17-
summary = []
18-
for line in file:
19-
course, *grades = line.strip().split(":")
20-
grades = [int(grade) for grade in grades[0].split(",")]
21-
average = sum(grades) / len(grades)
22-
summary.append((course, average))
23-
return summary
16+
# Your code should go here.
17+
18+
...

chapters/chapter08_files/exercises/exercise_61.py

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,6 @@
2424
def inventory_management(
2525
file_name: str, restock_threshold: int
2626
) -> dict[str, list[dict[str, str]]]:
27-
total_inventory_value = 0.0
28-
restock_alerts = []
27+
# Your code should go here.
2928

30-
with open(file_name, "r") as file:
31-
# Read the header line
32-
header = file.readline().strip().split(",")
33-
34-
# Check if header is correct
35-
if header != ["item_name", "quantity", "price"]:
36-
raise ValueError("Incorrect CSV format")
37-
38-
for line in file:
39-
# Split each line into columns
40-
columns = line.strip().split(",")
41-
42-
# Extract data from columns
43-
item_name = columns[0]
44-
quantity = int(columns[1])
45-
price = float(columns[2])
46-
47-
# Calculate the total inventory value
48-
total_inventory_value += quantity * price
49-
50-
# Check if restock is needed
51-
if quantity < restock_threshold:
52-
restock_alerts.append(item_name)
53-
54-
return {
55-
"total_inventory_value": total_inventory_value,
56-
"restock_alerts": restock_alerts,
57-
}
29+
...

chapters/chapter08_files/exercises/exercise_62.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717

1818

1919
def is_file_sorted(file_name: str) -> bool:
20-
numbers = []
21-
with open(file_name) as file:
22-
for line in file:
23-
if not line.strip().isdigit():
24-
continue
25-
numbers.append(int(line))
26-
return numbers == sorted(numbers)
20+
# Your code should go here.
21+
22+
...

chapters/chapter10_oop/exercises/exercise_69.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ def __init__(self):
1919
self.income = 0
2020

2121
def calculate_tax(self, income: int) -> float:
22-
self.income = income
23-
if self.income <= 10000:
24-
return 0
25-
elif self.income <= 50000:
26-
return self.income * 0.1
27-
elif self.income <= 100000:
28-
return self.income * 0.2
29-
elif self.income > 100000:
30-
return self.income * 0.3
31-
else:
32-
return 0
22+
# Your code should go here.
23+
24+
...

chapters/chapter10_oop/exercises/exercise_74.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ class Card:
3737
class Account:
3838
# Your code should go here.
3939
...
40+
41+
42+
@dataclass
43+
class Customer:
44+
# Your code should go here.
45+
...
Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# Exercise 78 - Simple University System
2-
# Given the following
2+
# Given the following class definitions.
3+
# Implement the `Student` and `Professor` classes to inherit from the `Person` class.
4+
35
from __future__ import annotations
46

57

68
class Course:
79
def __init__(self, course_name: str, course_code: str):
810
self.course_name = course_name
911
self.course_code = course_code
10-
self.professor = None
11-
self.students = []
12+
self.professor: Professor | None = None
13+
self.students: list[Student] = []
1214

1315
def add_student(self, student: Student):
1416
self.students.append(student)
@@ -26,29 +28,32 @@ def __init__(self, name: str, email: str):
2628
self.email = email
2729

2830

29-
class Student(Person):
31+
# Implement the `Student` and `Professor` classes to inherit from the `Person` class.
32+
class Student:
3033
def __init__(self, name: str, email: str, student_id: str):
3134
super().__init__(name, email)
3235
self.student_id = student_id
3336
self.enrolled_courses: list[Course] = []
3437

35-
def enroll(self, course: Course):
36-
self.enrolled_courses.append(course)
37-
course.add_student(self)
38+
def enroll(self, course: Course) -> None:
39+
# Your code should go here.
40+
...
3841

39-
def list_courses(self):
40-
return [course.course_name for course in self.enrolled_courses]
42+
def list_courses(self) -> list[str]:
43+
# Your code should go here.
44+
...
4145

4246

43-
class Professor(Person):
47+
class Professor:
4448
def __init__(self, name: str, email: str, professor_id: str):
4549
super().__init__(name, email)
4650
self.professor_id = professor_id
4751
self.taught_courses: list[Course] = []
4852

49-
def assign_course(self, course: Course):
50-
self.taught_courses.append(course)
51-
course.assign_professor(self)
53+
def assign_course(self, course: Course) -> None:
54+
# Your code should go here.
55+
...
5256

53-
def list_courses(self):
54-
return [course.course_name for course in self.taught_courses]
57+
def list_courses(self) -> list[str]:
58+
# Your code should go here.
59+
...

chapters/chapter11_standard_library/exercises/exercise_81.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Exercise 81 - Functools
22
# Write a function that uses `functools.partial` to create a function that always multiplies by 10.
3+
from functools import partial # noqa: F401
34

45

56
def multiply(x, y):

chapters/chapter11_standard_library/exercises/exercise_82.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Exercise 82 - Flatten a List of Lists
22
# Write a function that flattens a list of lists using `itertools.chain`.
33
# The function should take a list of lists and return a single flattened list.
4-
from itertools import chain
4+
from itertools import chain # noqa: F401
55

66
# Example:
77
# flatten_list_of_lists([[1, 2, 3], [4, 5], [6, 7, 8, 9]]) == [1, 2, 3, 4, 5, 6, 7, 8, 9]

chapters/chapter11_standard_library/exercises/exercise_83.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Exercise 83 - Cached Fibonacci
22
# Given this fibonacci function, we want to cache the results using `functools.lru`.
3-
from functools import lru_cache
3+
from functools import lru_cache # noqa: F401
44

55

66
def compute_fibonacci(n: int) -> int:

chapters/chapter15_concurrency/exercises/exercise_100.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# NOTE: To run the unit tests for this exercise, you need to install the following:
44
# $ pip install pytest pytest-asyncio
55

6-
import asyncio
6+
import asyncio # noqa: F401
77
from functools import wraps
88
import timeit
99
from typing import Callable, Any
@@ -21,15 +21,13 @@ def wrapper(*args, **kwargs) -> Any:
2121
print(f"Time taken for {func.__name__}: {end - start:.4f} seconds")
2222
return result
2323

24-
@wraps(func)
25-
async def async_wrapper(*args, **kwargs) -> Any:
26-
start = timeit.default_timer()
27-
result = await func(*args, **kwargs)
28-
end = timeit.default_timer()
29-
print(f"Time taken for {func.__name__}: {end - start:.4f} seconds")
30-
return result
24+
# Your code should go here.
25+
# Add an `async_wrapper` function to handle async functions
26+
...
3127

32-
return async_wrapper if asyncio.iscoroutinefunction(func) else wrapper
28+
# Hint: Check if the function is a coroutine function using `asyncio.iscoroutinefunction()`
29+
# before returning the appropriate wrapper function.
30+
return wrapper
3331

3432
if _func is None:
3533
return decorator

0 commit comments

Comments
 (0)