0

I am practising Python questions on HackerRank. Here's the question.

This is my solution:

if __name__ == '__main__':
    n = int(input())
    i = tuple(map(int,input().split()))
    print(hash(i))
    

Desired Output is

3713081631934410656

However, the output I get is

-3550055125485641917

Why is that the case?

Someone tested my code, and it gave them the desired code. I tried copying and pasting my code again. Still didn't work.

enter image description here

Edit

It started working. I had to select Pypy3 (shoutout Bereal). Also, big ups to Chepner for pointing out Python3 Hash functionality.

halfer
  • 19,824
  • 17
  • 99
  • 186
Inshaullah
  • 99
  • 5
  • 1
    I copy-pasted your code there, and got exactly what's expected. – bereal Oct 09 '22 at 15:56
  • Tangentially, the code you put inside `if _ name__ == ’__main__’:` should be absolutely trivial. The condition is only useful when you `import` this code; if all the useful functionality is excluded when you `import`, you will never want to do that anyway. See also https://stackoverflow.com/a/69778466/874188 – tripleee Oct 09 '22 at 16:06
  • I removed the `if __name__ == '__main__'` part, still the same problem. – Inshaullah Oct 09 '22 at 16:08
  • is it being called a failure, they list the output as "Sample" output. The hash value could vary depending on a number of factors. – LhasaDad Oct 09 '22 at 16:09
  • 1
    FWIW, `hash((1,2))` produces the negative value in Python 3.9, the positive in Python 2.7, on my local machine. – chepner Oct 09 '22 at 16:09
  • 1
    Choose "Pypy 3" in the dropdown menu above the code. – bereal Oct 09 '22 at 16:11
  • @LhasaDad yeah, it shows test case failed. The problem wants the value to be exactly as the desired output. I don't know why am I getting that value. – Inshaullah Oct 09 '22 at 16:11
  • 2
    I got OP's result. – tdelaney Oct 09 '22 at 16:12
  • 1
    i think @chepner has the real reason the value is different. – LhasaDad Oct 09 '22 at 16:13
  • Shoutout to bereal and chepner for pointing that out. Thanks <3 – Inshaullah Oct 09 '22 at 16:13
  • 1
    Not thrilled with a test written that is so dependent on the environment settings. – LhasaDad Oct 09 '22 at 16:15
  • 1
    in the real world you would calculate the hash of two tuples and check if the hash matched (two tuples that were the same) here you have a predefined value that could vary as we saw with the answer – LhasaDad Oct 09 '22 at 16:16

1 Answers1

1

From the discussion section: "THE TEST is for PYPY3, not Python3. Make sure to change language to Pypy3 !!!"

source: https://www.hackerrank.com/challenges/python-tuples/forum

In general @Inshaullah, it's best to check out the discussion forums on these sites if there's problems along these lines.

optimus_prime
  • 1,039
  • 2
  • 10
  • 21