We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Basic Data Types
- Tuples
- Discussions
Tuples
Tuples
Sort by
recency
|
1822 Discussions
|
Please Login in order to post a comment
The issue in the problem arises because Python 3.3 and above introduced hash randomization for security, which means the hash() of the same tuple can vary between runs and machines, whereas in Python 2 the hash() output for the same tuple is fixed and predictable. The HackerRank problem expects a specific deterministic hash value (3713081631934410656) for the tuple (1, 2), which aligns with Python 2 behavior. Therefore, running the code in Python 3 on your machine gives a different result because of the randomization, but switching to Python 2, where hash() is consistent, produces the expected output and passes the judge. This is why using Python 2 helps solve the problem correctly.
This is my code and if you are using it too change the python 3 to python2 first from the menu above the shell
I had to switch the language to python 2 and use raw_input() instead of input() to get it to work. It seems that the hash function works differently in python 3 than it did in python 2, which is likely the version of python this challenge was written for. HackerRank really needs to update this challenge for python 3!
This is my python 2 code I used:
compute and print the hash
print hash(elements)
Here is - Javascript project for students
For some reason, Python 3 throws wrong answer. It generates correct answer with Python 2.
if name == 'main':
n = int(input())
t = tuple(map(int, input().split()))
print(hash(t))
our Output (stdout)
-3550055125485641917 `
I Didn't understand how to use the hash() please explain