CS 345 Homework 12

Due Monday May 1 at 9pm. Upload to Desire 2 Learn dropbox for homework 12.

Hash Set Mapping Solution

Description

This homework exercise will allow you to explore the implementation of a simple hash table set using buckets. To do this you will start with hw_12_starter.py and proceed to implement the functions provided.

Start here: hw_12_starter.py

Tasks:

A. Implement all methods except for the constructor in class HashSetTbl.

B. Test your 10-bucket table by adding a sufficient number words to your set to force the use of your buckets. For example you might take the following paragraph, parse it into words, then insert each word into your set. Note that your set class should not add duplicates.

As she said this she looked down at her hands and was surprised to see that she had put on one of the Rabbis little white kid gloves while she was talking How can I have done that she thought I must be growing small again She got up and went to the table to measure herself by it and found that as nearly as she could guess she was now about two feet high and was going on shrinking rapidly she soon found out that the cause of this was the fan she was holding and she dropped it hastily just in time to avoid shrinking away altogether

Turn In:

Rename the source code file containing your version of the program according to syllabus.

Solution

"""
Solution for hw 12 cs 345 sp 2017
hw_12_starter.py
"""

class HashSetTbl:
    
    def __init__(self):
        """ constructor creates a 10 empty buckets 
        each bucket contains zero or more unique values.
        def. This class maintains a set property (all unique values)
        """
        self.bucket_list = [ [] for i in range(10)]

    def __getitem__(self, v):
        """override retrieval to support syntax: tblobject['red'] 
        should return the value v in table
        if v not found in table, raise KeyError
        Must use the hash value of v THEN sequential search of bucket
        at index[h(v)]
        """
        bucket = self.bucket_list[self.hash_func(v)]
        for i in bucket:
            if i == v:
                return i
        raise KeyError

    def insert(self, v):
        """assignment to support syntax: tblobject.add('blue')
        should add the value v to proper bucket in table IF it does 
        not already exist.
        """
        bucket = self.bucket_list[self.hash_func(v)]
        for i in bucket:
            if i == v:
                return
        bucket.append(v)
        return

    def __iter__(self):
        """ implement generator to allow looping over entire contents
        of the set.
        """
        for bucket in self.bucket_list:
            for i in bucket:
                yield i

    def __len__(self):
        """ returns the total number of assigned values in table."""
        num_items = 0
        for bucket in self.bucket_list:
            num_items += len(bucket)
        return num_items

    def bucket_dump(self):
        """ prints a snapshot of the bucket list to the screen. Example:
            [0]: 'a', 'tree', 'mouse'
            [1]: 'red'
            ...
            [9]: 'top', 'bottom'
        """
        for position, bucket in enumerate(self.bucket_list):
            print '[', position, '] ITEMS: (', len(bucket), ') ==>',
            for i in bucket:
                print i,
            print '\n'

    def hash_func(self, v):
        """
        a. computes a hash code for v using python hash() function
        b. compresses hash code relative to size of bucket list.
        returns the compression value
        """
        return hash(v) % len(self.bucket_list)


if __name__ == '__main__':

    text = 'As she said this she looked down at her hands and was surprised to see that she had put on one of the Rabbis little white kid gloves while she was talking How can I have done that she thought I must be growing small again She got up and went to the table to measure herself by it and found that as nearly as she could guess she was now about two feet high and was going on shrinking rapidly she soon found out that the cause of this was the fan she was holding and she dropped it hastily just in time to avoid shrinking away altogether'
    
    h = HashSetTbl()
    
    data = text.split()
    for i in data: h.insert(i)
    
    print len(h)
    
    h.bucket_dump()

    for i in h:
        print i,
    try:
        print h['rabbis']
    except KeyError:
        print 'rabbis not found'

© 2017 - . All rights reserved
Built using Jekyll