So I am trying to make a hash function that put an element k into a 2D array based on it's mod. For this example the size of the outer array is 4. So to start out the hash table I want it to be a 2D array that has the size of the outer array to be 4. Then there will be 4 empty arrays inside the outer array. Like so...
n = 4
A = [[]]* n
Which is [ [], [], [], [] ] So when I use the hash function like this hash(A, 2) it should output this [ [], [], [2], [] ] based on this code below...
def hash(A, k):
idx = k % 4
for i in range(len(A)):
for j in range(len(A[i])):
if i == idx:
print(A[i][j])
A[i][j] = A[i][j].append(k)
So the problem is that it outputs this [ [], [], [], [] ] and not this [ [], [], [2], [] ].
I have tried...
def hash(A, k):
idx = k % 4
A[idx].append(k)
but this only outputs [[2], [2], [2], [2]] which is not what I want.
How do I make my hash function give me this output [ [], [], [2], [] ]?
(P.S. I know it's a lot better to simply just make an array of linked lists. I am doing this for a better understanding of hash table if they were implemented with arrays and to get a better understanding of 2D arrays.)
len(A[i])is always 0, so nothing ends up happening. If you change toA = [[] for i in range(n)]then the second function should work.listobjects are not arrays.