d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some Python Help > Offering Fg
Add Reply New Topic New Poll
Member
Posts: 40,064
Joined: Nov 22 2009
Gold: 0.00
Apr 2 2013 09:05pm
Code
def __init__(self, initialSize=5, maxLoadFactor=.5, maxChainLength=3):
       '''store the parameters as class variables'''
       self.initialSize = initialSize
       self.maxLoadFactor = maxLoadFactor
       self.currLoadFactor=0
       self.maxChainLength = maxChainLength
       self.m=initialSize
       self.hashtable = []
       '''initialize all slots in the hashtable to None'''
       for i in range(self.initialSize):
           self.hashtable.append(None)
           
   def insert(self, item):
       if self.contains(item)==True:
           print("Insert error: duplicate item")
           return False
       else:
           slotNum=item%(self.m)
           if self.hashtable[slotNum]==None:
               self.currLoadFactor=self.currLoadFactor+1
               self.hashtable[slotNum]=LinkedList()
               self.hashtable[slotNum].add(item)
               if self.currLoadFactor>=self.maxLoadFactor:
                   self.hashtable.resize(2(self.m))
           else:
               self.hashtable[slotNum].add(item)
               if len(self.hashtable[slotNum])>=self.maxChainLength:
                   self.hashtable.resize(2(self.m))
           return True
   def contains(self, item):
       isIn=False
       slotNum=item%(self.m)
       if self.hashtable[slotNum]!=None:
           isIn=(self.hashtable[slotNum]).search(item)
       return isIn
   def resize(self, newSize):
       h = []
       for i in range(newSize):
           h.append(None)
       for i in range(self.m):
           if self.hashtable[i] != None:
               linkList=self.hashtable[i]
               current=linkList.head
               for i in range (length(linkList)):
                   h.insert(current.getData())
                   current=current.getNext()
       self.m=newSize
       self.hashtable=h
   def remove(self, item):
       if self.hashtable.contains(item)==False:
           return None
       else:
           slotNum=item%(self.m)
           self.hashtable[slotNum].remove(item)
           if self.hashtable[slotNum].length==0:
               self.hashtable[slotNum]=None
               if self.m>self.initialSize and self.currLoadFactor<=(.5*self.maxLoadFactor):
                   self.hashtable.resize(int(.5*self.m))
           return item
           
   def getNumItems(self):
       numItems=len(self.hashtable.getAllItems())
       return numItems
   
   def getAllItems(self):
       allItems=[]
       currNode=0
       for i in range(len(self.hashtable)):
           if self.hashtable[i]!=None:
               linkList=self.hashtable[i]
               cur=linkList.head
               for i in range(length(linkList)):
                   allItems.append(cur.getData())
                   cur=cur.getNext()
       return allItems
   def size(self):
       return self.m
   def isEmpty(self):
       empty=False
       for i in range(self.m):
           if i==None:
               empty=True
           else:
               return False
       return empty
   def getLinkListStats(self):
       longestLen=0
       numOfZero=0
       totalLen=0
       numofChains=0
       for i in range(len(self.hashtable)):
           if self.hashtable[i]==None:
               numOfZero=numOfZero+1
               numofChains=numofChains+1
           else:
               linkList=self.hashtable[i]
               totalLen=totalLen+length(self.hashtable[i])
               numofChains=numofChains+1
               if length(linkList)>longestLen:
                   longestLen=length(linkList)
       return longestLen,numOfZero,(totalLen/numofChains)
             
   def displayStats(self):
       return self.m, self.hashtable.getNumItems(),self.currLoadFactor,self.hashtable.getLinkListStats()



Is my code and I'm getting this error

Code
Traceback (most recent call last):
 File "E:/Python Stuff/172/TestHashTable.py", line 7, in <module>
   table.insert(0)
 File "E:/Python Stuff/172\HashTable.py", line 29, in insert
   self.hashtable.resize(2(self.m))
AttributeError: 'list' object has no attribute 'resize'
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 2 2013 09:18pm
self.hashtable = []
self.hashtable.resize(2(self.m))

are you sure list has resize(int) method?
Member
Posts: 40,064
Joined: Nov 22 2009
Gold: 0.00
Apr 2 2013 09:24pm
Quote (carteblanche @ Apr 2 2013 11:18pm)
self.hashtable = []
self.hashtable.resize(2(self.m))

are you sure list has resize(int) method?


yes i wrote it.. it was part of my assignment to write it in the hashtable function =\
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 2 2013 09:32pm
Quote (Funion @ Apr 2 2013 11:24pm)
yes i wrote it.. it was part of my assignment to write it in the hashtable function =\


This is the only assignment i see for self.hashtable:
self.hashtable = []

it's been a while since i used python, so i could be wrong. but from what i can tell, self.hashtable is NOT a class you wrote, but rather a list. so when you call self.hashtable.resize(2(self.m)) you're invoking [].resize(2(self.m)). this is not a method you wrote, it must come from the standard python library. does this method exist?

perhaps you meant to do this:

resize(2(self.m))

instead of:

self.hashtable.resize(2(self.m))

?

This post was edited by carteblanche on Apr 2 2013 09:35pm
Member
Posts: 40,064
Joined: Nov 22 2009
Gold: 0.00
Apr 2 2013 09:36pm
Quote (carteblanche @ Apr 2 2013 11:32pm)
This is the only assignment i see for self.hashtable:
self.hashtable = []

it's been a while since i used python, so i could be wrong. but from what i can tell, self.hashtable is NOT a class you wrote, but rather a list. so when you call self.hashtable.resize(2(self.m)) you're invoking [].resize(2(self.m)). this is not a method you wrote, it must come from the standard python library.

perhaps you meant to do this:

resize(2(self.m))

instead of:

self.hashtable.resize(2(self.m))

?


without having self.hashtable.resize it wouldn't know what to resize... resize(2(self.m)) python thinks this is a global name

When I said I wrote it it was part of my assignment I was told to right a resize function for this for my programming class... I cannot figure out what i did wrong.. but if you know a working resize on a hash table function I'm all ears... :)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 2 2013 09:47pm
Quote
without having self.hashtable.resize it wouldn't know what to resize... resize(2(self.m)) python thinks this is a global name


well, i mean, i think your intention is to use the method you wrote, right? it looks like the code you posted is your HashTable class and it's backed by a list. i'm not referring to some global function but rather the one you wrote a few lines farther down. maybe try self.resize(2(self.m)) instead of resize(2(self.m))? i don't remember if self is required in python
Member
Posts: 40,064
Joined: Nov 22 2009
Gold: 0.00
Apr 2 2013 09:59pm
Quote (carteblanche @ Apr 2 2013 11:47pm)
well, i mean, i think your intention is to use the method you wrote, right? it looks like the code you posted is your HashTable class and it's backed by a list. i'm not referring to some global function but rather the one you wrote a few lines farther down. maybe try self.resize(2(self.m)) instead of resize(2(self.m))? i don't remember if self is required in python


nada =\
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Apr 4 2013 06:15am
I can probably take a look at this and actually run it after work tonight.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll