In this project, I continued to practice object-oriented programming in Python.
I learned about class methods, static methods, class vs instance attributes, and
how to use the special __str__
and __repr__
methods.
- tests: Folder of test files. Provided by ALX.
-
0. Simple rectangle
- 0-rectangle.py: Empty Python class that defines a rectangle.
-
1. Real definition of a rectangle
- 1-rectangle.py: Python class that defines a rectangle. Builds on
0-rectangle.py with:
- Private instance attribute
width
. - Property getter
def width(self):
to getwidth
. - Property setter
def width(self, value):
to setwidth
. - Private instance attribute
height
. - Property getter
def height(self):
to getheight
. - Property setter
def height(self, value):
to setheight
. - Instantiation with optional
width
andheight
:def __init(self, width=0, height=0):
- Private instance attribute
- If either of
width
orheight
is not an integer, aTypeError
is raised with the messagewidth must be an integer
orheight must be an integer
. - If either of
width
orheight
is less than0
, aValueError
is raised with the messagewidth must be >= 0
orheight must be >= 0
.
- 1-rectangle.py: Python class that defines a rectangle. Builds on
0-rectangle.py with:
-
2. Area and Perimeter
- 2-rectangle.py: Python class that defines a rectangle. Builds on
1-rectangle.py with:
- Public instance method
def area(self):
that returns the area of the rectangle. - Public instance attribute
def perimeter(self):
that returns the permiter of the rectangle (if either ofwidth
orheight
equals0
, the perimeter is0
).
- Public instance method
- 2-rectangle.py: Python class that defines a rectangle. Builds on
1-rectangle.py with:
-
3. String representation
- 3-rectangle.py: Python class that defines a rectangle. Builds on
2-rectangle.py with:
- Special method
__str__
to print the rectangle with the#
character (if either ofwidth
orheight
equals0
, the method returns an empty string.).
- Special method
- 3-rectangle.py: Python class that defines a rectangle. Builds on
2-rectangle.py with:
-
4. Eval is magic
- 4-rectangle.py: Python class that defines a rectangle. Builds on
3-rectangle.py with:
- Special method
__repr__
to return a string representation of the rectangle.
- Special method
- 4-rectangle.py: Python class that defines a rectangle. Builds on
3-rectangle.py with:
-
5. Detect instance deletion
- 5-rectangle.py: Python class that defines a rectangle. Builds on
4-rectangle.py with:
- Special method
__del__
that prints the messageBye rectangle...
when aRectangle
is deleted.
- Special method
- 5-rectangle.py: Python class that defines a rectangle. Builds on
4-rectangle.py with:
-
6. How many instances
- 6-rectangle.py: Python class that defines a rectangle. Builds on
5-rectangle.py with:
- Public class attribute
number_of_instances
that is initialized to0
, incremented for each new instantiation, and decremened for each instance deletion.
- Public class attribute
- 6-rectangle.py: Python class that defines a rectangle. Builds on
5-rectangle.py with:
-
7. Change representation
- 7-rectangle.py: Python class that defines a rectangle. Builds on
6-rectangle.py with:
- Public class attribute
class_symbol
that is initialized to#
but can be any type - used as the symbol for string representation.
- Public class attribute
- 7-rectangle.py: Python class that defines a rectangle. Builds on
6-rectangle.py with:
-
8. Compare rectangles
- 8-rectangle.py: Python class that defines a rectangle. Builds on
7-rectangle.py with:
- Static method
def bigger_or_equal(rect_1, rect_2):
that returns the rectangle with the greater area (returnsrect_1
if both areas are equal). - If either of
rect_1
orrect_2
is not aRectangle
instance, aTypeError
is raised with the messagerect_1 must be an instance of Rectangle
orrect_2 must be an instance of Rectangle
.
- Static method
- 8-rectangle.py: Python class that defines a rectangle. Builds on
7-rectangle.py with:
-
9. A square is a rectangle
- 9-rectangle.py: Python class that defines a rectangle. Builds on
8-rectangle.py with:
- Class method
def square(cls, size=0):
that returns a newRectangle
instance withwidth == height == size
.
- Class method
- 9-rectangle.py: Python class that defines a rectangle. Builds on
8-rectangle.py with:
-
10. N Queens
- 101-nqueens.py: Python program that solves the N queens puzzle.
- Usage:
./101-nqueens.py N
- Determines all possible solutions for placing N non-attacking queens on an NxN chessboard.
- Exactly two arguments must be provided. Otherwise, the program prints
Usage: nqueens N
and exits with the status1
. - If the provided
N
is not an integer, the program printsN must be a number
and exits with the status1
. - If the provided
N
is less than4
, the program printsN must be at least 4
and exits with the status1
. - Solutions are printed one per line in the format
[[r, c], [r, c], [r, c], [r, c]]
wherer
andc
represent the row and column, respectively, where a queen must be placed.