简体   繁体   中英

How to find if the elements in a list are in ascending order or in descending order without using sort function?

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")
def function(array):
    # check if array of length 1 so it will be mix order
    if len(array) == 1:
        print("It is not in order")
    
    # use constants to check nature of list 
    ascending_count = 0
    decending_count = 0
    
    for a, b in zip(array, array[1:]):
        if a>=b:
            # increase count if decending
            decending_count += 1
        else:
            # increase count if ascending
            ascending_count += 1
        # if both ascending and decending then it is mix 
        if ascending_count and decending_count:
            print("It is not in order")
            break
    
    if decending_count > 0 and ascending_count == 0 :
        print("It is in descending order")
    elif ascending_count > 0 and decending_count == 0:
        print("It is in ascending order")
        

function([1,2,3,4,5])
function([5,4,3,2,1])
function([1,2,3,1,2])

output

It is in ascending order
It is in descending order
It is not in order
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.

Make a list with three True. In the first there will be a result of testing that all items are larger than a previous ones, in the second vice versa. Third is always True. After, print a text corresponding with the first True in result list

res = [True]*3
for a,b in zip(lst[:-1], lst[1:]):
    res[0] &= (a<b)
    res[1] &= (a>b)
print(['ascending order', 'descending order', 'not in order'][res.index(True])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM