143k views
5 votes
Ask the user to enter the number of elements in a list of numbers. Take in the numbers from the user and then output the list the user entered . Followed by the list the user entered with only the 2nd half sorted. NOTE: You may assume the user will enter a list with an even number of elements. You should sort only the 2nd half the first half is to remain as is.

User Pochopsp
by
6.4k points

1 Answer

6 votes

Answer:

#Section 1

lst= []

lstNo=int(input("Enter even number of elements in List: "))

for i in range(0, lstNo):

pr=int(input(": " ))

lst.append(pr)

print(lst)

#Section 2

nlst=[]

dlst=[]

n =len(lst)

i=0

while (i < n/2):

nlst.append(lst[i])

i = i+1

j = n-1

while j >= n/2:

dlst.append(lst[j])

j = j-1

dlst.sort()

for a in range(len(dlst)):

nlst.append(dlst[a])

print(nlst)

Step-by-step explanation:

#section 1:

An empty list is declared to hold the list inputs by the user lst= []

The program then prompts the user to enter an even number of elements that will be contained in the list.

The for loop is used to iterate from zero(0) to the number of elements stated, in order to get the input that is appended to the list.

lastly, the list is printed out.

#Section 2:

In this section two new lists are created to hold one half of the value respectively.

The first list collects the first half of the list using a while loop and stores it, it does not perform any sorting on it.

The second list collects the second half using a while loop and sorts the list using the .sort() method which arranges elements in an ascending order.

Finally, the second list that has been sorted is added to the first and the result is printed to the screen.

A picture of how the code will run has been attached.

Ask the user to enter the number of elements in a list of numbers. Take in the numbers-example-1
User Guillaume Smet
by
5.9k points