22.3k views
3 votes
. Write a function called is_sorted that takes a list as a parameter and returns True if the list is sorted in ascending order and False otherwise. Here, I want you to make use of the input function, where users could provide an input that would be the members of the list.

User Murat Kara
by
4.4k points

1 Answer

3 votes

Answer:

There are multiple ways to solve the given problem, I will be presenting the most simple and easy way of solving this problem in few lines of code.

Python code:

def is_sorted(user_list):

return user_list == sorted(user_list)

user_list=input("Please enter a list: ")

user_list = user_list.split()

print(is_sorted(user_list))

print("Your list: ", user_list)

Step-by-step explanation:

A function is_sorted is created which takes a list as input and using the sorted() function of python which returns true when the list is sorted in ascending order and returns false when the list is not sorted in ascending order.

Driver code includes getting input list from the user and using split() function to create a list separated by space then printing the output of is_sorted function and in the last printing the contents of that list.

Output:

Please enter a list: 1 3 6 9

True

Your list: ['1', '3', '6', '9']

Please enter a list: 15 7 2 20

False

Your list: ['15', '7', '2', '20']

. Write a function called is_sorted that takes a list as a parameter and returns True-example-1
. Write a function called is_sorted that takes a list as a parameter and returns True-example-2
User Jan Zich
by
4.4k points