0

Good evening guys,

for my homework i have to implement a class called 'car' which consists of certain parameters like 'registration', 'color', 'manufacturer', 'model' with with output and comparison methods. After this i have to implement a bubble sort to sort these instances by 'manufacturer' and 'model'; the other attributes are neglectable.

my Code so far:

def class car():
    self.__init__(self, registration, color, manufacturer, model):
        self.registration = registration
        self.color = color
        self.manufacturer = manufacturer
        self.model = model
    def output(self):
        return '{}{}{}{}'.format(self.registration, self.color, self. manufacturer, self.model)

    def bubblesort(list_1):
        for k in range(len(list_1)-1, 0, -1):
            for i in range(0,k):
                if list_1[i] >  list[i+1]:
                    list_1[i], list_1[i+1] = list_1[i+1], list_1[i]
        return list_1

what i have to do now is two create an instance of car like this:

instance = [Car(2003, 'black', 'BMW', 'M4')
            Car(2005, 'red', 'Audi', 'Q3')
            Car(2010, 'green', 'BMW', 'X1')
            Car(2007, 'pink', 'Subaru', 'BRZ')]
            Car(1998, 'black', 'Audi', 'Q5')

and after sorting it has to look like this:

[Car(2005, 'red', 'Audi', 'Q3')
Car(1998, 'black', 'Audi', 'Q5')
Car(2003, 'black', 'BMW', 'M4')
Car(2010, 'green', 'BMW', 'X1')
Car(2007, 'pink', 'Subaru', 'BRZ')]

So I have pretty much everything but i don't know how to sort 'the whole instance' - actually i don't even know if you can say this...:D

Maybe some of you guys can help me out; thanks.

2 Answers2

1

Here is the sample code that does what you need. The comparison operator is overloaded (I guessed the logic of comparison).

class Car(object):
    def __init__(self, registration, color, manufacturer, model):
        self.registration = registration
        self.color = color
        self.manufacturer = manufacturer
        self.model = model

    def __str__(self):
        return '{}\t{}\t{}\t{}'.format(self.registration, self.color, self. manufacturer, self.model)

    def __gt__(self, other):
        if isinstance(other, Car):
            if self.manufacturer != other.manufacturer:
                return self.manufacturer.lower() > other.manufacturer.lower()
            else:
                return self.model.lower() > other.model.lower()

def bubblesort(list_1):
    for k in range(len(list_1)-1, 0, -1):
        for i in range(0,k):
            if list_1[i] >  list_1[i+1]:
                list_1[i], list_1[i+1] = list_1[i+1], list_1[i]
    return list_1

instance = [Car(2003, 'black', 'BMW', 'M4'),
            Car(2005, 'red', 'Audi', 'Q3'),
            Car(2010, 'green', 'BMW', 'X1'),
            Car(2007, 'pink', 'Subaru', 'BRZ'),
            Car(1998, 'black', 'Audi', 'Q5')]

sorted_instance = bubblesort(instance)
for item in sorted_instance:
    print(str(item))
aldarel
  • 446
  • 2
  • 7
  • You can simplify the logic of `__eq__` down to `return (self.manufacturer.lower(), self.model.lower()) == (other.manufacturer.lower(), other.model.lower())`. Same applies to `__gt__`, you only need to replace `==` with `>`, and the tuple class does the rest, the way you described – 9000 Jan 25 '17 at 21:01
  • ooooh my gosh - thank you very much; I appreciate it! Would it be possible for you to explain how the __gt__ method works? Would be nice to know so I can use it from now on :) Thanks a lot! – Daniel Jürgens Jan 25 '17 at 21:10
  • Whenever Python interpreter stumbles across comparison operator - ">" ("greater than") __gt__ function is executed for instance that is left value of the operator (here list_1[i]) . Right value (here list_1[i+1]) of the operator is "other" argument. – aldarel Jan 25 '17 at 21:18
  • okay - then..what exactly is the difference between __str__ function and __repr__ function? is it just 'easier to read' than __repr__ or is there any information loss by using __str__ ? – Daniel Jürgens Jan 25 '17 at 21:39
  • Here is quite decent explanation if u want to delve deeper: http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python – aldarel Jan 25 '17 at 21:45
0

You can only sort comparable things, that is, something that can be thought of "less than" another such thing.

For cars it might be model name, year, etc, or a combination of these.

To make an object comparable, you need to implement methods like __lt__ (less tan) and __eq__ (equals).

But your class seems to be just a bunch of immutable fields (which is usually good). Try using a namedtuple, it creates a class for you, and it provides an implementation for comparison methods. With it, your cars would be sorted by the first field declared, then by second, etc.

Also, the bubblesort method definitely does not belong to the Car class, for it never uses any instance data of a car. It should be a standalone function.

9000
  • 39,899
  • 9
  • 66
  • 104
  • see, that's why I like this platform so much! unfortunately I do not know how to implement everything at all..thanks for your advice - I'll try and see if i get things running :) – Daniel Jürgens Jan 25 '17 at 20:49