In [1]:
class Node:

    def __init__(self, name, phone, address, nexxt):
        self._name = name
        self._phone = phone
        self._address = address
        self._next = nexxt

    def __str__(self):
        return "("+self._name+","+self._phone+","+self._address+")"
In [ ]:
#from Node import *

MAXSIZE = 100 # number of buckets

class ContactsHT:

    @classmethod
    def myhash(cls,name):
        sum = 0
        for x in name:
            sum = sum + ord(x)
        return sum%MAXSIZE
    
    # hash table for contacts
    def __init__(self):
        self._hashtable = [Node("","","",None)]*MAXSIZE
        #self._hashtable = [Node("","","",None) for i in range(MAXSIZE)]
        self._size = 0

    def find(self,name):
        bnum = ContactsHT.myhash(name)
        p = self._hashtable[bnum]
        found = False
        while p._next != None and not found:
            if p._next._name == name:
                found = True
            else:
                p = p._next
        return (found,p)

    def insert(self,contact):
        (found,p) = self.find(contact[0])
        if found:
            return False
        else:
            bnum = ContactsHT.myhash(contact[0])
            p = Node(contact[0],contact[1],contact[2],self._hashtable[bnum]._next)
            self._hashtable[bnum]._next = p
            self._size = self._size + 1
            return True

    def delete(self,name):
        #??? homework
        
    def update(self,contact):
        #??? homework
        
    def size(self):
        return self._size

    def __str__(self):
        result = "\n"
        for hindex in range(MAXSIZE):
            p = self._hashtable[hindex]
        while p._next != None:
            result = result + str(p._next)+"\n"
            p = p._next
        return result+"\n"
In [3]:
[str(Node("","","",None)) for i in range(3)]
Out[3]:
['(,,)', '(,,)', '(,,)']
In [4]:
[Node("","","",None)]*3
Out[4]:
[<__main__.Node at 0x1071c2970>,
 <__main__.Node at 0x1071c2970>,
 <__main__.Node at 0x1071c2970>]
In [5]:
[9]*12
Out[5]:
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
In [6]:
[9 for i in range(12)]
Out[6]:
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
In [14]:
def myhash(name):
    sum = 0
    for x in name:
        sum = sum + ord(x)
    print(sum)
    return sum%100  # number between 0 and 99
In [15]:
myhash("Alan")
380
Out[15]:
80
In [16]:
myhash("Sunderraman")
1152
Out[16]:
52
In [17]:
myhash("America")
690
Out[17]:
90
In [13]:
ord('S') + ord('u')
Out[13]:
200
In [ ]: