Python实现碰撞链

时间:2013-10-29 22:26:40

标签: python function hash

我正在尝试运行以下功能:

class HashTable( ):
    """
       The HashTable data structure contains a collection of values
       where each value is located by a hashable key.
       No two values may have the same key, but more than one
       key may have the same value.
    """

    __slots__ = ( "table", "size" )


def mkHashTable(capacity=100):
    """
    creates a hash table with capacity 100
    """
    aHashTable = HashTable() 
    aHashTable.table = [None for _ in range(capacity)]
    aHashTable.size = 0
    return aHashTable

def HashTableToStr(hashtable):
    """
    converts to a string
    """
    result = ""
    for i in range( len( hashtable.table ) ):
        if i != None:
            result += str( i ) + ": "
            result += EntryToStr( hashtable.table[i] ) + "\n"
    return result


class _Entry( ):
    """
       A class used to hold key/value pairs.
    """

    __slots__ = ( "key", "value" )


def EntryToStr(entry):
  """
       return the string representation of the entry.
  """
  return "(" + str( entry.key ) + ", " + str( entry.value ) + ")" 

def mkEntry(key, value):
  aEntry = _Entry();
  aEntry.key = key;
  aEntry.value = value;
  return aEntry;


def hash_function( val, n ):
    """
       Compute a hash of the val string that is in [0 ... n).
    """
    #hashcode = hash( val ) % n
    #return hashcode
    hashcode = 1
    for i in val:
        hashcode *= ord(i)

    # hashcode = 0
    # hashcode = len(val) % n
    return hashcode % n

def keys( hTable ):
    """
       Return a list of keys in the given hashTable.
    """
    result = []
    for entry in hTable.table:
        if entry != None:
            result.append( entry.key )
    return result

def contains( hTable, key ):
    """
       Return True iff hTable has an entry with the given key.
    """
    index = hash_function( key, len( hTable.table ) )
    startIndex = index # We must make sure we don't go in circles.
    while hTable.table[ index ] != None and hTable.table[ index ].key != key:
        index = ( index + 1 ) % len( hTable.table )
        if index == startIndex:
            return False
    if hTable.table[index]!=None:
        return hTable.table[ index ].value[index] != None
    else:
        return False
def put( hTable, key, value ):
    """
       Using the given hash table, set the given key to the
       given value. If the key already exists, the given value
       will replace the previous one already in the table.
       If the table is full, an Exception is raised.
    """
    index = hash_function( key, len( hTable.table ) )
    startIndex = index # We must make sure we don't go in circles.
    while hTable.table[ index ] != None and hTable.table[ index ].key != key:
        index = ( index + 1 ) % len( hTable.table )
        if index == startIndex:
            raise Exception( "Hash table is full." )
    if hTable.table[ index ] == None:
        entry=mkEntry(key,value)


        L = [entry]
        hTable.table[ index ] = mkEntry( key, L )

        hTable.size += 1
    else:
        for i in range(len(hTable.table[ index ].value)):
            if(hTable.table[ index ].value[i].key==key):
                hTable.table[ index ].value[i].value=value
            else:
                entry=mkEntry(key,value)
                hTable.table[ index ].value.append(entry)
    return True

def get( hTable, key ):
    """
       Return the value associated with the given key in
       the given hash table.
       Precondition: contains(hTable, key)
    """
    index = hash_function( key, len( hTable.table ) )
    startIndex = index # We must make sure we don't go in circles.
    while hTable.table[ index ] != None and hTable.table[ index ].key != key:
        index = ( index + 1 ) % len( hTable.table )
        if index == startIndex:
            raise Exception( "Hash table does not contain key." )
    if hTable.table[ index ] == None:
        raise Exception( "This list does not contain key." )
    else:
        for i in hTable.table[ index ].value:
            if i.key==key:
                return i.value
            else:
                raise Exception("There is no such key in the table.")

def imbalanced(hTable):
    """
    takes the average of what is found in the hash table
    """
    _sum = 0
    nume = 0
    for i in range(len(hTable)):
        if hTable.table[i] == None:
            pass
        else:
            _sum += len(hTable.table[i].value)
            nume +=1
    return (_sum / nume) - 1

def word_count(filename):
    """Report on the frequency of different words in the
       file named by the argument.
    """
    count = 0
    inFile = open(filename)
    hTable = mkHashTable()
    for line in inFile:
        for word in line.split():
            count += 1
            if not contains(hTable,word):
                put(hTable,word,1)
            else:
                newval = get(hTable,word)
                put(hTable,word,1+newval)    
    inFile.close()

    print("Total words:", count)
    print("Unique words:", hTable.size)
    big = 0
    for i in hTable:
        for j in range(hTable[i].value):
            if hTable[i].value[j] > get(hTable,big):
                big = j
    print("Most used word:  ", get(hTable, big), "  occurred", j, "times.")

def main():
    filename = input("Enter filename: ")
    word_count(filename)

main()

我要做的是实现链接以解决冲突并使用哈希表计算任何文本文件中的单词数。每次我改变一些东西,我似乎导致另一个错误,目前错误表明索引超出范围。它还相信其他情况下表中没有要求的密钥。我无法弄清楚我做错了什么。

0 个答案:

没有答案