Skip to content
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
65 changes: 40 additions & 25 deletions Arrays.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import ctypes
import ctypes # provides low-level arrays

class Arrays:
def __init__(self):
self._n = 0 #counts current number of elements
self._capacity = 1 #Capacity = total number of blocks reserved for elements
self._A = _create_array(self._capacity)
class DynamicArray:
"""A dynamic array class akin to a simplified Python list."""

def __init__ (self):
"""Create an empty array."""
self._n = 0 # count actual elements
self._capacity = 1 # default array capacity
self._A = self._make_array(self._capacity) # low-level array

def __len__ (self):
"""Return number of elements stored in the array."""
return self._n

def __getitem__ (self, k):
"""Return element at index k."""
if not 0 <= k < self._n:
raise IndexError('invalid index' )
return self._A[k]


def append(self, obj):
"""Add object to end of the array."""
if self._n == self._capacity: # not enough room
self._resize(2*self._capacity)
self._A[self._n] = obj
self._n += 1

def insert(self, obj, index):
"""Add object to the specified index of the array."""
if self._n == self._capacity: # not enough room
self._resize(2 * self._capacity) # so double capacity
if self._n == self._capacity : # not enough room
self._resize(2*self._capacity) # so double capacity
if index > self._n:
raise IndexError('invalid index')
for i in range(self._n, index, -1):
self._A[i] = self._A[i - 1]
self._A[i] = self._A[i-1]
self._A[index] = obj
self._n += 1

Expand All @@ -28,30 +42,31 @@ def delete(self, index):
if (index >= self._n) or (index < 0):
raise IndexError('invalid index')
for i in range(index, self._n - 1):
self._A[i] = self._A[i + 1]
self._A[i] = self._A[i+1]
self._n -= 1

def _resize(self, c): # nonpublic utitity
"""Resize internal array to capacity c."""
B = self._make_array(c) # new (bigger) array
for k in range(self._n): # for each existing value
B[k] = self._A[k]
self._A = B # use the bigger array
self._capacity = c

def _make_array(self, c): # nonpublic utitity
"""Return new array with capacity c."""
return (c*ctypes.py_object)()

def print_array_details(self):
"Prints array, capacity and length"
print("------------------------------------------")
print("Length of array is " + str(self._n))
print("Length of array is "+ str(self._n))
print("Capacity of array is " + str(self._capacity))
print("Array is ")
print("Array is " )
for i in range(self._n):
print(str(self._A[i]) + " ")

def _resize(self, c):
"""Resize internal array to capacity c."""
B = self._make_array(c) # new (bigger) array
for k in range(self._n): # for each existing value
B[k] = self._A[k]
self._A = B # use the bigger array
self._capacity = c

def _create_array(self,c):
return (ctypes.py_object * c)()

A = Arrays()
A = DynamicArray()
A.print_array_details()
A.insert(2,0)
A.print_array_details()
Expand Down
99 changes: 99 additions & 0 deletions ChainHashMap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
class Item:
def __init__(self, k, v):
self._key = k
self._value = v

class ChainHashMap:
def __init__(self, capacity=7):

#Creates a list of size CAPACITY and initialize it with None
self._table = [None]*capacity

#Keeping the count of total entries in table
self._n = 0

def _hash_function(self, key):
return key%len(self._table)

def __len__(self):
return self._n

def __getitem__(self, key):
hashed_key = self._hash_function(key)
if self._table[hashed_key] is None:
raise KeyError("key error")
else:
for item in self._table[hashed_key]:
if item._key == key:
return item._value
raise KeyError("key error")

def __setitem__(self, key, value):
hashed_key = self._hash_function(key)
if self._table[hashed_key] is None:
self._table[hashed_key] = []
for item in self._table[hashed_key]:
if item._key == key:
item._value = value
return

self._table[hashed_key].append(Item(key,value))
self._n += 1

if self._n > len(self._table)//2:
self._resize(2*len(self._table)-1)

def _resize(self, capacity):
old = []
for bucket in self._table:
if bucket is not None:
for items in bucket:
old.append(items)
self._table = capacity*[None]
self._n = 0
for items in old:
self[items._key] = items._value

def print(self):
for i in range(len(self._table)):
if self._table[i] is not None:
print("..............................")
print("At " + str(i))
for item in self._table[i]:
print(str(item._key) + " " + str(item._value))






testHashMap = ChainHashMap()

print("size of table is "+ str(len(testHashMap._table)))
#Testing set function
testHashMap[10] = "John"
testHashMap[21] = "Ram"
testHashMap[24] = "Aman"

#Testing get function
print("value for key 24 is: " + testHashMap[24])
#print("value for key 25 is: " + testHashMap[25])


#Testing resize function
testHashMap[1] = "Jyoti"
#testHashMap[2] = "Kate"
#testHashMap[3] = "Phoebe"
#testHashMap[8] = "Jyoti"
#testHashMap[56] = "Kate"
#testHashMap[34] = "Phoebe"
#testHashMap[18] = "Jyoti"
#testHashMap[23] = "Kate"
#testHashMap[39] = "Phoebe"
#testHashMap[13] = "Jyoti"
#testHashMap[29] = "Kate"
#testHashMap[37] = "Phoebe"

testHashMap.print()
#Check size of table
print("size of table is "+ str(len(testHashMap._table)))
95 changes: 95 additions & 0 deletions LinearProbeHashMap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class Item:
def __init__(self, k, v):
self._key = k
self._value = v

class LinearProbeHashMap:
def __init__(self, capacity=11):

#Creates a list of size CAPACITY and initialize it with None
self._table = [None]*capacity

#Keeping the count of total entries in table
self._n = 0

def _hash_function(self, key):
return key%len(self._table)

def __len__(self):
return self._n

def _find_bucket(self, key, hashed_key):
while True:
if self._table[hashed_key] is not None:
if self._table[hashed_key]._key == key:
return hashed_key, True
hashed_key = (hashed_key + 1)%len(self._table)
else:
return hashed_key, False

def __getitem__(self, key):
hashed_key = self._hash_function(key)
hashed_key, found = self._find_bucket(key, hashed_key)
if found is False:
raise KeyError("key error")
return self._table[hashed_key]._value

def __setitem__(self, key, value):
hashed_key = self._hash_function(key)
hashed_key, found = self._find_bucket(key, hashed_key)
self._table[hashed_key] = Item(key, value)
if found is False:
self._n += 1

if self._n > len(self._table)//2:
self._resize(2*len(self._table)-1)

def _resize(self, capacity):
old = []
for bucket in self._table:
if bucket is not None:
old.append(bucket)
self._table = capacity*[None]
self._n = 0
for items in old:
self[items._key] = items._value

def print(self):
for i in range(len(self._table)):
if self._table[i] is not None:
print("..............................")
print("At " + str(i))
print(str(self._table[i]._key) + " " + str(self._table[i]._value))

testHashMap = LinearProbeHashMap()

print("size of table is "+ str(len(testHashMap._table)))
#Testing set function
testHashMap[10] = "John"
testHashMap[21] = "Ram"
testHashMap[24] = "Aman"




#Testing resize function
testHashMap[22] = "Jyoti"
testHashMap[23] = "Kate"
testHashMap[3] = "Phoebe"
testHashMap[4] = "Jyotishi"
#testHashMap[56] = "Kate"
#testHashMap[34] = "Phoebe"
#testHashMap[18] = "Jyoti"
#testHashMap[23] = "Kate"
#testHashMap[39] = "Phoebe"
#testHashMap[13] = "Jyoti"
#testHashMap[29] = "Kate"
#testHashMap[37] = "Phoebe"

#Testing get function
print("value for key 21 is: " + testHashMap[21])
#print("value for key 25 is: " + testHashMap[25])

testHashMap.print()
#Check size of table
print("size of table is "+ str(len(testHashMap._table)))
7 changes: 4 additions & 3 deletions LinkedListInsertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ def append(self, data):
last_node = self.head
if self.head is None:
self.head = temp
while last_node.next :
last_node = last_node.next
last_node.next = temp
if last_node is not None: #so that append function works even if, only l1=LinkedList() is created and other nodes are not traversed
while last_node.next :
last_node = last_node.next
last_node.next = temp

class Node:
def __init__(self, data):
Expand Down