Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def hash(st):
- st_len = len(st)
- # if there is no text we can return -1 since that's an invalid hash
- if st_len == 0:
- return -1
- # dif has is used to offset the has so input like 'a' would go into bucket 0
- dif_hash = ord('a')
- # if the length is only one we don't need to get the summation of characters 1 to st_len
- if st_len == 1:
- return ord(st) - dif_hash
- else:
- # calculating the summation of the strings ascii values
- # word[0] + Σ i=1, i < st_len - 'a' ascii value
- summation = 0
- for i in range(1, st_len):
- summation += ord(st[i])
- return ord(st[0]) + summation - dif_hash
- while True:
- st = input(">>> ")
- hash_code = (hash(st))
- print(hash_code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement