From fcefea3bc02fe7a52bccb166ada29eb036c075e4 Mon Sep 17 00:00:00 2001 From: anavitor97 Date: Sun, 31 Oct 2021 15:39:06 -0400 Subject: [PATCH 1/3] adding homework 5 --- homework/HW5/animal.py | 57 +++++++++++++++++++++ homework/HW5/binary_search_tree.py | 82 ++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 homework/HW5/animal.py create mode 100644 homework/HW5/binary_search_tree.py diff --git a/homework/HW5/animal.py b/homework/HW5/animal.py new file mode 100644 index 0000000..944999d --- /dev/null +++ b/homework/HW5/animal.py @@ -0,0 +1,57 @@ +#!/usr/bin/python3 + + +class Animal: + + # a class attribute of the valid species in our universe + valid_species = { + 'cat', + 'dog', + 'duck', + 'elf', + 'goblin', + 'horse', + 'human', + 'mermaid', + 'nightingale', + 'pig', + 'swan', + 'wolf' + } + + + def __init__(self, name, species): + self.name = name + self._species = species + + + def __repr__(self): + return f'{self.name} ({self._species})' + + @property + def species(self): + return self._species + + ##Add another method species(self, into) to the Animal class. + ##This method should set the _species attribute of this Animal to into, + ##but before that, it makes sure that into is in the valid_species + ##list. If into is not in the valid_species list, raise an Exception. + ##Apply the species.setter decorator to this method. + + @species.setter + def species(self, into): + assert into in Animal.valid_species, Exception(f'invalid species: {into}') + self._species = into + + +>>> dog = Animal('Fido', 'dog') +>>> vars(dog) + +>>> dog = Animal('Fido', 'dog') +>>> dog.species + +>>> dog.species = 'cat' +>>> dog.species + +>>> dog.species = 'TheThing' + diff --git a/homework/HW5/binary_search_tree.py b/homework/HW5/binary_search_tree.py new file mode 100644 index 0000000..78026d7 --- /dev/null +++ b/homework/HW5/binary_search_tree.py @@ -0,0 +1,82 @@ +#!/usr/bin/python3 + + +class BSTNode: + + def __init__(self, key, val): + self.key, self.val = key, val + self.left = None + self.right = None + self.size = 1 + + def __str__(self): + return f'BSTNode({self.key}, {self.val})' + \ + '\n|\n|-(L)->' + '\n| '.join(str(self.left ).split('\n')) + \ + '\n|\n|-(R)->' + '\n| '.join(str(self.right).split('\n')) + + +class BSTTable: + def __init__(self): + self._root = None + + def __str__(self): + return str(self._root) + + def __len__(self): + return self._size(self._root) + + def put(self, key, val): + self._root = self._put(self._root, key, val) + + def get(self, key): + return self._get(self._root, key) + + def _put(self, node, key, val): + if node is None: + return BSTNode(key,val) + elif key == node.key: + node.val = val + elif node.key < key: + node.right = self._put(node.right, key, val) + else: #node.key > key : + node.left = self._put(node.left, key, val) + + node.size = self._size(node.right) + self._size(node.left) + 1 + return node + + def _get(self, node, key): + if node is None: + raise KeyError("The node with key", key, "doesn't exist yet, create one before") + elif node.key == key: + return node.val + elif node.key < key: + return self._get(node.right, key) + #return node.right.val + else: #node.key > key : + return self._get(node.left, key) + #return node.left.val + #pass # TODO + + @staticmethod + def _size(node): + return node.size if node else 0 + + # The put method inserts a key-value pair into the BSTTable. + # It delegates the function call to _put. Please implement the non-public + # method _put. It takes a node, a key, and a val as arguments. node is the root of + # the subtree into which the key-value pair is to be inserted. After the insertion, + # the root of this subtree may change. _put should return the new root of this subtree. + # The difference of the input subtree and the returned subtree is that in the returned + # subtree, a new key-value pair has been inserted. If the key was already in the subtree, + # then we update the value of the corresponding node. + + +greektoroman = BSTTable() +print(greektoroman.put('Athena', 'Minerva')) +print(greektoroman.put('Eros', 'Cupid')) +print(greektoroman.put('Aphrodite', 'Venus')) +print(greektoroman.get('Eros')) +print(greektoroman.get('Vitoria')) +print(greektoroman.get('Lala')) + +print (greektoroman) \ No newline at end of file From fb698604dd0cd52aadfc3a97a881ce35ca3e3537 Mon Sep 17 00:00:00 2001 From: anavitor97 Date: Sun, 31 Oct 2021 15:41:14 -0400 Subject: [PATCH 2/3] adding Final folder for HW5 --- homework/HW5/HW5-final/animal.py | 57 ++++++++++++++ homework/HW5/HW5-final/binary_search_tree.py | 82 ++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 homework/HW5/HW5-final/animal.py create mode 100644 homework/HW5/HW5-final/binary_search_tree.py diff --git a/homework/HW5/HW5-final/animal.py b/homework/HW5/HW5-final/animal.py new file mode 100644 index 0000000..944999d --- /dev/null +++ b/homework/HW5/HW5-final/animal.py @@ -0,0 +1,57 @@ +#!/usr/bin/python3 + + +class Animal: + + # a class attribute of the valid species in our universe + valid_species = { + 'cat', + 'dog', + 'duck', + 'elf', + 'goblin', + 'horse', + 'human', + 'mermaid', + 'nightingale', + 'pig', + 'swan', + 'wolf' + } + + + def __init__(self, name, species): + self.name = name + self._species = species + + + def __repr__(self): + return f'{self.name} ({self._species})' + + @property + def species(self): + return self._species + + ##Add another method species(self, into) to the Animal class. + ##This method should set the _species attribute of this Animal to into, + ##but before that, it makes sure that into is in the valid_species + ##list. If into is not in the valid_species list, raise an Exception. + ##Apply the species.setter decorator to this method. + + @species.setter + def species(self, into): + assert into in Animal.valid_species, Exception(f'invalid species: {into}') + self._species = into + + +>>> dog = Animal('Fido', 'dog') +>>> vars(dog) + +>>> dog = Animal('Fido', 'dog') +>>> dog.species + +>>> dog.species = 'cat' +>>> dog.species + +>>> dog.species = 'TheThing' + diff --git a/homework/HW5/HW5-final/binary_search_tree.py b/homework/HW5/HW5-final/binary_search_tree.py new file mode 100644 index 0000000..78026d7 --- /dev/null +++ b/homework/HW5/HW5-final/binary_search_tree.py @@ -0,0 +1,82 @@ +#!/usr/bin/python3 + + +class BSTNode: + + def __init__(self, key, val): + self.key, self.val = key, val + self.left = None + self.right = None + self.size = 1 + + def __str__(self): + return f'BSTNode({self.key}, {self.val})' + \ + '\n|\n|-(L)->' + '\n| '.join(str(self.left ).split('\n')) + \ + '\n|\n|-(R)->' + '\n| '.join(str(self.right).split('\n')) + + +class BSTTable: + def __init__(self): + self._root = None + + def __str__(self): + return str(self._root) + + def __len__(self): + return self._size(self._root) + + def put(self, key, val): + self._root = self._put(self._root, key, val) + + def get(self, key): + return self._get(self._root, key) + + def _put(self, node, key, val): + if node is None: + return BSTNode(key,val) + elif key == node.key: + node.val = val + elif node.key < key: + node.right = self._put(node.right, key, val) + else: #node.key > key : + node.left = self._put(node.left, key, val) + + node.size = self._size(node.right) + self._size(node.left) + 1 + return node + + def _get(self, node, key): + if node is None: + raise KeyError("The node with key", key, "doesn't exist yet, create one before") + elif node.key == key: + return node.val + elif node.key < key: + return self._get(node.right, key) + #return node.right.val + else: #node.key > key : + return self._get(node.left, key) + #return node.left.val + #pass # TODO + + @staticmethod + def _size(node): + return node.size if node else 0 + + # The put method inserts a key-value pair into the BSTTable. + # It delegates the function call to _put. Please implement the non-public + # method _put. It takes a node, a key, and a val as arguments. node is the root of + # the subtree into which the key-value pair is to be inserted. After the insertion, + # the root of this subtree may change. _put should return the new root of this subtree. + # The difference of the input subtree and the returned subtree is that in the returned + # subtree, a new key-value pair has been inserted. If the key was already in the subtree, + # then we update the value of the corresponding node. + + +greektoroman = BSTTable() +print(greektoroman.put('Athena', 'Minerva')) +print(greektoroman.put('Eros', 'Cupid')) +print(greektoroman.put('Aphrodite', 'Venus')) +print(greektoroman.get('Eros')) +print(greektoroman.get('Vitoria')) +print(greektoroman.get('Lala')) + +print (greektoroman) \ No newline at end of file From 34d1152717ea873b0693b91bd3a9783841beb319 Mon Sep 17 00:00:00 2001 From: anavitor97 Date: Sun, 31 Oct 2021 15:49:27 -0400 Subject: [PATCH 3/3] updating HW5 --- homework/HW5/HW5-final/animal.py | 17 ++++++++--------- homework/HW5/HW5-final/binary_search_tree.py | 6 ++++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/homework/HW5/HW5-final/animal.py b/homework/HW5/HW5-final/animal.py index 944999d..5bcb8f7 100644 --- a/homework/HW5/HW5-final/animal.py +++ b/homework/HW5/HW5-final/animal.py @@ -44,14 +44,13 @@ def species(self, into): self._species = into ->>> dog = Animal('Fido', 'dog') ->>> vars(dog) +dog = Animal('Fido', 'dog') +print(vars(dog)) ->>> dog = Animal('Fido', 'dog') ->>> dog.species - ->>> dog.species = 'cat' ->>> dog.species - ->>> dog.species = 'TheThing' +dog = Animal('Fido', 'dog') +print(dog.species) +dog.species = 'cat' +print(dog.species) +dog.species = 'TheThing' +print(dog.species) diff --git a/homework/HW5/HW5-final/binary_search_tree.py b/homework/HW5/HW5-final/binary_search_tree.py index 78026d7..f61bce6 100644 --- a/homework/HW5/HW5-final/binary_search_tree.py +++ b/homework/HW5/HW5-final/binary_search_tree.py @@ -76,7 +76,9 @@ def _size(node): print(greektoroman.put('Eros', 'Cupid')) print(greektoroman.put('Aphrodite', 'Venus')) print(greektoroman.get('Eros')) + + +print(greektoroman) + print(greektoroman.get('Vitoria')) print(greektoroman.get('Lala')) - -print (greektoroman) \ No newline at end of file