Question
5.24 Lab: Is the List Sorted? For this assignment, you have to complete the function isSorted(numbers), which, informally, checks if an input list of number
5.24 Lab: Is the List Sorted?
For this assignment, you have to complete the function isSorted(numbers), which, informally, checks if an input list of number is sorted in ascending order. Formally, the function input is a list of numbers numbers and returns a tuple x,y where x is a boolean (True / False) that indicates if the list is sorted and y is an integer index denoting the index of the first out of order number or -1 if the list is sorted. In other words, y is an integer that denotes the index in the list where the corresponding number is less than the previous number in the list.
Code so far:
def isSorted(numbers): """ Parameter: numbers (list): A list of integers. Returns: bool: True if the list of numbers is sorted, False otherwise. int : -1 if the list of numbers is sorted, otherwise the first index of the list that is out of order.
The return statements would like (True, -1) and (False, i) where i is an integer index. """ # write your code here.
if __name__ == '__main__': """ To test your code, you can change the following int list, named numbers, and check the output. """ numbers = [0, -10, 2, 3, 4, 5]
""" Note that in the following line, there are two variables on the left side of the assignment operator. Since the isSorted() function returns a tuple containing two pieces of data, to store these returned data we need two variables. This process is called Unpacking. The returned values will be stored into the corresponding variables on the left side of the assignment operator. """ returnedIsSorted, returnedIndex = isSorted(numbers) if returnedIsSorted: print('The list is sorted.') else: print('The list is not sorted. The out of order index is {}'.format(returnedIndex))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started