Skip to content

Commit 723d59a

Browse files
author
Shehab Abdel-Salam
committed
Fix old exercises
1 parent fdeb9c0 commit 723d59a

File tree

11 files changed

+135
-156
lines changed

11 files changed

+135
-156
lines changed
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
# Exercise 07 - May The Force Be With You
2-
# Write a program that calculates the force of gravity between two objects.
3-
# The formula to calculate the force of gravity is:
4-
# F = G * (m1 * m2) / r^2
5-
# https://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation
1+
# Exercise 07 - Midpoint Calculation
2+
# Write a program that calculates the midpoint between two points.
3+
# The program should take the x and y coordinates of two points as input
4+
# and return the midpoint as a tuple.
65

7-
G = 6.674 * 10**-11 # Gravitational constant
6+
# The formula for calculating the midpoint is as follows:
7+
# midpoint_x = (x1 + x2) / 2
8+
# midpoint_y = (y1 + y2) / 2
9+
# The midpoint is the point that is exactly halfway between two points.
10+
# For example, given the points (1, 2) and (3, 4), the midpoint should be (2, 3).
811

912

10-
def calculate_gravity_force(mass1, mass2, distance):
13+
# Note: you can return more than one value by separating them with a comma.
14+
15+
16+
def calculate_midpoint(x1, y1, x2, y2):
1117
# Your code should go here.
1218

1319
return ...

chapters/chapter02_variables/tests/test_ch02.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ..exercises.exercise_04 import compute_length_difference
55
from ..exercises.exercise_05 import compute_average_grade
66
from ..exercises.exercise_06 import convert_binary_to_decimal
7-
from ..exercises.exercise_07 import calculate_gravity_force
7+
from ..exercises.exercise_07 import calculate_midpoint
88

99

1010
def test_e01():
@@ -49,7 +49,7 @@ def test_e06():
4949

5050

5151
def test_e07():
52-
assert calculate_gravity_force(1, 1) == 9.8
53-
assert calculate_gravity_force(5, 2) == 19.6
54-
assert calculate_gravity_force(10, 5) == 49
55-
assert calculate_gravity_force(100, 10) == 980
52+
assert calculate_midpoint(0, 0, 0, 0) == (0.0, 0.0)
53+
assert calculate_midpoint(1, 2, 3, 4) == (2.0, 3.0)
54+
assert calculate_midpoint(5, 10, 15, 20) == (10.0, 15.0)
55+
assert calculate_midpoint(-5, -10, -15, -20) == (-10.0, -15.0)
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
# Exercise 13 - ISBN Verifier
2-
# Write a program that checks if a given ISBN number is valid.
3-
# The ISBN is valid if:
4-
# - It consists of 10 digits.
5-
# - The first 9 digits are numbers.
6-
# - The last digit is a number or an uppercase X.
7-
# - The ISBN is valid if the sum of the 10 digits multiplied by their position modulo 11 is 0.
8-
# The program should return True if the ISBN is valid and False otherwise.
9-
# formula: (d1*1 + d2*2 + d3*3 + d4*4 + d5*5 + d6*6 + d7*7 + d8*8 + d9*9 + d10*10) % 11 == 0
1+
# Exercise 13 - Add Without Plus Operator
2+
# This program below adds two numbers without using the '+' operator.
103

4+
# There is a bug in the code below. Find it and fix it.
115

12-
def is_valid_isbn(isbn):
13-
# Your code should go here.
146

15-
return ...
7+
def add_without_plus_operator(a, b):
8+
# Continue the loop until the carry (b) becomes zero.
9+
while b != 0:
10+
# Calculate the bitwise AND of 'a' and 'b'.
11+
data = a & b
12+
# XOR 'a' and 'b' to get the sum without considering carry.
13+
a = a ^ b
14+
b = data >> 1
15+
16+
return a

chapters/chapter03_operators/tests/test_ch03.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ..exercises.exercise_10 import bitwise_operations
44
from ..exercises.exercise_11 import area_of_triangle
55
from ..exercises.exercise_12 import calculate_hypotenuse
6-
from ..exercises.exercise_13 import is_valid_isbn
6+
from ..exercises.exercise_13 import add_without_plus_operator
77
from ..exercises.exercise_14 import generate_stats_report
88

99

@@ -41,12 +41,11 @@ def test_e12():
4141

4242

4343
def test_e13():
44-
assert is_valid_isbn("123456789X") is True
45-
assert is_valid_isbn("987654321X") is True
46-
assert is_valid_isbn("1234567890") is False
47-
assert is_valid_isbn("1234567891") is False
48-
assert is_valid_isbn("1234567892") is False
49-
assert is_valid_isbn("1234567893") is False
44+
assert add_without_plus_operator(0, 0) == 0
45+
assert add_without_plus_operator(10, 2) == 12
46+
assert add_without_plus_operator(2, 10) == 12
47+
assert add_without_plus_operator(-20, 10) == -10
48+
assert add_without_plus_operator(-10, -20) == -30
5049

5150

5251
def test_e14():

chapters/chapter08_files/exercises/exercise_60.py

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

1414

1515
def course_grades_summary(file_name: str) -> list[tuple[str, float]]:
16-
# Your code should go here.
17-
18-
...
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
Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,57 @@
11
# Exercise 61 - 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.
2+
# Given a text file containing inventory data and generates a report on stock levels and restock alerts.
3+
# This file contains columns: item_name, quantity, and price.
4+
# Read the file and calculate the total value of the inventory.
55
# Identify items with stock levels below the restock threshold and store them in a list.
66
# Return a dictionary containing the total inventory value and the list of items that need restocking.
77

8-
# Example data.csv:
9-
# item_name,item_id,quantity,price
8+
# Example data.txt:
9+
# item_name,quantity,price
1010
# Apple,1,100,0.5
1111
# Banana,2,200,0.3
1212
# Orange,3,150,0.4
1313

14-
# The function should return:
15-
14+
# With restock threshold of 200. The function should return:
1615
# {
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-
# ],
16+
# "total_inventory_value": 170.0,
17+
# "restock_alerts": ["Apple", "Orange"]
2218
# }
2319

24-
# The restock threshold is 200 items.
2520
# The total inventory value is calculated as the sum of quantity * price for each item.
2621
# The restock alerts are items with a quantity below the restock threshold.
2722

2823

2924
def inventory_management(
3025
file_name: str, restock_threshold: int
3126
) -> dict[str, list[dict[str, str]]]:
32-
# Your code should go here.
27+
total_inventory_value = 0.0
28+
restock_alerts = []
29+
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)
3353

34-
...
54+
return {
55+
"total_inventory_value": total_inventory_value,
56+
"restock_alerts": restock_alerts,
57+
}

chapters/chapter08_files/exercises/exercise_62.py

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

1818

1919
def is_file_sorted(file_name: str) -> bool:
20-
# Your code should go here.
21-
22-
...
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)

0 commit comments

Comments
 (0)