Lab3_22130037_TruongTanDat.ipynb - Colab
Lab3_22130037_TruongTanDat.ipynb - Colab
ipynb - Colab
class Point():
def __init__(self,x,y):
self.x=x
self.y=y
def __gt__(self,other):
return self.x*self.x + self.y*self.y > other.x*other.x + other.y*other.y
def __lt__(self,other):
return self.x*self.x + self.y*self.y < other.x*other.x + other.y*other.y
def __eq__(self,other):
return self.x*self.x + self.y*self.y == other.x*other.x + other.y*other.y
def __str__(self):
return "("+str(self.__x)+","+str(self.__y)+")"
p1 = Point(3,4)
p2 = Point(5,6)
print(p1>p2)
print(p1<p2)
print(p1==p2)
https://colab.research.google.com/drive/14nYMQEkhIbaT3ozP1C2I-0ILV6u7PC2p#scrollTo=aR41Kz1dw2g9&printMode=true 1/5
23:01 14/10/24 Lab3_22130037_TruongTanDat.ipynb - Colab
False
True
False
class Square(Shape):
def __init__(self,point,size):
super().__init__(point)
self.size=size
def __str__(self):
return "Square("+str(self.size)+")"
def p(self):
return self.size*4
def area(self):
return self.size*self.size
def inside(self, p):
return (self.point.x <= p.x <= self.point.x + self.size) and (self.point.y <= p.y <= self.point.y + self.size)
squa = Square(Point(5,8),4)
p_squa = Point(7,9)
print(squa.p())
print(squa.area())
print(squa.inside(p_squa))
16
16
True
class Rectangle(Shape):
def __init__(self,point,w,h):
super().__init__(point)
self.w=w
self.h=h
def __str__(self):
return "Rectangle("+str(self.w)+","+str(self.h)+")"
def p(self):
return (self.w+self.h)*2
def area(self):
return self.w*self.h
def inside(self, p):
return (self.point.x <= p.x <= self.point.x + self.w) and (self.point.y <= p.y <= self.point.y + self.h)
rec = Rectangle(Point(2,3),4,5)
p_rec = Point(4,5)
print(rec.p())
print(rec.area())
print(rec.inside(p_rec))
18
20
True
class Cricle(Shape):
def __init__(self,point, r):
super().__init__(point)
self.r=r
def __str__(self):
return "Cricle("+str(self.r)+")"
def p(self):
return 3.14*self.r*2
def area(self):
return 3.14*self.r*self.r
def inside(self, p):
return (self.point.x - p.x) ** 2 + (self.point.y - p.y) ** 2 <= self.r ** 2
cri = Cricle(Point(2,3),4)
p_cri = Point(4,5)
print(cri.p())
print(cri.area())
print(cri.inside(p_cri))
25.12
50.24
True
https://colab.research.google.com/drive/14nYMQEkhIbaT3ozP1C2I-0ILV6u7PC2p#scrollTo=aR41Kz1dw2g9&printMode=true 2/5
23:01 14/10/24 Lab3_22130037_TruongTanDat.ipynb - Colab
class Employee:
def __init__(self,id,name,birthYear,salaryRate):
self.id=id
self.name=name
self.birthYear=birthYear
self.salaryRate=salaryRate
def __str__(self):
return f"id={self.id},name={self.name},birthYear={self.birthYear},salaryRate={self.salaryRate}"
def getbirthYear(self):
return self.birthYear
class Department:
def __init__(self,name,employees):
self.name=name
self.employees=employees
# 1) countEmployees(int year): determine the number of employees having birth
# year equals to a given year
def count_employees(self,year):
# return len([emp for emp in self.__employees if emp.getbirthYear() == year])
count = sum(1 for employee in self.employees if employee.birthYear == year)
return count
# 2)findOldestEmployee(): find the oldest employee in the department
def oldEmployee(self):
return min(self.employees, key=lambda emp: emp.getbirthYear())
# 3)statByBirthYear(): to make a statistic on the number of employees by
# birth year,the key is birth year and the value is the number of students.
def statByBirthYear(self):
birthYear_stat = {}
for employee in self.employees:
if employee.birthYear in birthYear_stat:
birthYear_stat[employee.birthYear] += 1
else:
birthYear_stat[employee.birthYear] = 1
return birthYear_stat
https://colab.research.google.com/drive/14nYMQEkhIbaT3ozP1C2I-0ILV6u7PC2p#scrollTo=aR41Kz1dw2g9&printMode=true 3/5
23:01 14/10/24 Lab3_22130037_TruongTanDat.ipynb - Colab
class publications:
def __init__(self, title, numberOfPages, yearsOfPublication, author, price):
self.title = title
self.numberOfPages = numberOfPages
self.yearsOfPublication = yearsOfPublication
self.author = author
self.price = price
def __str__(self):
return f"Title: {self.title}\nNumber of Pages: {self.numberOfPages}\nYears of Publication: {self.yearsOfPublication}\nAuthor: {self.
class Magazine(publications):
def __init__(self, title, numberOfPages, yearsOfPublication, author, price, name):
super().__init__(title, numberOfPages, yearsOfPublication, author, price)
self.name = name
class ReferenceBook(publications):
def __init__(self, title, numberOfPages, yearsOfPublication, author, price, field, chapters):
super().__init__(title, numberOfPages, yearsOfPublication, author, price)
self.field = field
self.chapters = chapters
class chapters:
def __init__(self, title, numberOfPage):
self.title = title;
self. numberOfPage = numberOfPage
class Bookstore():
def __init__(self):
self.books = []
def add_publication(self, publication):
self.books.append(publication)
https://colab.research.google.com/drive/14nYMQEkhIbaT3ozP1C2I-0ILV6u7PC2p#scrollTo=aR41Kz1dw2g9&printMode=true 4/5
23:01 14/10/24 Lab3_22130037_TruongTanDat.ipynb - Colab
#3) Whether two publications are of the same type and by the same author.
def check_author(self, publication1, publication2):
if isinstance(publication1, type(publication2)) and publication1.author == publication2.author:
return True
else:
return False
#5) Find the reference book with the chapter containing the most pages.
def referenceBook_with_most_pages(self):
max_chapters = None
max_pages = 0
for publication in self.books:
if isinstance(publication, ReferenceBook):
# Access the "num_pages" key from the dictionaries within publication.chapters
total_pages = sum(chapter["num_pages"] for chapter in publication.chapters)
if total_pages > max_pages:
max_pages = total_pages
max_chapters = (publication.title, total_pages)
return max_chapters
#6) Check whether a magazine is in the bookstore based on its name.
def check_magazine_in_theBookstore(self, magazine_name):
for publication in self.books:
if isinstance(publication, Magazine) and publication.name == magazine_name:
return True
return False
r1 = ReferenceBook("Python Programming", 300, 2018, "Guido van Rossum", 45000, "Khoa học máy tính",
[{"title": "Giới thiệu Python", "num_pages": 50}, {"title": "Python nâng cao", "num_pages": 75}])
r2 = ReferenceBook("Java core", 400, 2016, "Dr. Smith", 60000, "Ngôn ngữ Java",
[{"title": "Java basic", "num_pages": 100}, {"title": "Java swing", "num_pages": 150}])
bookstore = Bookstore()
bookstore.add_publication(m1)
bookstore.add_publication(m2)
bookstore.add_publication(r1)
bookstore.add_publication(r2)
#1: Loại ấn phẩm: {'Tech Today': 'Magazine', 'Science Weekly': 'Magazine', 'Python Programming': 'Reference Book', 'Java core': 'Re
['Tech Today']
#2: Tạp chí xuất bản cách đây 10 năm: None
#3:Kiểm tra2 ấn phẩm có cùng loại và cùng tác giả không False
#4 Tổng chi phí của tất cả ấn phẩm: 134000
#5) Sách tham khảo với chương có số trang nhiều nhất: ('Java core', 250)
#6) Tạp chí 'Tech Monthly' có tồn tại: True
#7) Tạp chí xuất bản trong năm 2010: ['Science Weekly']
https://colab.research.google.com/drive/14nYMQEkhIbaT3ozP1C2I-0ILV6u7PC2p#scrollTo=aR41Kz1dw2g9&printMode=true 5/5