Hashtables Python

时间:2016-02-09 05:10:22

标签: python python-3.x hashtable

我目前正在完成一项任务,无法理解PrefixTable类中的商店功能所需的内容。如果有人可以提供一些建议,从哪里开始或给我一个正确的方向,这将非常感激。

class PrefixItem(object):

    '''Stores a prefix:possibles pair.

    >>> p = PrefixItem('th', SortedFrequencyList())
    >>> p.possibles.add('e', 40)
    >>> p.possibles.add('o', 10)
    >>> p
    {th: ({e: 40}, {o: 10})}'''

    def __init__(self, prefix, possibles):

        '''Initialises a new PrefixItem with the given letter `prefix` and
        SortedFrequencyList of `possibles`.'''

        self.prefix = prefix
        self.possibles = possibles

    def __hash__(self):
        return hash(self.prefix)

    def __repr__(self):
        return ('{' + self.prefix + ': ' + str(self.possibles) + '}')


class PrefixTable(object):

    '''A simple hash table for storing prefix:possible combinations using 
    PrefixItems internally.'''

    def __init__(self, slots):

        '''Initialises the PrefixTable with a number of `slots`. The table cannot
        store more items than the number of slots specified here.'''

        self.data = [None] * slots

    def store(self, prefix, possibles):

        '''Stores the given letter `prefix` and list of `possibles` (a 
        SortedFrequencyList) in the hash table using a PrefixItem. If the
        item is successfully stored in the table, this method returns 
        True, otherwise (for example, if there is no more room left in the 
        table) it returns False.

        >>> p = PrefixTable(1)
        >>> p.store('th', SortedFrequencyList())
        True
        >>> p
        [{th: ()}]
        >>> p.store('ca', SortedFrequencyList())
        False'''

0 个答案:

没有答案