Skip to content

Commit 65334eb

Browse files
author
Shehab Abdel-Salam
committed
Add 1 exercise in ch4
1 parent a814d46 commit 65334eb

File tree

13 files changed

+102
-75
lines changed

13 files changed

+102
-75
lines changed
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
# Exercise 04_03 - Check Number
2-
# Write a program that returns:
3-
# - 'Positive' if the number is greater than 0
4-
# - 'Negative' if the number is less than 0
5-
# - 'Zero' if the number is equal to 0
1+
# Exercise 04_03 - Alice and Mark
2+
# Alice has scored X marks in her test and Bob has scored Y marks in the same test.
3+
# Alice is happy if she scored at least twice the marks of Bob’s score.
64

5+
# Write a program that checks if Alice is happy or not.
6+
# Example 1:
7+
# Input: 10, 5
8+
# Output: True
79

8-
def check_number(number):
10+
# Example 2:
11+
# Input: 5, 10
12+
# Output: False
13+
14+
15+
def is_alice_happy(alice_marks, bob_marks):
916
# Your code should go here.
1017

1118
return ...
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# Exercise 04_04 - Even or Odd Number
2-
# Write a program that takes an integer and returns whether its an even or odd number.
3-
# Bonus: can you solve this in a single line of code? (Hint: Search for ternary operator)
1+
# Exercise 04_03 - Check Number
2+
# Write a program that returns:
3+
# - 'Positive' if the number is greater than 0
4+
# - 'Negative' if the number is less than 0
5+
# - 'Zero' if the number is equal to 0
46

57

6-
def is_even(number):
8+
def check_number(number):
79
# Your code should go here.
810

911
return ...
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
# Exercise 04_05 - Grade Result
2-
# Write a program that take an integer between 0 and 100
3-
# and returns the grade result based on the following rules:
4-
# 1. “A” if grade is between 90 and 100
5-
# 2. “B” if grade is between 80 and 89
6-
# 3. “C” if grade is between 70 and 79
7-
# 4. “Not Pass” if grade is less than 70
1+
# Exercise 04_04 - Even or Odd Number
2+
# Write a program that takes an integer and returns whether its an even or odd number.
3+
# Bonus: can you solve this in a single line of code? (Hint: Search for ternary operator)
84

95

10-
def grade_result(grade):
6+
def is_even(number):
117
# Your code should go here.
128

139
return ...
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# Exercise 04_06 - Sum Numbers
2-
# Write a program that sums the numbers from start to end.
3-
# For example, if the start is 1 and the end is 5, the sum is 15 (1 + 2 + 3 + 4 + 5).
1+
# Exercise 04_05 - Grade Result
2+
# Write a program that take an integer between 0 and 100
3+
# and returns the grade result based on the following rules:
4+
# 1. “A” if grade is between 90 and 100
5+
# 2. “B” if grade is between 80 and 89
6+
# 3. “C” if grade is between 70 and 79
7+
# 4. “Not Pass” if grade is less than 70
48

5-
# Can you solve this once using a for loop and once using a while loop?
69

7-
8-
def sum_numbers(start, end):
10+
def grade_result(grade):
911
# Your code should go here.
1012

1113
return ...
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Exercise 04_07 - Sum Even Numbers
2-
# Write a program that sums the even numbers from start to end.
3-
# For example, if the start is 1 and the end is 5, the sum is 6 (2 + 4).
1+
# Exercise 04_06 - Sum Numbers
2+
# Write a program that sums the numbers from start to end.
3+
# For example, if the start is 1 and the end is 5, the sum is 15 (1 + 2 + 3 + 4 + 5).
44

55
# Can you solve this once using a for loop and once using a while loop?
66

77

8-
def sum_even_numbers(start, end):
8+
def sum_numbers(start, end):
99
# Your code should go here.
1010

1111
return ...
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# Exercise 04_08 - Largest Index
2-
# Write a program that returns the index of the largest element in the list.
3-
# For example, given the list [1, 2, 3, 4, 5], the function should return 4.
1+
# Exercise 04_07 - Sum Even Numbers
2+
# Write a program that sums the even numbers from start to end.
3+
# For example, if the start is 1 and the end is 5, the sum is 6 (2 + 4).
44

5+
# Can you solve this once using a for loop and once using a while loop?
56

6-
def largest_index(elements):
7+
8+
def sum_even_numbers(start, end):
79
# Your code should go here.
810

911
return ...
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Exercise 04_09 - Calculate Total Length
2-
# Write a program that calculates the total length of all words in a list.
3-
# For example, given the list ['hello', 'world'], the total length is 10.
1+
# Exercise 04_08 - Largest Index
2+
# Write a program that returns the index of the largest element in the list.
3+
# For example, given the list [1, 2, 3, 4, 5], the function should return 4.
44

55

6-
def calculate_total_length(words):
6+
def largest_index(elements):
77
# Your code should go here.
88

99
return ...
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Exercise 04_10 - Double Elements
2-
# Write a program that doubles each element in a list of numbers.
3-
# For example, given the list [1, 2, 3, 4, 5], the function should return [2, 4, 6, 8, 10].
1+
# Exercise 04_09 - Calculate Total Length
2+
# Write a program that calculates the total length of all words in a list.
3+
# For example, given the list ['hello', 'world'], the total length is 10.
44

55

6-
def double_elements(numbers):
6+
def calculate_total_length(words):
77
# Your code should go here.
88

99
return ...
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
# Exercise 04_11 - Find the First Divisible by 5
2-
# Write a program that finds the first number in a list that is divisible by 5.
3-
# If a number is found, return it and exit the loop. If no number is found, return None.
4-
# For example, given [12, 5, 20, 25], the function should return 5.
5-
# or given [1, 2, 3, 4, 6], the function should return None.
1+
# Exercise 04_10 - Double Elements
2+
# Write a program that doubles each element in a list of numbers.
3+
# For example, given the list [1, 2, 3, 4, 5], the function should return [2, 4, 6, 8, 10].
64

75

8-
def find_first_divisible_by_5(numbers):
6+
def double_elements(numbers):
97
# Your code should go here.
108

119
return ...
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# Exercise 04_12 - Skipping Negative Numbers
2-
# Write a program that sums all positive numbers in a list, skipping any negative numbers.
3-
# For example, if the list is [1, -2, 3, -4, 5], the sum is 9 (1 + 3 + 5).
1+
# Exercise 04_11 - Find the First Divisible by 5
2+
# Write a program that finds the first number in a list that is divisible by 5.
3+
# If a number is found, return it and exit the loop. If no number is found, return None.
4+
# For example, given [12, 5, 20, 25], the function should return 5.
5+
# or given [1, 2, 3, 4, 6], the function should return None.
46

57

6-
def sum_positive_numbers(numbers):
8+
def find_first_divisible_by_5(numbers):
79
# Your code should go here.
810

911
return ...

0 commit comments

Comments
 (0)