Posted By

iziaman on 07/16/16


Tagged

data python source hashing


Versions (?)

double hashing


 / Published in: Python
 

create the result of the double hashing

  1. class DoubleHashing(object):
  2. def __init__(self,data=list()):
  3. self.data=data
  4. self.fx=int(input("module fx:\t"))
  5. self.gx=int(input("module gx:\t"))
  6. self.fx_data=list()
  7. self.gx_data=list()
  8. self.hashTabel=list()
  9. self.steps=list()
  10. self.setModes()
  11. for i in range(self.fx):
  12. self.hashTabel.append(None)
  13. self.steps.append(None)
  14. self.createHashTabel()
  15. self.printToFile()
  16.  
  17. def setModes(self):
  18. for i in self.data:
  19. self.fx_data.append(i % self.fx)
  20. self.gx_data.append(self.gx-i % self.gx)
  21.  
  22. def createHashTabel(self):
  23. i=0
  24. while i < len(self.data):
  25. j=0
  26. while j<100:
  27. temp=(self.fx_data[i]+self.gx_data[i]*j) % self.fx
  28. if(self.hashTabel[temp] == None):
  29. self.hashTabel[temp]=self.data[i]
  30. self.steps[temp]=j
  31. break
  32. j+=1
  33. i+=1
  34.  
  35. def printHashtabel(self):
  36. res=""
  37. res+="data\t Fx\t Gx\n"
  38. for i in range(0,len(self.data)):
  39. res+= "{}\t {} \t{}\n".format(self.data[i],self.fx_data[i],self.gx_data[i])
  40. res+="-"*10
  41. res+='\n'
  42. for i in range(0,len(self.hashTabel)):
  43. res+= "{}.\t {}\t{}\n".format(i, self.hashTabel[i],self.steps[i])
  44. # print(i, self.hashTabel[i],sep='\t')
  45. return res
  46.  
  47. def printToFile(self,fName="log1"):
  48. fName=fName+"_"+str(self.fx)+'_'+str(self.gx)
  49. x= open(fName, "w")
  50. x.write(self.printHashtabel())
  51. def main():
  52. #enter here the data you want to hash
  53. data=[70,16,23,3,24,38,60,115,46,98,99]
  54. DoubleHashing(data)
  55.  
  56.  
  57. if __name__=="__main__":
  58. main()

Report this snippet  

You need to login to post a comment.