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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!