formations/algo/poo/cours/snippets/sorter.py

39 lines
1.0 KiB
Python
Raw Normal View History

2017-09-05 09:25:48 +02:00
class Sorter:
def sort(self, list):
for i in range(len(list) - 1):
for j in range(i, len(list)):
if self.compareItems(list[i], list[j]):
list[i], list[j] = list[j], list[i]
def getName(self):
return "Trieur de liste"
def getDescription(self):
raise NotImplementedError
def compareItems(self, item1, item2):
raise NotImplementedError
class AscendantSorter(Sorter):
def compareItems(self, item1, item2):
return item1 >= item2
def getDescription(self):
return "Tri par ordre normal"
def getName(self):
return "Ascendant"
class DescendantSorter(Sorter):
def compareItems(self, item1, item2):
return item1 <= item2
def getDescription(self):
return "Tri par ordre inverse"
if __name__ == '__main__':
list = ['b', 'e', 'a', 'c', 'z']
s = AscendantSorter()
s.sort(list)
print list
s = DescendantSorter()
s.sort(list)
print list