#This function is for testing, do not modify
def check(actual, expected):
if expected != actual:
print(f"Function should return the value {expected}, it is returning the value {actual}")
else:
print(f"Congratulations, the test case passed!")
#This function is for testing, do not modify
def check_multiple(actual, expected):
if actual not in expected:
print(f"Function should return one of the values in the list {expected}, it is returning the value {actual}")
else:
print(f"Congratulations, the test case passed!")
Now, you are given a list clubs_list
which has both the club name and the club points. Write a function qualifying_club(L, P)
where given a list L
and number of qualifying teams T
, you are required to find which top T
teams have qualified to the chamipions league based on their points.
clubs_list = [["Ethiopia Bunna", 66], ["Dedebit", 67], ["St George", 71], [
"Bahrdar Kenema", 94], ["Lucy", 95], ["Adama Kenema", 70], ["Mechal", 53]]
def qualifying_club(L, T):
pass
Write a function sublist
such that for a sorted list L
and two values small
and large
(with large > small
), sublist(L, small, large)
will return a pair of indices beg
, end
that correspond to the part of L
that is between small
and large
.
Follow up: Can you make your function fast using binary search?
def sublist(L, small, large):
pass
sublist([2, 10, 34, 55, 100, 204], 15, 101) == (2, 4) # since L[2]=34 and L[4]=100
# TEST_CASE
check(sublist([2, 10, 34, 55, 100, 204, 555], 15, 101),
(2, 4)) # since L[2]=34 and L[4]=100
check(sublist([2, 10, 34, 55, 100, 555], 15, 101), (2, 4))
check(sublist([2, 10, 14, 34, 55, 100, 204, 555], 15, 101), (3, 5))
check(sublist([2, 10, 24, 55, 98, 99, 100, 204, 555], 15, 101), (2, 6))
Local maximum
You are given an unsorted list L
of nonnegative integers. For example,
L = [200, 500, 30, 0, 1, 4]
Find any number n
in the list that is greater than the numbers to the left and right of it and return it.
For example,
localMax(L)
should return 500
.
The first element is a local max if it is greater than the second element. The last element is a local max if it is greater than the second to last element.
For example: L = [1, 2, 3, 4, 5]
localMax(L) == 5
.
def localMax(L):
pass
#TEST_CASE
L = [1, 2, 3, 4, 5]
check_multiple(localMax(L), [5])
M=[4, 78, 90, 23, 65, 12, 19]
check_multiple(localMax(M), [19,65,90])
N=[19, 18, 3]
check_multiple(localMax(N), [19])
K=[23, 45, 67, 78, 98]
check_multiple(localMax(K), [98])
Given a sorted list, find the closest smaller element to a certain value. If their is no such number return -1
.
Eg. If L = [10, 15, 4, 9, 5]
and v = 10
, the function should return 9.
def find_closest_smaller(L, v):
pass
Write a function number_of_smaller_elements
that takes two lists (L
and M
) as input parameters. The first list is sorted.
For each element in the second list, the function should determine the number of elements in the first list that are less than or equal to it. The function should return this information as a list.
Hint: create a helper function that uses binary search
def number_of_smaller_elements(L, M):
pass
check(number_of_smaller_elements([1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1]), [6, 5, 4, 3, 2, 1])