1. Write a function that checks if a number is a prime number.
Return True if it is prime, otherwise
return False.
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
# Example
print(is_prime(7)) # Output: True
2. Create a dictionary where the keys are student names and the values are lists of grades. Write a
function that returns the average grade for a given student.
def get_average_grade(student_dict, student_name):
grades = student_dict.get(student_name, [])
if grades:
return sum(grades) / len(grades)
return None
# Example
students = {'Ali': [80, 90, 85], 'Zeynep': [70, 75, 80]}
print(get_average_grade(students, 'Ali')) # Output: 85.0
3. Write a function that takes a list as input and returns a new list that is the reverse of the original
without using .reverse() or slicing.
def reverse_list(lst):
reversed_lst = []
i = len(lst) - 1
while i >= 0:
reversed_lst.append(lst[i])
i -= 1
return reversed_lst
# Example
print(reverse_list([1, 2, 3, 4])) # Output: [4, 3, 2, 1]
4. Write a function that takes a string and returns a dictionary showing the count of each character.
def char_count(text):
count_dict = {}
for char in text:
if char in count_dict:
count_dict[char] += 1
else:
count_dict[char] = 1
return count_dict
# Example
print(char_count("hello")) # Output: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
5. Write a function that takes a list of lists and returns a single flattened list. Example: [[1, 2], [3, 4]] ->
[1, 2, 3, 4]
def flatten_list(nested_list):
flat_list = []
for sublist in nested_list:
for item in sublist:
flat_list.append(item)
return flat_list
# Example
print(flatten_list([[1, 2], [3, 4]])) # Output: [1, 2, 3, 4]
6. You have a dictionary of students with their list of grades. Write a function to find and return the
name of the student with the highest average.
def student_with_highest_average(student_dict):
highest_avg = -1
top_student = ""
for student, grades in student_dict.items():
avg = sum(grades) / len(grades)
if avg > highest_avg:
highest_avg = avg
top_student = student
return top_student
# Example
grades = {'Ali': [80, 90], 'Zeynep': [100, 100], 'Mehmet': [70, 65]}
print(student_with_highest_average(grades)) # Output: Zeynep