A community for students. Sign up today!
Here's the question you clicked on:
 0 viewing
 one year ago
Checking a list to see if it's sorted.
I'm trying to write a function that takes a list and checks to see if its sorted in ascending order. At the moment i can only manage it by copying the list, sorting the copy, and comparing the two. But, it's not in the spirit of the exercise. I should really be using iteration and comparison operators! Can anyone assist?
 one year ago
Checking a list to see if it's sorted. I'm trying to write a function that takes a list and checks to see if its sorted in ascending order. At the moment i can only manage it by copying the list, sorting the copy, and comparing the two. But, it's not in the spirit of the exercise. I should really be using iteration and comparison operators! Can anyone assist?

This Question is Closed

slotema
 one year ago
Best ResponseYou've already chosen the best response.1The basic approach would be to get the current element and the next element and check whether or not they're stored in correct order. You'll need to do this for each element in the list.

petew71
 one year ago
Best ResponseYou've already chosen the best response.0Well, i got to this (below). But, It looks kinda messy, and it doesn't work with letters! def is_sorted(l): """ checks to see if a list is in ascending order""" for look in range(0,len(l)): if [look] > l[look]+1 or l[2] > l[1]: return False return True print is_sorted([1,3,5,7,6])

slotema
 one year ago
Best ResponseYou've already chosen the best response.1The problem seems to be in your comparison: `if [look] > l[look]+1 or l[2] > l[1]:`. There are two main problems. First, though this could be a typo, you compare `[look]`, which is a list with `l[look]+1`, which is a number. You should really compare `l[look]` instead of `[look]`. Second, there's the `l[look]+1`. What this says is basically: give me the value of `l[look]` and add one to it. This way, `l[look]` will always be smaller than `l[look]+1`. What you'd want instead is the value of `l` at index `loop+1`.

petew71
 one year ago
Best ResponseYou've already chosen the best response.0Ahh, yes..It was a typo. Or to be more accurate, a copyandpasteo (which was playing up!) sorry...! As to your second point..I have made a modification thanks to your hint. I've also done a different version of the function. Which do you think is a better example of code practice? I have tested both and can find no errors. Many thanks for your input ! def is_sorted(t): """ checks to see if a list is in ascending order""" for look in range(0,len(t)1): if t[look] > t[look+1] or t[2] > t[1]: return False return True print is_sorted([1,3,5,8,7]) print is_sorted(['c','e','g']) def another_is_sorted(t): """ checks to see if a list is in ascending order""" for look in range(0,len(t)1): first_item, next_item = t[look], t[look+1] if first_item > next_item: return False return True print another_is_sorted([2,4,6,]) print another_is_sorted(['a','b','d','c'])

slotema
 one year ago
Best ResponseYou've already chosen the best response.1I don't really know which one is better code practice. Apart from the or part in the first function (which you don't really need anymore, I think), if you use t[look] and t[look+1] more often, I'd say the second function would be better. Another thing, in you use range and want it started at 0, you can just say e.g. range(len(t)1). Range'll know that you want to start from 0.

petew71
 one year ago
Best ResponseYou've already chosen the best response.0Thanks for those pointers...and taking time out to help!
Ask your own question
Ask a QuestionFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.