Skip to content

Commit

Permalink
Merge pull request #6 from anavitor97/HW5-dev
Browse files Browse the repository at this point in the history
Hw5 submission
  • Loading branch information
vitoriarlima authored Nov 24, 2021
2 parents 8e6e26a + 34d1152 commit 3517b46
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 0 deletions.
56 changes: 56 additions & 0 deletions homework/HW5/HW5-final/animal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/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')
print(vars(dog))

dog = Animal('Fido', 'dog')
print(dog.species)
dog.species = 'cat'
print(dog.species)

dog.species = 'TheThing'
print(dog.species)
84 changes: 84 additions & 0 deletions homework/HW5/HW5-final/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/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)

print(greektoroman.get('Vitoria'))
print(greektoroman.get('Lala'))
57 changes: 57 additions & 0 deletions homework/HW5/animal.py
Original file line number Diff line number Diff line change
@@ -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'

82 changes: 82 additions & 0 deletions homework/HW5/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3517b46

Please sign in to comment.