Looking for answers? Westonci.ca is your go-to Q&A platform, offering quick, trustworthy responses from a community of experts. Ask your questions and receive accurate answers from professionals with extensive experience in various fields on our platform. Get precise and detailed answers to your questions from a knowledgeable community of experts on our Q&A platform.

Write the following function that returns True if the list is already sorted in increasing order:
def issorted (1st):
Write a test program that prompts the user to enter a list of numbers separated by a space in one line and displays whether the list is sorted or not.
Here is a sample run:
Sample Run 1
Enter list: 1 1 3 4 4 5 7 9 10 30 11
The list is not sorted
Sample Run 2
Enter list: 1 1 3 4 4 5 7 9 10 30
The list is already sorted​

Sagot :

Answer:

# Get input and strip any leading/trailing spaces

inputList = input('Enter list: ').strip()

def issorted(lst):

   if len(lst) < 2:

       return True

   

   current = 1

   prev = 0

   while current < len(lst):

       # Compare if current value is less than the previous one

       if int(lst[current]) < int(lst[prev]):

           return False

       

       prev = current

       current += 1

   

   return True

# Convert input to list

inputList = inputList.split(' ')

# Print output

if issorted(inputList):

   print("The list is already sorted​")

else:

   print("The list is not sorted")