From 89956b8afb0b49fb0ac32c1e66a8375df67c5760 Mon Sep 17 00:00:00 2001 From: Abimbola Ronald Date: Mon, 24 Jul 2023 08:53:39 -0700 Subject: [PATCH] Advanced Task --- python-classes/100-singly_linked_list.py | 2 +- python-classes/101-square.py | 70 ++++++++++++++++++++++++ python-classes/102-square.py | 6 +- 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100755 python-classes/101-square.py diff --git a/python-classes/100-singly_linked_list.py b/python-classes/100-singly_linked_list.py index 0ffc189..062f5f2 100755 --- a/python-classes/100-singly_linked_list.py +++ b/python-classes/100-singly_linked_list.py @@ -27,8 +27,8 @@ def data(self, value): self.__data = value @property - """property(get&set) for the next_node.""" def next_node(self): + """property(get&set) for the next_node.""" return self.__next_node @next_node.setter diff --git a/python-classes/101-square.py b/python-classes/101-square.py new file mode 100755 index 0000000..1c8c587 --- /dev/null +++ b/python-classes/101-square.py @@ -0,0 +1,70 @@ +#!/usr/bin/python3 +"""Define a class Square.""" + + +class Square: + """A class that defines a square.""" + + def __init__(self, size=0, position=(0, 0)): + """Initialize a new square. + + Args: + size (int): The size of the new square. + position (int, int): The position of the new square. + """ + self.size = size + self.position = position + + @property + def size(self): + """property(get&set) for the current size of the square.""" + return (self.__size) + + @size.setter + def size(self, value): + if not isinstance(value, int): + raise TypeError("size must be an integer") + elif value < 0: + raise ValueError("size must be >= 0") + self.__size = value + + @property + def position(self): + """property(get&set) for the current position of the square.""" + return (self.__position) + + @position.setter + def position(self, value): + if (not isinstance(value, tuple) or + len(value) != 2 or + not all(isinstance(num, int) for num in value) or + not all(num >= 0 for num in value)): + raise TypeError("position must be a tuple of 2 positive integers") + self.__position = value + + def area(self): + """Return the current area of the square.""" + return (self.__size * self.__size) + + def my_print(self): + """Prints in stdout the square with the character #.""" + if self.__size == 0: + print("") + return + + [print("") for i in range(0, self.__position[1])] + for i in range(0, self.__size): + [print(" ", end="") for x in range(self.__position[0])] + [print("#", end="") for y in range(self.__size)] + print() + + def __str__(self): + """The print() representation for a Square obj.""" + if self.__size != 0: + [print() for i in range(0, self.__position[1])] + for i in range(0, self.__size): + [print(" ", end="") for j in range(self.__position[0])] + [print("#", end="") for k in range(self.__size)] + if i != self.__size - 1: + print() + return ("") diff --git a/python-classes/102-square.py b/python-classes/102-square.py index 5ffccfa..40435d9 100755 --- a/python-classes/102-square.py +++ b/python-classes/102-square.py @@ -16,7 +16,7 @@ def __init__(self, size=0): def size(self): """property(get&set) for the current size of the square.""" return self.__size - + @size.setter def size(self, value): if (not isinstance(value, int) and not isinstance(value, float)): @@ -24,11 +24,11 @@ def size(self, value): if (size < 0): raise ValueError('size must be >= 0') self.__size = value - + def area(self): """Returns the area of the current square.""" return (self.__size**2) - + def __eq__(self, other_square): """Returns the == comparision to a Square.""" return self.area() == other_square.area()