Python Forum

Full Version: Class sort not sorting corretly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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.
how can you do it with the sort function?

Yeah I am not familiar with binary tree.
Well, you could just sort the list after adding each new value, but I doubt that's the solution your teacher is looking for.
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
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.
I fixed it!! thank you everyone