Implement a class named Rectangle
to store the coordinates of a rectangle given
by its top-left point P1(x1, y1) and bottom-right point P2(x2, y2).
width()
and height()
methods which return, respectively, the width and height of a rectangle.
area
to return an area of a rectangle.
circumference
to return the perimeter of the rectangle.
__str__
method such that when you print one of the objects its coordinates (x1,y1), (x2, y2) are printed.
First, implement a class Product
representing products to store in a fridge:
name
- product name, qty
- quantity, unit
(piece, kilogram, liter, etc.)
inc(self, by)
increases the quantity of the product by the by
value.
use(self, qty)
decreases the quantity of the product by the qty
value. You have to take care of the case when the required quantity is greater than the available quantity of the product.
__str__(self)
.
Then, implement a class Fridge
to represent a collection of products (food) in a fridge.
Product
objects.add(self, product_name, qty, unit)
appends a new item to the list of products.inc(self, product_name, by)
increases an amount of food already present in the fridge by the by
value.use(self, product_name, qty)
decreases an amount of food present in the fridge by the qty
value. In case you use the whole amount of the product, delete its corresponding Product
object from the list.__str__(self)
lists all items/products (incl. quantity and unit) stored in the fridge.
if __name__ == "__main__": fridge = Fridge() fridge.add("tomatoes", 10, "pieces") fridge.add("onions", 3, "pieces") fridge.add("eggs", 12, "pieces") fridge.add("butter", 500, "grams") fridge.add("oil", 1, "liter") print(fridge) fridge.inc("onions", 8) print(fridge) fridge.use("butter", 200) print(fridge) fridge.use("butter", 500) print(fridge)
Fridge: ...10 pieces of tomatoes ...3 pieces of onions ...12 pieces of eggs ...500 grams of butter ...1 liter of oil Adding 8 pieces of onions. Fridge: ...10 pieces of tomatoes ...11 pieces of onions ...12 pieces of eggs ...500 grams of butter ...1 liter of oil Using 200 grams of butter. Fridge: ...10 pieces of tomatoes ...11 pieces of onions ...12 pieces of eggs ...300 grams of butter ...1 liter of oil Using 300 grams of butter. Fridge: ...10 pieces of tomatoes ...11 pieces of onions ...12 pieces of eggs ...1 liter of oil
Create a class representing playing cards (standard 52-card deck). A card (object) is described with 2 pieces of information:
Notes:
J
, Q
, K
, A
)
C
, D
, H
, S
)
__str__
should output full names of ranks and suits (not acronyms). Examples:
>>> print(Card('A', 'S')) Card: ace of spades >>> print(Card(2, 'D')) Card: 2 of diamonds >>> print(Card('K', 'H')) Card: king of hearts >>> print(Card(9, 'C')) Card: 9 of clubs
__repr__
is need for printing a list of objects. Re-use the already implemented __str__
method.
__eq__
and __lt__
are needed for the .sort()
method to work.
Required filename: 12_weekly_hw.py
.
class Card: def __init__(self, rank, suit): def __str__(self): def __repr__(self): return str(self) def __eq__(self, other): def __lt__(self, other): if __name__ == "__main__": cards = [] cards.append(Card('A', 'D')) cards.append(Card(10, 'S')) cards.append(Card('K', 'H')) cards.append(Card('A', 'C')) cards.append(Card(3, 'S')) cards.append(Card(3, 'D')) print(cards) cards.sort() print(cards)
[Card: ace of diamonds, Card: 10 of spades, Card: king of hearts, Card: ace of clubs, Card: 3 of spades, Card: 3 of diamonds] [Card: 3 of diamonds, Card: 3 of spades, Card: 10 of spades, Card: king of hearts, Card: ace of clubs, Card: ace of diamonds]