Python Programming Mrs Chambers alwasy has her class line up in height order(sho
ID: 3683262 • Letter: P
Question
Python Programming
Mrs Chambers alwasy has her class line up in height order(shortest at the front line). Every September a new class of exactly 20 3erd graders arrive, all of different height.for the first few daysit takes a long time to get the kids height order since no one knows where they should be in the line. This year Mrs. Chambers decided to try something new. One student is selected be the first in line. Then another student is selected and should we find that the first person in line is taller than them, the second student stands in forint of the first causeing all the other students to step back. All students will therfore be lined up from shortest to tallest.
Write a program that calculates the total number of steps taken during the ordering process for a given class.
Explanation / Answer
#this is basically an insertion sort
#Here meaning of total number of steps is not clear
def sort_numbers(s):
count = 0
for i in range(1, len(s)):
val = s[i]
j = i - 1
while (j >= 0) and (s[j] > val):
s[j+1] = s[j]
j = j - 1
if j != i-1:
count += 1
s[j+1] = val
return count
x_list = []
for i in range(0,5):
x = raw_input("Enter height ")
x_list.append(int(x))
print x_list
print "It took ",sort_numbers(x_list)," steps"
print(x_list)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.