Skip to content

Commit

Permalink
Advanced Task
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronnie5562 committed Jul 24, 2023
1 parent dc7fbd8 commit 201c223
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
77 changes: 77 additions & 0 deletions python-classes/100-singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -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)
54 changes: 54 additions & 0 deletions python-classes/102-square.py
Original file line number Diff line number Diff line change
@@ -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()
27 changes: 27 additions & 0 deletions python-classes/103-magic_class.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 201c223

Please sign in to comment.