Issue
The question which was given to me in python is , Accept a list with n number of terms and check if whether the elements are in ascending order , descending order or not in order without using sort function and def too.
Please Help me in doing this program.
Code:
a=list(input("Enter elements:"))
for i in range(len(a)):
for j in range(i):
if a[j]<=a[j+1]:
print("It is in ascending order")
elif a[j]>=a[j+1]:
print("It is in descending order")
else:
print("It is not in order")
Solution
def which_order(list1):
isAscending = True
isDescending = True
for i in range(1,len(list1)):
if(list1[i] >= list1[i-1]):
isDescending = False
elif(list1[i] <= list1[i-1]):
isAscending = False
if((not isAscending) and (not isDescending)):
print("The list is not sorted in either order.")
break
if(isAscending):
print("The list is sorted in ascending order.")
if(isDescending):
print("The list is sorted in descending order.")
Sample output:
list1 = [1,2,3,4]
list2 = [9,8,7,6]
list3 = [1,9,8,7,6]
which_order(list1)
which_order(list2)
which_order(list3)
The list is sorted in ascending order.
The list is sorted in descending order.
The list is not sorted in either order.
Answered By - Satheesh K
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.