Python Forum
Class sort not sorting corretly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class sort not sorting corretly
#1
class SortedList:
    def __init__(self):
        """
        Constructor: build an empty sorted list
        """
        self.nums = list()
    
    def add_value(self, value):
        """
        Add a new value to the sorted list:
        should insert the new value to the right index if there are existing values in the list.
        """
        if len(self.nums) == 0:
            self.nums.append(value)
        else:
            if value > max(self.nums):
                #append if value is greater than number being inserted
                self.nums.append(value)
            
            elif value <= max(self.nums):
                #insert the vlue if value is less than value
                self.nums.insert(0, value)
                
                
    
    def __str__(self):
        """
        Return a string with all the values in the list
        """
        return str(self.nums)
    
    def remove_value(self,value):
        """
        Remove a given value from the list.
        If there is no such value, return a ValueError: "No such value"
        Otherwise, remove one and only one instance of the specified value
        """
        if value not in self.nums:
            raise ValueError("No such value")
        else:
            self.nums.remove(value)
        
# Testing code: build an empty sorted list and add values.
my_sorted_list = SortedList()
my_sorted_list.add_value(1)
my_sorted_list.add_value(2)
my_sorted_list.add_value(0)
my_sorted_list.add_value(100)
my_sorted_list.add_value(-100)
my_sorted_list.add_value(50)

# print the items in the list, the values should be sorted: [-100, 0, 1, 2, 50, 100]
print(my_sorted_list)

# try to remove two values, the 2nd value to remove will cause an error
try:
    my_sorted_list.remove_value(2)
    my_sorted_list.remove_value(22)
except ValueError as e: 
    print(e)

print(my_sorted_list)    
the value 50 is not sorting correctly but all of the other values are sorting correctly
Reply
#2
This could be made a lot easier and with a lot less code using the sorted() function. Just a thought.
https://www.programiz.com/python-program...-in/sorted
Reply
#3
You are only putting new values in at either end, sometimes you need to put them in the middle somewhere (like with 50). You need to search through the list to find the two values the new value is between, and insert it there. Assuming you haven't covered something more efficient like binary trees.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
how can you do it with the sort function?

Yeah I am not familiar with binary tree.
Reply
#5
Well, you could just sort the list after adding each new value, but I doubt that's the solution your teacher is looking for.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Yeah I don't think I can do that. Is there a better way to insert something in the middle. I have don't know how to do that
Reply
#7
You can use the insert method that you already have in there to insert it at any index. The 0 index just inserts it at the front. You just need to search through the list to find the right index to insert it at.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
I fixed it!! thank you everyone
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,354 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  how to sort a list without .sort() function letmecode 3 3,413 Dec-28-2020, 11:21 PM
Last Post: perfringo
  [split] Manual Sort without Sort function fulir16 2 3,154 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 48,904 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU
  Bubble Sort Algorithm Not Sorting LikedList Contents JayJayOi 0 3,680 Jan-19-2018, 12:09 AM
Last Post: JayJayOi

Forum Jump:

User Panel Messages

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