Write a predicate function called check_list that accepts a list of integers as an argument and returns True if at least half of the values in the list are less than the first number in the list. Return False otherwise.

Respuesta :

Answer:

# the function check_list is defined

# it takes a parameter

def check_list(integers):

# length of half the list is assigned

# to length_of_half_list

length_of_half_list = len(integers) / 2

# zero is assigned to counter

# the counter keep track of

# elements whose value is less

# than the first element in the list

counter = 0

# the first element in list is

# assigned to index

index = integers[0]

# for loop that goes through the

# list and increment counter

# whenever an element is less

# than the index

for i in range(len(integers)):

if integers[i] < index:

counter += 1

# if statement that returns True

# if counter is greater than half

# length of the list else it will return

# False

if counter > length_of_half_list:

return True

else:

return False

# the function is called with a

# sample list and it returns True

print(check_list([5, 6, 6, 1, 9, 2, 3, 4, 1, 2, 3, 1, 7]))

Explanation:

The program is written is Python and is well commented.

The example use returns True because the number of elements with value less than 5 are more than half the length of the list.