Westonci.ca is the premier destination for reliable answers to your questions, provided by a community of experts. Join our platform to connect with experts ready to provide precise answers to your questions in different areas. Discover detailed answers to your questions from a wide network of experts on our comprehensive 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")

Thanks for using our service. We aim to provide the most accurate answers for all your queries. Visit us again for more insights. We appreciate your time. Please come back anytime for the latest information and answers to your questions. Westonci.ca is here to provide the answers you seek. Return often for more expert solutions.