Python Forum
defining a function to see if a list is sorted
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
defining a function to see if a list is sorted
#1
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.
Reply
#2
why not just assume unsorted and then sort.
Sorting is pretty quick unless the file is extremely large
Reply
#3
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.
Reply
#4
Hint: you don't need i, use directly for value in list, but there are other issues.
Reply
#5
for index, value in enumerate(my_list):
    if my_list[index - 1] < value:
        # actions
    else:
        # actions
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
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]?
Reply
#7
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:
    ...
Reply
#8
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))
Reply
#9
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?
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A function that checks if the list is sorted pooyan89 16 16,588 Apr-22-2024, 09:14 AM
Last Post: DeaD_EyE
  How to Sorted and display the Subclasses of BaseException Fernando_7obink 9 3,714 Feb-10-2021, 12:04 PM
Last Post: buran
  Defining x and y axes in Spectrogram Function amy5678 3 2,276 Nov-29-2020, 01:42 PM
Last Post: jefsummers
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,049 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  python 3.4.3 Question a defining function student8 1 4,328 Oct-02-2017, 07:59 PM
Last Post: nilamo
  sorted list not displaying Crackity 6 5,085 Jul-18-2017, 12:50 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020