Skip to content

Commit 0c0cfd9

Browse files
author
Shehab Abdel-Salam
committed
Add exercises for ch12
1 parent 775d700 commit 0c0cfd9

File tree

18 files changed

+298
-52
lines changed

18 files changed

+298
-52
lines changed

chapters/chapter05_functions/exercises/exercise_ch5_01.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Exercise 05_01 - Simple Function
22
# Write a function that adds a number by a factor passed as an argument.
33
# The factor should have a default value of 1.
4-
# For example, increment_value_by(5, 3) should return 8 and increment_value_by(5) should return 6.
4+
# For example
5+
# increment_value_by(5, 3) should return 8
6+
# increment_value_by(5) should return 6.
57

68

79
# You may need to update the function signature. See tests/test_ch5.py for inspiration.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Exercise 07_08 - String Compression
2+
# Write a function that compresses a string using a simple compression algorithm.
3+
# If a character appears more than once in a row, replace the sequence with
4+
# the character followed by the number of times it appears.
5+
# For example, the string "aaabbbbbcc" would be compressed to "a3b5c2".
6+
7+
8+
def compress_string(s: str) -> str:
9+
# Your code should go here.
10+
11+
...

chapters/chapter07_strings/tests/test_ch07.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from ..exercises.exercise_ch7_04 import replace_spaces_with_hyphens
55
from ..exercises.exercise_ch7_05 import extract_error_messages
66
from ..exercises.exercise_ch7_06 import calculate_product_sales
7+
from ..exercises.exercise_ch7_07 import find_longest_word
8+
from ..exercises.exercise_ch7_08 import compress_string
79

810

911
def test_ch07_e01():
@@ -93,3 +95,25 @@ def test_ch07_e06():
9395
"2021-01-03,Banana,300",
9496
]
9597
) == [("Apple", 300), ("Orange", 450), ("Banana", 450)]
98+
99+
100+
def test_ch07_e07():
101+
assert find_longest_word("") == ""
102+
assert (
103+
find_longest_word(["hello", "world", "python", "programming"]) == "programming"
104+
)
105+
assert (
106+
find_longest_word(["hello", "world", "python", "programming", "language"])
107+
== "programming"
108+
)
109+
assert find_longest_word(["I", "am", "learning", "Python"]) == "learning"
110+
assert find_longest_word(["Hello", "World"]) == "Hello"
111+
112+
113+
def test_ch07_e08():
114+
assert compress_string("") == ""
115+
assert compress_string("abcd") == "abcd"
116+
assert compress_string("aaabbbbbcc") == "a3b5c2"
117+
assert compress_string("aabbbcc") == "a2b3c2"
118+
assert compress_string("aaaaaabbbc") == "a6b2c"
119+
assert compress_string("aadddcccbb") == "a2d3c3b2"
Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1-
# Exercise 08_03 - File Handler
2-
# Write a custom context manager using a class FileHandler that opens
3-
# and closes a file. Use it to read from data.txt.
1+
# Exercise 08_04 - Inventory Management System
2+
# Write a CSV file containing inventory data and generates a report on stock levels and restock alerts.
3+
# The CSV file contains columns: item_name, item_id, quantity, and price.
4+
# Read the CSV file and calculate the total value of the inventory.
5+
# Identify items with stock levels below the restock threshold and store them in a list.
6+
# Return a dictionary containing the total inventory value and the list of items that need restocking.
47

8+
# Example data.csv:
9+
# item_name,item_id,quantity,price
10+
# Apple,1,100,0.5
11+
# Banana,2,200,0.3
12+
# Orange,3,150,0.4
513

6-
class FileHandler: ...
14+
# The function should return:
15+
16+
# {
17+
# "total_inventory_value": 140.0,
18+
# "restock_alerts": [
19+
# {"item_name": "Apple", "item_id": 1, "quantity": 100, "price": 0.5},
20+
# {"item_name": "Orange", "item_id": 3, "quantity": 150, "price": 0.4},
21+
# ],
22+
# }
23+
24+
# The restock threshold is 200 items.
25+
# The total inventory value is calculated as the sum of quantity * price for each item.
26+
# The restock alerts are items with a quantity below the restock threshold.
27+
28+
29+
def inventory_management(
30+
file_name: str, restock_threshold: int
31+
) -> dict[str, list[dict[str, str]]]:
32+
# Your code should go here.
33+
34+
...
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Exercise 08_05 - File Handler
2+
# Write a custom context manager using a class FileHandler that opens
3+
# and closes a file. Use it to read from data.txt.
4+
5+
6+
class FileHandler: ...

chapters/chapter08_files/tests/test_ch08.py

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ..exercises.exercise_ch8_01 import count_errors_from_file
66
from ..exercises.exercise_ch8_02 import count_word_frequency
77
from ..exercises.exercise_ch8_03 import course_grades_summary
8+
from ..exercises.exercise_ch8_04 import inventory_management
89

910
# from ..exercises.exercise_ch8_04 import FileHandler
1011

@@ -126,10 +127,101 @@ def test_ch08_e03(source, expected):
126127
assert course_grades_summary(source) == expected
127128

128129

129-
# TODO: Fix the exercise.
130-
# def test_ch08_e04(source):
131-
# file_handler = FileHandler(source)
132-
# file_handler.write("Hello, World!")
133-
# assert file_handler.read() == "Hello, World!"
134-
# file_handler.close()
135-
# assert file_handler.closed
130+
@pytest.mark.parametrize(
131+
"file_content, restock_threshold, expected",
132+
[
133+
(
134+
textwrap.dedent(
135+
"""
136+
"""
137+
),
138+
0,
139+
[],
140+
),
141+
(
142+
textwrap.dedent(
143+
"""
144+
item_name,item_id,quantity,price
145+
Apple,1,100,0.5
146+
Banana,2,200,0.3
147+
Orange,3,150,0.4
148+
"""
149+
),
150+
200,
151+
[
152+
{
153+
"item_name": "Apple",
154+
"item_id": 1,
155+
"quantity": 100,
156+
"price": 0.5,
157+
},
158+
{
159+
"item_name": "Orange",
160+
"item_id": 3,
161+
"quantity": 150,
162+
"price": 0.4,
163+
},
164+
],
165+
),
166+
(
167+
textwrap.dedent(
168+
"""
169+
item_name,item_id,quantity,price
170+
Apple,1,100,0.5
171+
Banana,2,200,0.3
172+
Orange,3,150,0.4
173+
"""
174+
),
175+
100,
176+
[
177+
{
178+
"item_name": "Apple",
179+
"item_id": 1,
180+
"quantity": 100,
181+
"price": 0.5,
182+
},
183+
],
184+
),
185+
(
186+
textwrap.dedent(
187+
"""
188+
item_name,item_id,quantity,price
189+
Apple,1,100,0.5
190+
Banana,2,200,0.3
191+
Orange,3,150,0.4
192+
"""
193+
),
194+
300,
195+
[],
196+
),
197+
(
198+
textwrap.dedent(
199+
"""
200+
item_name,item_id,quantity,price
201+
Apple,1,100,0.5
202+
Banana,2,200,0.3
203+
Orange,3,150,0.4
204+
Pear,4,300,0.6
205+
Kiwi,5,400,0.7
206+
"""
207+
),
208+
250,
209+
[
210+
{
211+
"item_name": "Apple",
212+
"item_id": 1,
213+
"quantity": 100,
214+
"price": 0.5,
215+
},
216+
{
217+
"item_name": "Orange",
218+
"item_id": 3,
219+
"quantity": 150,
220+
"price": 0.4,
221+
},
222+
],
223+
),
224+
],
225+
)
226+
def test_ch08_e04(source, restock_threshold, expected):
227+
assert inventory_management(source, restock_threshold) == expected
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
# Exercise 12.1 - Fibonacci Iterator
2-
# Write a Python class that implements an iterator for generating the first n Fibonacci numbers.
1+
# Exercise 12_01 - Even Number Iterator
2+
# Write a Python class that implements an iterator for generating the first n even numbers.
33

44

5-
def my_function():
6-
# Students will write their solution here
7-
return "Hello, World!"
5+
class EvenNumberIterator:
6+
def __init__(self, n: int): ...
7+
8+
def __iter__(self): ...
9+
10+
def __next__(self): ...
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
# Exercise 12.1 - Generator for Fibonacci Sequence
2-
# Write a Python generator function to yield Fibonacci numbers up to a specified limit.
1+
# Exercise 12_01 - Fibonacci Iterator
2+
# Write a Python class that implements an iterator for generating the first n Fibonacci numbers.
33

44

5-
def my_function():
6-
# Students will write their solution here
7-
return "Hello, World!"
5+
class FibonacciIterator:
6+
def __init__(self, n: int): ...
7+
8+
def __iter__(self): ...
9+
10+
def __next__(self): ...
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# Exercise 12.1 - Counter Closure
2-
# Write a Python closure that keeps track of how many times it has been called.
1+
# Exercise 12_02 - Generator for Even Numbers
2+
# Write a Python generator to yield the first n even numbers.
33

44

5-
def my_function():
6-
# Students will write their solution here
7-
return "Hello, World!"
5+
def even_numbers_generator(n: int): ...
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# Exercise 12.1 - Counter Closure
2-
# Create a decorator timer that measures and prints the time taken for a function to execute.
1+
# Exercise 12_02 - Generator for Fibonacci Sequence
2+
# Write a Python generator to yield Fibonacci numbers up to a specified limit.
33

44

5-
def my_function():
6-
# Students will write their solution here
7-
return "Hello, World!"
5+
def fibonacci_generator(limit: int): ...

0 commit comments

Comments
 (0)