214,184 views
30 votes
30 votes
Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd. Ex: If the input is:

User Sacho
by
2.7k points

1 Answer

10 votes
10 votes

Answer:

The program in Python is as follows:

myList = []

num = int(input())

count = 0

while num>=0:

myList.append(num)

count+=1

num = int(input())

myList.sort()

mid = int((count-1)/2)

print(myList[mid])

Step-by-step explanation:

This initializes the list

myList = []

This gets the first input

num = int(input())

This initializes count to 0

count = 0

The following is repeated until the user inputs a negative number

while num>=0:

This appends the input number to the list

myList.append(num)

This increments count by 1

count+=1

This gets another input

num = int(input())

This sorts the list

myList.sort()

Assume the number of inputs is odd, the middle element is calculated as

mid = int((count-1)/2)

This prints the middle element

print(myList[mid])

From the complete question. the condition that ends the loop is a negative integer input

User Jim Eisenberg
by
2.5k points