0% found this document useful (0 votes)
3 views

Lab3_22130037_TruongTanDat.ipynb - Colab

Uploaded by

truongdat531
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab3_22130037_TruongTanDat.ipynb - Colab

Uploaded by

truongdat531
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

23:01 14/10/24 Lab3_22130037_TruongTanDat.

ipynb - Colab

from abc import ABC


class Shape(ABC):
def __init__(self, point):
self.point = point
def p(self):
pass
def area(self):
pass
def distanceToO(self):
pass

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

e1 = Employee("01", "Dat", 2004, 50000)


e2 = Employee("02", "Anh", 1990, 60000)
e3 = Employee("03", "Phuong", 1985, 70000)
e4 = Employee("04", "Thuy", 1999, 80000)

department = Department("IT", [e1, e2, e3, e4])


print(f"Nhân viên sinh năm 1985: {department.count_employees(2004)}")
print(f"Nhân viên sinh năm già nhất: {department.oldEmployee()}")
print(f"Thống kê theo năm sinh: {department.statByBirthYear()}")

Nhân viên sinh năm 1985: 1


Nhân viên sinh năm già nhất: id=03,name=Phuong,birthYear=1985,salaryRate=70000
Thống kê theo năm sinh: {2004: 1, 1990: 1, 1985: 1, 1999: 1}

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)

# 1) Determine the type of each publication (Magazine or Reference).


def publication_type(self):
publication_Types = {}
for publication in self.books:
if isinstance(publication, Magazine):
publication_Types[publication.title] = 'Magazine'
elif isinstance(publication, ReferenceBook):
publication_Types[publication.title] = 'Reference Book'
return publication_Types
# 2) Whether a publication is a magazine and was published 10 years ago (from 2024).
def magazine_published_10_years_ago(self):
ten_years_ago = 2024 - 10
ofd_magazine = [publication.title for publication in self.books if isinstance(publication, Magazine) and publication.yearsOfPublicat
print(ofd_magazine)

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

#4) Compute the cost of all publications in the bookstore.


def sum_cost (self):
return sum(publication.price for publication in self.books)

#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

#7) Find all magazines published in a given year.


def find_magazines_by_year(self, year):
magazines = [publication.title for publication in self.books if isinstance(publication, Magazine) and publication.yearsOfPublication
return magazines

m1 = Magazine("Tech Today", 50, 2014, "John Doe", 16000, "Tech Monthly")


m2 = Magazine("Science Weekly", 40, 2010, "Jane Doe", 13000, "Science World")

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)

print("#1: Loại ấn phẩm: ",bookstore.publication_type())


print("#2: Tạp chí xuất bản cách đây 10 năm:",bookstore.magazine_published_10_years_ago())
print("#3:Kiểm tra2 ấn phẩm có cùng loại và cùng tác giả không: ",bookstore.check_author(r1, r2))
print("#4 Tổng chi phí của tất cả ấn phẩm:", bookstore.sum_cost())
print("#5) Sách tham khảo với chương có số trang nhiều nhất:", bookstore.referenceBook_with_most_pages())
print("#6) Tạp chí 'Tech Monthly' có tồn tại:", bookstore.check_magazine_in_theBookstore("Tech Monthly"))
print("#7) Tạp chí xuất bản trong năm 2010:", bookstore.find_magazines_by_year(2010))

#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

You might also like