Python Forum
defining a function to see if a list is sorted - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: defining a function to see if a list is sorted (/thread-14417.html)

Pages: 1 2


defining a function to see if a list is sorted - Siylo - Nov-29-2018

I am trying to define a function called isSorted that should take a list as an argument an return True if it is sorted and False if it is not sorted. I've tried to look up videos for help and haven't found any relevant ones. The problem I am having is how to check the value of an index against the previous one to see if it is larger. Can anyone give me an idea on how to do this?

def isSorted(list):
    previous_number = 0
    for i in list:
        value = list[i]
        if value - previous_number >= 0:
            previous_number = list[i]
            return True
        else:
            return False


lyst = [1,2,3,4,5,6,7,8,9]

isSorted(lyst)
this is what I have come up with but when I print it out it is always true, even when i make the list unsorted.


RE: defining a function to see if a list is sorted - Larz60+ - Nov-29-2018

why not just assume unsorted and then sort.
Sorting is pretty quick unless the file is extremely large


RE: defining a function to see if a list is sorted - Siylo - Nov-29-2018

I can't. It is a specific homework project. The project is as stated, define a function called isSorted, that takes a list as an assignment, that will return True if the list is sorted and False if it is not.


RE: defining a function to see if a list is sorted - Gribouillis - Nov-29-2018

Hint: you don't need i, use directly for value in list, but there are other issues.


RE: defining a function to see if a list is sorted - wavic - Nov-29-2018

for index, value in enumerate(my_list):
    if my_list[index - 1] < value:
        # actions
    else:
        # actions



RE: defining a function to see if a list is sorted - Siylo - Nov-29-2018

How do I compare a value against the previous value in the list? When I use the code, that is posted above this post, it returns false even if the list is a sorted list. We haven't gone over enumerate or anything like that in class so far so I know it can be done without using it. When using
 for value in list:
How can I compare value[2] to value[1]?


RE: defining a function to see if a list is sorted - Gribouillis - Nov-29-2018

Siylo Wrote:How can I compare value[2] to value[1]?
You need to manage a previous value, you can initialize it to list[0], for example
previous_value = list[0]
for value in list:
    ...



RE: defining a function to see if a list is sorted - Siylo - Nov-29-2018

There has to be an issue with where my return statement is because it is returning True even when the list is not sorted.
def isSorted(list):
    previous_value = list[0]
    for value in list:
        
        if value >= previous_value:
            previous_value = value
            return True
        else:
            return False
        

lyst = [1,2,3,4,3,6,7,8,9]

isSorted(lyst)

print(isSorted(lyst))



RE: defining a function to see if a list is sorted - Gribouillis - Nov-29-2018

You're returning True too early. If the two first values are sorted, how can you return True without checking the rest of the list?


RE: defining a function to see if a list is sorted - Siylo - Nov-29-2018

If I back up my return True statement to fall in the same column as my for loop it still returns True. Shouldn't the if loop run for each value in the list per the for loop?

I figured it out. Thank you for helping me @Gribouillis!! Thank you for not just giving me the answer. I want to work my way through it so I understand it. Sometimes I just need a kick in the pants to change the way I'm looking at it.