Skip to content

Commit a814d46

Browse files
author
Shehab Abdel-Salam
committed
Add 2 exercises
1 parent 0c0cfd9 commit a814d46

File tree

8 files changed

+87
-44
lines changed

8 files changed

+87
-44
lines changed
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# Exercise 02_01 - Temperature Conversion
2-
# Write a program that converts a temperature from Celsius to Fahrenheit.
3-
# Hint: The formula is: F = C * 9/5 + 32
1+
# Exercise 02_01 - Convert Hours to Seconds
2+
# Write a program that converts hours to seconds.
43

4+
# For example, given the number of hours 2, the function should return 7200.
55

6-
def celsius_to_fahrenheit(celsius):
6+
7+
def hours_to_seconds(hours):
78
# Your code should go here.
89

910
return ...
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
# Exercise 02_06 - Binary to Decimal
2-
# Write a program that takes a binary string as input and
3-
# returns its decimal representation as an integer.
4-
# For example, given the binary string "1010", the function should return 10.
5-
# or given the binary string "1101", the function should return 13.
1+
# Exercise 02_05 - Temperature Conversion
2+
# Write a program that converts a temperature from Celsius to Fahrenheit.
3+
# Hint: The formula is: F = C * 9/5 + 32
64

75

8-
def convert_binary_to_decimal(binary):
6+
def celsius_to_fahrenheit(celsius):
97
# Your code should go here.
108

119
return ...
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
# Exercise 02_04 - 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 02_06 - Binary to Decimal
2+
# Write a program that takes a binary string as input and
3+
# returns its decimal representation as an integer.
4+
# For example, given the binary string "1010", the function should return 10.
5+
# or given the binary string "1101", the function should return 13.
66

7-
G = 6.674 * 10**-11 # Gravitational constant
87

9-
10-
def calculate_gravity_force(mass1, mass2, distance):
8+
def convert_binary_to_decimal(binary):
119
# Your code should go here.
1210

1311
return ...
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Exercise 02_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
6+
7+
G = 6.674 * 10**-11 # Gravitational constant
8+
9+
10+
def calculate_gravity_force(mass1, mass2, distance):
11+
# Your code should go here.
12+
13+
return ...

chapters/chapter02_variables/tests/test_ch02.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
from ..exercises.exercise_ch2_01 import celsius_to_fahrenheit
1+
from ..exercises.exercise_ch2_01 import hours_to_seconds
22
from ..exercises.exercise_ch2_02 import compute_average_grade
33
from ..exercises.exercise_ch2_03 import add_numbers
44
from ..exercises.exercise_ch2_04 import compute_length_difference
5-
from ..exercises.exercise_ch2_05 import convert_binary_to_decimal
6-
from ..exercises.exercise_ch2_06 import calculate_gravity_force
5+
from ..exercises.exercise_ch2_05 import celsius_to_fahrenheit
6+
from ..exercises.exercise_ch2_06 import convert_binary_to_decimal
7+
from ..exercises.exercise_ch2_07 import calculate_gravity_force
78

89

910
def test_ch02_e01():
10-
assert celsius_to_fahrenheit(0) == 32
11-
assert celsius_to_fahrenheit(100) == 212
12-
assert celsius_to_fahrenheit(37) == 98.6
11+
assert hours_to_seconds(0) == 0
12+
assert hours_to_seconds(1) == 3600
13+
assert hours_to_seconds(2) == 7200
14+
assert hours_to_seconds(3) == 10800
1315

1416

1517
def test_ch02_e02():
@@ -34,13 +36,19 @@ def test_ch02_e04():
3436

3537

3638
def test_ch02_e05():
39+
assert celsius_to_fahrenheit(0) == 32
40+
assert celsius_to_fahrenheit(100) == 212
41+
assert celsius_to_fahrenheit(37) == 98.6
42+
43+
44+
def test_ch02_e06():
3745
assert convert_binary_to_decimal("0") == 0
3846
assert convert_binary_to_decimal("10") == 2
3947
assert convert_binary_to_decimal("11") == 3
4048
assert convert_binary_to_decimal("100") == 4
4149

4250

43-
def test_ch02_e06():
51+
def test_ch02_e07():
4452
assert calculate_gravity_force(1, 1) == 9.8
4553
assert calculate_gravity_force(5, 2) == 19.6
4654
assert calculate_gravity_force(10, 5) == 49
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
# Exercise 03_06 - Stats Report
2-
# Given three numbers that represent the number of wins, losses, and draws of a football team,
3-
# write a program that generates a report with the following information:
4-
# 1- The total number of games played.
5-
# 2- The percentage of games won.
6-
# 3- The percentage of games lost.
7-
# 4- The percentage of games drawn.
8-
# The function should return a string with the report.
9-
# For example, given the numbers 3, 2, and 1, the function should return:
10-
# "The team played 6 games. They won 50.0% of the games, lost 33.3% of the games, and drew 16.7% of the games."
11-
# Note: The percentages should be rounded to one decimal place.
12-
# You can assume that the input numbers are > 0.
1+
# Exercise 03_06 - 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
1310

1411

15-
def generate_stats_report(wins, losses, draws):
16-
total_games = None
17-
wins_percentage = None
18-
losses_percentage = None
19-
draws_percentage = None
12+
def is_valid_isbn(isbn):
2013
# Your code should go here.
2114

22-
return f"The team played {total_games} games. They won {wins_percentage}% of the games, lost {losses_percentage}% of the games, and drew {draws_percentage}% of the games." # noqa
15+
return ...
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Exercise 03_07 - Stats Report
2+
# Given three numbers that represent the number of wins, losses, and draws of a football team,
3+
# write a program that generates a report with the following information:
4+
# 1- The total number of games played.
5+
# 2- The percentage of games won.
6+
# 3- The percentage of games lost.
7+
# 4- The percentage of games drawn.
8+
# The function should return a string with the report.
9+
# For example, given the numbers 3, 2, and 1, the function should return:
10+
# "The team played 6 games. They won 50.0% of the games, lost 33.3% of the games, and drew 16.7% of the games."
11+
# Note: The percentages should be rounded to one decimal place.
12+
# You can assume that the input numbers are > 0.
13+
14+
15+
def generate_stats_report(wins, losses, draws):
16+
total_games = None
17+
wins_percentage = None
18+
losses_percentage = None
19+
draws_percentage = None
20+
# Your code should go here.
21+
22+
return f"The team played {total_games} games. They won {wins_percentage}% of the games, lost {losses_percentage}% of the games, and drew {draws_percentage}% of the games." # noqa

chapters/chapter03_operators/tests/test_ch03.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from ..exercises.exercise_ch3_03 import bitwise_operations
44
from ..exercises.exercise_ch3_04 import area_of_triangle
55
from ..exercises.exercise_ch3_05 import calculate_hypotenuse
6-
from ..exercises.exercise_ch3_06 import generate_stats_report
6+
from ..exercises.exercise_ch3_06 import is_valid_isbn
7+
from ..exercises.exercise_ch3_07 import generate_stats_report
78

89

910
def test_ch03_e01():
@@ -40,6 +41,15 @@ def test_ch03_e05():
4041

4142

4243
def test_ch03_e06():
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
50+
51+
52+
def test_ch03_e07():
4353
assert (
4454
generate_stats_report(1, 0, 0)
4555
== r"The team played 1 games. They won 100.0% of the games, lost 0.0% of the games, and drew 0.0% of the games."

0 commit comments

Comments
 (0)