Write the following function that returns true if the list i

Write the following function that returns true if the list is already sorted in increasing order: def is Sorted(list): Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run:

Solution

# python code to check if list is sorted

def isSorted(list):
# iterate over list
for i in xrange(1,len(list)):
# return false if unordered elements are found
if list[i-1] > list[i]:
return False
return True


string_input = raw_input(\"Enter list: \")
#splits the input string on spaces
list = string_input.split()
# make them float
list = [float(a) for a in list]
if isSorted(list):
print \"List is sorted\"
else:
print \"List is not sorted\"

\'\'\'
output:

Enter list: 3 4 5 6
List is sorted

Enter list: 4 3 5 6
List is not sorted

\'\'\'

 Write the following function that returns true if the list is already sorted in increasing order: def is Sorted(list): Write a test program that prompts the us

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site