I am trying to implement this code for hash table in python and i don't understand why i and k are not used in the while loop.
Can somebody help me please,

import random

data = []
m = 10
t = []
for i in range(10):
    data.append(random.randint(0, 100))

print(data)
n = len(data)
print(n)

def hfunc(data, m):
    return list(map(lambda index : index % m, data))

def htable(table):
    table = [None]*10
    return table

def get_index(keys, m):
    index = hfunc(keys, m)
    return(index)

def get_i(index):
    for i in index:
        return(i)

def get_k(data):
    for k in data:
        return k

print (get_index(data, m))

def linear(keys):
    table = htable(t)
    index = get_index(keys, m)
    j = 0
    i = get_i(index)
    k = get_k(keys)
    while (j == m):
        if table[i] == None:
            table[i] = k
            j = j+1
    
print(linear(data))

Recommended Answers

All 2 Replies

i don't understand

That's because you don't know what the program is doing, so add some print statements, for example:

def get_k(data):
    print "get_k data", data
    for k in data:
        print "testing", k
        return k

Also, both functions are the same so eliminate one of them, and it is not obvious what you are trying to do, so if you add a description to the function we can tell what it is supposed to do and perhaps have more to say.

Again, the simple rule of Python coding:
import
define
main code

Giving your code a common sense structure makes it easier to read and understand for people you are asking for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.