联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2018-08-09 01:50

""" Use for the hash table question """

from stats import StatCounter



class CounterLinkNode:

   def __init__(self, word, count=1):

       self.word = MyString(word)

       self.count = count

       self.next_node = None


   def __repr__(self):

       return str(self.word) + ": " + str(self.count)



class CounterLinkedList:


   def __init__(self, head=None):

       self.head = head

       self.__n_accesses__ = 0


   def __repr__(self):

       node = self.head

       string = str(node)

       while node.next_node:

           string += " -> " + str(node.next_node)

           node = node.next_node

       string = "[" + string + "]"

       return string


   def __contains__(self, item):

       raise TypeError(

           "You can't use the 'in' keyword with a CounterLinkedList")


   def get_accesses(self):

       return self.__n_accesses__



class MyString:

   '''A wrapped string that counts comparisons of itself

  against strings and delegates all other operations to the

  string itself.'''


   def __init__(self, i):

       self.i = i


   def __eq__(self, j):

       if type(j) != MyString:

           StatCounter.increment()

       return self.i == j


   def __le__(self, j):

       if type(j) != MyString:

           StatCounter.increment()

       return self.i <= j


   def __ne__(self, j):

       if type(j) != MyString:

           StatCounter.increment()

       return self.i != j


   def __lt__(self, j):

       if type(j) != MyString:

           StatCounter.increment()

       return self.i < j


   def __gt__(self, j):

       if type(j) != MyString:

           StatCounter.increment()

       return self.i > j


   def __ge__(self, j):

       if type(j) != MyString:

           StatCounter.increment()

       return self.i >= j


   def __repr__(self):

       return repr(self.i)


   def __getattr__(self, attr):

       '''All other behaviours use self.i'''

       return self.i.__getattr__(attr)



def _c_mul(a, b):

   """Substitute for c multiply function"""

   return ((int(a) * int(b)) & 0xFFFFFFFF)



def nice_hash(input_string):

   """Takes a string name and returns a hash for the string. This hash value

   will be os independent, unlike the default Python hash function."""

   if input_string is None:

       return 0  # empty

   value = ord(input_string[0]) << 7

   for char in input_string:

       value = _c_mul(1000003, value) ^ ord(char)

   value = value ^ len(input_string)

   if value == -1:

       value = -2

   return value



def hash_word(item, slots):

   return nice_hash(item) % slots


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp