Respuesta :
Answer:
The function in Python is as follows:
def heightChecker(heights):
expected = []
count = 0
for i in range(len(heights)):
expected.append(heights[i])
expected.sort()
for i in range(len(heights)):
if expected[i] != heights[i]:
count+=1
return count
Step-by-step explanation:
Required
Function to return the number of out of position students
See comment for complete question
This defines the function. It receives the heights as a parameter, from the main
def heightChecker(heights):
This initializes a list, (expected list)
expected = []
This initializes the count of out of position students
count = 0
This iterates through the original list
for i in range(len(heights)):
The list item is then appended to the expected list
expected.append(heights[i])
This sorts the expected list in ascending order
expected.sort()
This iterates through the original and the expected lists
for i in range(len(heights)):
If list elements at the same index are not the same, count is incremented by 1
if expected[i] != heights[i]:
count+=1
This returns count
return count