#๐Ÿ”’ Find strings in a hashmap

16 messages ยท Page 1 of 1 (latest)

molten sage
#

I'm having trouble understanding what I'm supposed to do in this question

"""
You are given the size N of a hash table. 
Your task is to find two strings that map to the same location in the hash table based on Python's hash function. 
In other words, you need to find strings x and y such that hash(x) % N == hash(y) % N. 
You can assume that N is at most 100. 
Your solution should work efficiently in these cases. 
Implement in the file samehash.py a function find that returns the desired strings as a pair.

Note that the behavior of the hash function changes each time the Python interpreter is started. Therefore, the function find should give a different solution each time it is run
"""
def find(N):    
    return 0
if __name__ == "__main__":
    print(find(42)) # e.g. ('abc', 'aybabtu')

I though maybe it was the ascii values or something but that wasn't the case. How are abc and aybabtu related in any way??
According to the question they have the same location in this arbitrary hash table, but why?? To me it looks like these are randomly generated strings and there's no way to verify if that output is correct or not, because the condition isn't even satisfied

strange steppeBOT
#

@molten sage

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

molten sage
#

all I have as an input is a hashtable size, no actual hash table

#

there is literally a 1/42 chance of getting equal output when running this

print(hash('abc')%42,hash('aybabtu')%42)
#

!e
print(hash('abc')%42,hash('aybabtu')%42)

strange steppeBOT
molten sage
#

!e
print(hash('abc')%42,hash('aybabtu')%42)

strange steppeBOT
molten sage
#

I am probably looking at this exercise the wrong way, but I'm just not seeing it

sly tusk
#

maybe find out how python's hash work and try to generate 2 strings that can collide

#

or you can just generate like 101 words (given N is atmost 100) by pigeon hole principle atleast 2 words will collide you can find them by bruteforce

#

!e

from collections import defaultdict
def find(N):
  hashes = defaultdict(set)
  for i in map(str, range(101)):
    hashes[hash(i)%N].add(i)
    if len(hashes[hash(i)%N]) > 1:
      print(f"Collision Found: {hashes[hash(i)%N]} with hash {hash(i)%N}")
      break
find(50)
strange steppeBOT
molten sage
#

!solved

strange steppeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.