Skip to content

Instantly share code, notes, and snippets.

@gunnarig
Created March 9, 2019 19:05
Show Gist options
  • Save gunnarig/d2736ff5b54b46c6d026a1dd47b427a7 to your computer and use it in GitHub Desktop.
Save gunnarig/d2736ff5b54b46c6d026a1dd47b427a7 to your computer and use it in GitHub Desktop.
#class NotFoundException(Exception):
#pass
class Error(Exception):
pass
class Bucket:
class Node:
def __init__(self,key,data,next = None):
self.key = key
self.data = data
self.next = next
def __init__(self):
self.head = None
self.size = 0
def insert(self,key,data):
if self.__find(key,self.head) == None:
self.head = self.Node(key, data, self.head)
self.size +=1
def update(self,key,data):
value = self.__find(key,self.head)
comparison = value
if value == None:
return
else:
comparison.data = data
return
return self.update(key, self.head.next)
def __find(self,key,head):
if head == None:
return
if head.key == key:
return head
return self.__find(key, head.next)
def contains(self,key):
value = self.__find(key,self.head)
comparison = self.head.key
if comparison == key:
return True
elif value == None:
return False
else:
return False
def remove(self, key):
#print("data is: " + str(key))
#print("self.head.data is: " + str(self.head.data))
if self.head.key == key:
#print("self.head.data == val")
self.head = self.head.next
self.size -= 1
else:
self.remove_rec(key, self.head)
def remove_rec(self, key, Node):
#print("remove_rec key is: " + str(Node.key))
if Node != None:
if Node.next.key == key:
Node.next = Node.next.next
self.size -= 1
else:
self.remove_rec(key, Node.next)
def __setitem__(self, key, data):
value = self.__find(key,self.head)
if value == None:
self.insert(key,data)
else:
return self.update(key,data)
def __getitem__(self, key):
value = self.__find(key,self.head)
if value == None:
raise Error
else:
return value.data
def __len__(self):
return self.size
def main ():
goatfather = Bucket()
goatlength = len(goatfather)
goatfather.insert(5,"he man")
goatlength = len(goatfather)
#goatfather.insert(3,"she man")
#goatfather.insert(7,"skeletor")
#goatfather.insert(6,"ironman")
goatfather.insert(4,"olm man")
goatlength = len(goatfather)
goatfather.insert(14,"lizard")
goatlength = len(goatfather)
goatfather.insert(13,"goatfather himself")
print(goatfather.contains(18))
goatlength = len(goatfather)
goatfather.insert(18,"ronald mcdonald")
goatlength = len(goatfather)
goatfather.update(14,"Goatmaster")
goatlength = len(goatfather)
print(goatfather.contains(18))
goatfather.remove(18)
print(goatfather.contains(18))
print(len(goatfather))
goatfather[22] = "love"
goatfather[22] = "elderly man"
goatfather[20] = "love"
goatfather.remove(20)
my_data = goatfather[14]
my_data = goatfather[None]
print(my_data)
print(goatlength)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment