python-playground/playground.py
2024-02-06 15:28:03 +01:00

91 lines
2.0 KiB
Python

class Thing:
def __init__(self, weight):
self.weight = weight
def getWeigt(self):
return self.weight
class Book(Thing):
def __init__(self, weight, pages):
super().__init__(weight)
self.pages = []
self.numberOfPages = pages
def getNoOfPages(self) -> int:
return self.numberOfPages
def setPages(self, newPages):
self.numberOfPages = newPages
def addPage(self, string):
self.pages.append(string)
def getPages(self) -> []:
return self.pages
def is_list_sorted(list, compare_func) -> bool:
index = 0
list_len = len(list)
for _ in list[:list_len-1]:
if compare_func(list[index], list[index+1]) is True:
index += 1
else:
break
return list_len - 1 == index
def sort(list, compare_func) -> []:
sorted_list = list.copy()
list_len = len(sorted_list)
while is_list_sorted(sorted_list, compare_func) is False:
index = 0
for _ in sorted_list[:list_len-1]:
ret = compare_func(sorted_list[index], sorted_list[index+1])
if ret is False:
buf = sorted_list[index]
sorted_list[index] = sorted_list[index + 1]
sorted_list[index + 1] = buf
else:
pass
index += 1
return sorted_list
def main():
book1 = Book(500, 29)
book1.setPages(50)
print("Book 1 has %d pages" % book1.getNoOfPages())
print("Book 1 weighs %d grams" % book1.getWeigt())
book2 = Book(333, 50)
print("Book 2 has %d pages" % book2.getNoOfPages())
book2.addPage("Page1")
book2.addPage("Page2")
book2.addPage("Page3")
pages = book2.getPages()
for page in pages:
print(page)
numbers = [1, 5, 3, 7, 10, 9]
def sorter(a, b): return a >= b
ordered = sort(numbers, sorter)
print("Before: " + str(numbers))
print("After: " + str(ordered))
# sort(numbers, sorter)
if __name__ == '__main__':
main()