Skip to content

Monday MVP, tues notes. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__pycache__
__pycache__
/Pipfile
/Pipfile.lock
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/Hash-Tables.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions dynamic_array/dynamic_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class DynamicArray:
def __init__(self, capacity=8):
self.count = 0
self.capacity = capacity
self.storage = [None] * self.capacity

def insert(self, index, value):
if self.count == self.capacity:
# increase size
print("ERROR:, Array is full")
return

if index >= self.count:
print('Error: Index out of bounds')
return

for i in range(self.count, index -1):
self.storage[i] = self.storage[i-1]

self.storage[index] = value
self.count += 1

def append(self, value):
if self.count == self.capacity:
# increase size
print("ERROR:, Array is full")
return

# self.count += 1
# # account for zero index... - 1
# self.storage[self.count - 1] = value
#
# same as above
self.storage[self.count] = value
self.count += 1

# Double the size
def double_size(self):
self.capacity *= 2
new_storage = [None] * self.capacity

for i in range(self.count):
new_storage[i] = self.storage[i]

# change pointer
self.storage = new_storage
14 changes: 14 additions & 0 deletions hashes/hashes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import hashlib

# b string - bytes string - strips away -- .encode() works too
key = b"str"
my_string = "normal string"

for i in range (10):
hashed = hashlib.sha256(key).hexdigest()
print(hashed)


# for i in range(10):
# hashed = hash(key)
# print(hashed % 8)??????
14 changes: 14 additions & 0 deletions hashes1/hashesClass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import hashlib

# b string - bytes string - strips away -- .encode() works too
key = b"str"
my_string = "normal string".encode()

# for i in range (10):
# hashed = hashlib.sha256(key).hexdigest()
# print(hashed)


for i in range(10):
hashed = hash(key)
print(hashed % 8)
42 changes: 42 additions & 0 deletions src/collisions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@


import random


def how_many_before_collision(buckets, loops=1):
for i in range(loops):
tries = 0
tried = set()
while True:
random_key = str(random.random())
hash_index = hash(random_key) % buckets
if hash_index not in tried:
tried.add(hash_index)
tries += 1

else:
break

print(f"{buckets} buckets, {tries} hashes before collision. ({tries / buckets * 100:.1f})")

how_many_before_collision(32, 10)


def longest_linked_list_chain(keys, buckets, loops=10):
for i in range(loops):
key_counts = {}

for i in range(buckets):
key_counts[i] = 0

for i in range(keys):
random_key = str(random.random())
hash_index = hash(random_key) % buckets
key_counts[hash_index] += 1

largest_number = 0
for key in key_counts:
if key_counts[key] > largest_number:
largest_number = key

print(f"Longest linked list chain for {keys} keys in {buckets}(Load Factor: {keys/ buckets:2f}: {largest_number}")
61 changes: 55 additions & 6 deletions src/hashtable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# '''
# Linked List hash table key/value pair
# '''

from dynamic_array import dynamic_array

class LinkedPair:
def __init__(self, key, value):
self.key = key
Expand All @@ -16,15 +19,18 @@ def __init__(self, capacity):
self.capacity = capacity # Number of buckets in the hash table
self.storage = [None] * capacity


# _ means private function, should not be used outside of class, but you can
def _hash(self, key):
'''
Hash an arbitrary key and return an integer.

You may replace the Python hash with DJB2 as a stretch goal.
'''
return hash(key)
x = hash(key)
print('x', x)

return x
# _hash(3,'8')

def _hash_djb2(self, key):
'''
Expand All @@ -43,6 +49,18 @@ def _hash_mod(self, key):
return self._hash(key) % self.capacity


# if self.capacity is not FULL:
# if self.capacity:
# current_next = self.next
# self.next = LinkedPair(value, self, current_next)
# else:
# self.key = key
# print('k',self.key)
# self.value = value
# print('v',self.value)
#
# print(insert(3, 3, 5))

def insert(self, key, value):
'''
Store the value with the given key.
Expand All @@ -51,9 +69,16 @@ def insert(self, key, value):

Fill this in.
'''
pass

index = self._hash_mod(key)

if self.storage[index] is not None:
print(f"WARNING: Collusion has occured at {index}")

else:
self.storage[index] = (key, value)

return

def remove(self, key):
'''
Expand All @@ -63,8 +88,18 @@ def remove(self, key):

Fill this in.
'''
pass
index = self._hash_mod(key)

if self.storage[index] is not None:
if self.storage[index][0] == key:
self.storage[index] = None
else:
print(f"WARNING: Collusion has occured at {index}")

else:
print(f"Warning key ({key}) not found")

return

def retrieve(self, key):
'''
Expand All @@ -74,8 +109,18 @@ def retrieve(self, key):

Fill this in.
'''
pass
index = self._hash_mod(key)

if self.storage[index] is not None:
if self.storage[index][0] == key:
return self.storage[index][1]
else:
print(f"WARNING: Collusion has occured at {index}")

else:
return None

return

def resize(self):
'''
Expand All @@ -84,8 +129,12 @@ def resize(self):

Fill this in.
'''
pass
old_storage = self.storage
self.capacity *= 2
self.storage = [None] * self.capacity

for item in old_storage:
self.insert(item[0], item[1])


if __name__ == "__main__":
Expand Down
Loading