From 201c223e694be92c63e5c2d4e718f7994b271362 Mon Sep 17 00:00:00 2001 From: Abimbola Ronald Date: Mon, 24 Jul 2023 06:39:02 -0700 Subject: [PATCH] Advanced Task --- python-classes/100-singly_linked_list.py | 77 ++++++++++++++++++++++++ python-classes/102-square.py | 54 +++++++++++++++++ python-classes/103-magic_class.py | 27 +++++++++ 3 files changed, 158 insertions(+) create mode 100755 python-classes/100-singly_linked_list.py create mode 100755 python-classes/102-square.py create mode 100755 python-classes/103-magic_class.py diff --git a/python-classes/100-singly_linked_list.py b/python-classes/100-singly_linked_list.py new file mode 100755 index 0000000..0ffc189 --- /dev/null +++ b/python-classes/100-singly_linked_list.py @@ -0,0 +1,77 @@ +#!/usr/bin/python3 +"""This module define classes for singly-linked list impementation""" + + +class Node: + """Objs of this clsass represents a node in a singly-linked list.""" + + def __init__(self, data, next_node=None): + """Initialize a new Node. + + Args: + data (int): The data of the new Node. + next_node (Node): The next node of the new Node. + """ + self.__data = data + self.__next_node = next_node + + @property + """property(get&set) for the data of the Node.""" + def data(self): + return self.__data + + @data.setter + def data(self, value): + if (not isinstance(value, int)): + raise TypeError("data must be an integer") + self.__data = value + + @property + """property(get&set) for the next_node.""" + def next_node(self): + return self.__next_node + + @next_node.setter + def next_node(self, value): + if (not isinstance(value, Node) and value is not None): + raise TypeError("next_node must be a Node object") + self.__next_node = value + +class SinglyLinkedList: + """This class represents a singly-linked list.""" + + def __init__(self): + """Initialize a new SinglyLinkedList.""" + self.__head = None + + def sorted_insert(self, value): + """Insert a new Node to the SinglyLinkedList. + + The node is inserted into the list at the sorted + position in the list (increasing order) + + Args: + value (Node): The new Node to insert. + """ + new_node = Node(value) + if self.__head == None: + self.__head = new_node + elif self.__head.data > new_node.data: + new_node.next_node = self.__head + self.__head = new_node + else: + current_node = self.__head + while (current_node.next_node is not None and + current_node.next_node.data < value): + current_node = current_node.next_node + new_node.next_node = current_node.next_node + current_node.next_node = new_node + + def __str__(self): + """Defines the print() representation of a SinglyLinkedList obj""" + node_list = [] + current_node = self.__head + while (current_node is not None): + node_list.append(str(current_node.data)) + current_node = current_node.next_node + return '\n'.join(node_list) diff --git a/python-classes/102-square.py b/python-classes/102-square.py new file mode 100755 index 0000000..5ffccfa --- /dev/null +++ b/python-classes/102-square.py @@ -0,0 +1,54 @@ +#!/usr/bin/python3 +"""Define a class Square.""" + + +class Square: + """A class that defines a square""" + def __init__(self, size=0): + """Initialize a new square. + + Args: + size (int): The size of the new square. + """ + self.__size = size + + @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) and not isinstance(value, float)): + raise TypeError('size must be a number') + 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() + + def __ne__(self, other_square): + """Returns the != comparison to a Square.""" + return self.area() != other_square.area() + + def __lt__(self, other_square): + """Returns the < comparison to a Square.""" + return self.area() < other_square.area() + + def __le__(self, other_square): + """Returns the <= comparison to a Square.""" + return self.area() <= other_square.area() + + def __gt__(self, other_square): + """Returns the > comparison to a Square.""" + return self.area() > other_square.area() + + def __ge__(self, other_square): + """Returns the >= compmarison to a Square.""" + return self.area() >= other_square.area() diff --git a/python-classes/103-magic_class.py b/python-classes/103-magic_class.py new file mode 100755 index 0000000..5a64549 --- /dev/null +++ b/python-classes/103-magic_class.py @@ -0,0 +1,27 @@ +#!/usr/bin/python3 +"""Define a MagicClass that does exactly as the bytecode provided.""" + +import math + + +class MagicClass: + """Represent a circle.""" + + def __init__(self, radius=0): + """Initialize a MagicClass. + + Arg: + radius (float or int): The radius of the new MagicClass. + """ + self.__radius = 0 + if type(radius) is not int and type(radius) is not float: + raise TypeError("radius must be a number") + self.__radius = radius + + def area(self): + """Return the area of the MagicClass.""" + return (self.__radius ** 2 * math.pi) + + def circumference(self): + """Return The circumference of the MagicClass.""" + return (2 * math.pi * self.__radius)