Jun-16-2019, 05:14 PM
The task is :
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. For example:
>>> is_sorted([1, 2, 2])
True
>>> is_sorted(['b', 'a'])
False
My code is :
is_sorted([1,2,3,4,0,5,6])
the answer is True which is not correct! Why?
We see a[3]=4 and a[4]=0 which is not True. So why it gives me True?
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. For example:
>>> is_sorted([1, 2, 2])
True
>>> is_sorted(['b', 'a'])
False
My code is :
def is_sorted(a): for i in range(len(a)-1): if a[i]<=a[i+1]: return True return FalseWhen I try:
is_sorted([1,2,3,4,0,5,6])
the answer is True which is not correct! Why?
We see a[3]=4 and a[4]=0 which is not True. So why it gives me True?