def students_and_grades_at_one_discipline_sorted(self, discipline_id): sd = self.__student_discipline_repo.get_all() list_items_with_one_discipline = [] list_items_with_one_disciplineDTO = [] for item in sd: if item.get_discipline_id() == discipline_id: list_items_with_one_discipline.append(item) for i in list_items_with_one_discipline: try: sdi = i.get_student_discipline_id() st = i.get_student_id() di = i.get_discipline_id() grade = i.get_grade() s = self.__student_repo.find_by_id(st) d = self.__discipline_repo.find_by_id(di) dto = StudentDisciplineAssembler.create_student_discipline_dto( sdi, s, d, grade) list_items_with_one_disciplineDTO.append(dto) except ValidatorError as ve: raise StoreError(ex=ve) Sorting.sort(list_items_with_one_disciplineDTO, key=lambda x: x.student_name, algorithm=Algorithm.SELECTION_SORT) #self.sort_by_name(list_items_with_one_disciplineDTO) return list_items_with_one_disciplineDTO
def students_and_grades_at_one_discipline_sorted(self, discipline_id): sd = self.__student_discipline_repo.get_all() list_items_with_one_discipline = [] list_items_with_one_disciplineDTO = [] for item in sd: if item.get_discipline_id() == discipline_id: list_items_with_one_discipline.append(item) for i in list_items_with_one_discipline: try: sdi = i.get_student_discipline_id() st = i.get_student_id() di = i.get_discipline_id() grade = i.get_grade() s = self.__student_repo.find_by_id(st) d = self.__discipline_repo.find_by_id(di) dto = StudentDisciplineAssembler.create_student_discipline_dto(sdi, s, d, grade) list_items_with_one_disciplineDTO.append(dto) except ValidatorError as ve: raise StoreError(ex=ve) Sorting.sort(list_items_with_one_disciplineDTO, key=lambda x: x.student_name, algorithm = Algorithm.SELECTION_SORT) #self.sort_by_name(list_items_with_one_disciplineDTO) return list_items_with_one_disciplineDTO
self.__age = age @property def name(self): return self.__name @property def age(self): return self.__age def __str__(self): return self.name + " " + str(self.age) if __name__ == '__main__': l = [2, 1, 3] Sorting.sort(l) assert(l == [1, 2, 3]) p1 = Person("p1", 1) p2 = Person("p2", 2) p3 = Person("p3", 3) p4 = Person("p1", 2) # sort by name asc l = [p3, p2, p1, p4] Sorting.sort(l, key=lambda x:x.name) assert(l == [p1, p4, p2, p3]) # sort by age desc l = [p3, p2, p1, p4]
def test_shake_sort(): ''' test for SHAKE_SORT ''' l = [2, 1, 3] Sorting.sort(l, algorithm=Algorithm.SHAKE_SORT) assert (l == [1, 2, 3]) Sorting.sort(l, reverse=True, algorithm=Algorithm.SHAKE_SORT) assert (l == [3, 2, 1]) l = [2, 1, 2, 3, 1] Sorting.sort(l, algorithm=Algorithm.SHAKE_SORT) assert l == [1, 1, 2, 2, 3] Sorting.sort(l, reverse=True, algorithm=Algorithm.SHAKE_SORT) assert l == [3, 2, 2, 1, 1] p1 = Person("p1", 1) p2 = Person("p2", 2) p3 = Person("p3", 3) p4 = Person("p1", 2) # sort by name asc l = [p3, p2, p1, p4] Sorting.sort(l, key=lambda x: x.name, algorithm=Algorithm.SHAKE_SORT) assert (l == [p1, p4, p2, p3]) # sort by age desc l = [p3, p2, p4, p1] Sorting.sort(l, key=lambda x: x.age, reverse=True, algorithm=Algorithm.SHAKE_SORT) assert (l == [p3, p2, p4, p1]) # sort by age asc l = [p3, p2, p4, p1] Sorting.sort(l, key=lambda x: x.age, algorithm=Algorithm.SHAKE_SORT) assert (l == [p1, p2, p4, p3]) # sort by name,age ascending l = [p3, p2, p1, p4] Sorting.sort(l, key=lambda x: (x.name, x.age), algorithm=Algorithm.SHAKE_SORT) assert (l == [p1, p4, p2, p3]) # sort by name ascending and by age descending def person_less_than(p1, p2): """name ascending, age descending """ if p1.name == p2.name: return p1.age > p2.age return p1.name < p2.name Person.__lt__ = person_less_than Person.__gt__ = not person_less_than l = [p3, p2, p1, p4] Sorting.sort(l, algorithm=Algorithm.SHAKE_SORT) assert (l == [p4, p1, p2, p3]) print("Shake sort ok")
def test_shake_sort(): ''' test for SHAKE_SORT ''' l = [2, 1, 3] Sorting.sort(l, algorithm = Algorithm.SHAKE_SORT) assert(l == [1, 2, 3]) Sorting.sort(l, reverse = True, algorithm = Algorithm.SHAKE_SORT) assert (l == [3,2,1] ) l = [2,1,2,3,1] Sorting.sort(l, algorithm = Algorithm.SHAKE_SORT) assert l == [1,1,2,2,3] Sorting.sort(l, reverse = True, algorithm = Algorithm.SHAKE_SORT) assert l == [3,2,2,1,1] p1 = Person("p1", 1) p2 = Person("p2", 2) p3 = Person("p3", 3) p4 = Person("p1", 2) # sort by name asc l = [p3, p2, p1, p4] Sorting.sort(l, key=lambda x:x.name, algorithm = Algorithm.SHAKE_SORT) assert(l == [p1, p4, p2, p3]) # sort by age desc l = [p3, p2, p4, p1] Sorting.sort(l, key=lambda x:x.age, reverse=True, algorithm = Algorithm.SHAKE_SORT) assert(l == [p3, p2, p4, p1]) # sort by age asc l = [p3, p2, p4, p1] Sorting.sort(l, key=lambda x:x.age, algorithm = Algorithm.SHAKE_SORT) assert(l == [p1, p2, p4, p3]) # sort by name,age ascending l = [p3, p2, p1, p4] Sorting.sort(l, key=lambda x:(x.name, x.age), algorithm = Algorithm.SHAKE_SORT) assert(l == [p1, p4, p2, p3]) # sort by name ascending and by age descending def person_less_than(p1, p2): """name ascending, age descending """ if p1.name == p2.name: return p1.age > p2.age return p1.name < p2.name Person.__lt__ = person_less_than Person.__gt__ = not person_less_than l = [p3, p2, p1, p4] Sorting.sort(l, algorithm = Algorithm.SHAKE_SORT) assert(l == [p4, p1, p2, p3]) print("Shake sort ok")