satya

Wednesday, October 7, 2020

Program 20

                                                                              Program 20

Write a function called is_sorted that is given a list and returns True if the list is sorted and False otherwise.


def is_sorted(l):

    k=l[:]

    k.sort()

    print("The K List elements are",k)

    if(l==k):

        return True

    else:

        return False

        

        

n=int(input("Enter the number elements in the list:"))

l=[]

for i in range(1,n+1):

    l.append(int(input("Enter the list %d Element:"%(i))))

    

print("The L List elements are",l)


print(is_sorted(l))



OUTPUT:
============RESTART: D:/old/r19 lab-python/20.py ===========
Enter the number elements in the list:5
Enter the list 1 Element:1
Enter the list 2 Element:2
Enter the list 3 Element:3
Enter the list 4 Element:4
Enter the list 5 Element:5
The L List elements are [1, 2, 3, 4, 5]
The K List elements are [1, 2, 3, 4, 5]
True
>>> 
============ RESTART: D:/old/r19 lab-python/20.py ===========
Enter the number elements in the list:5
Enter the list 1 Element:3
Enter the list 2 Element:2
Enter the list 3 Element:1
Enter the list 4 Element:5
Enter the list 5 Element:6
The L List elements are [3, 2, 1, 5, 6]
The K List elements are [1, 2, 3, 5, 6]
False
>>> 


No comments:

Post a Comment

Program 30

                                                                         Program 30 Write a Python class to implement pow(x, n). class Pow1:...