Tuple in python [Programming Problem]

I have to write a program which,
Given an integer, n , and n space-separated integers as input, create a tuple,t , of those n integers. Then compute and print the result of hash(t) .

Input Format

The first line contains an integer, n, denoting the number of elements in the tuple.
The second line contains n space-separated integers describing the elements in tuple t .

Output Format

Print the result of hash(t).

Problem in details at hackerrank

my code:

n = int(input())
list_B = []
value = input()
list = value.split()
s = len(list)
if s > n:
    for x in range(n):
        list_B.append(int(list[x]))
t = tuple(list_B)
print(hash(t))

Why my code gives wrong answer ?

I also collect the code which is correct from youtube

This code is,

n = int(input())
li = []
for x in input().split():
    li.append(int(x))
t = tuple(li)
print(hash(t))

Why this code is correct?
This code fail to denoting the number of elements in the tuple and moreover here n is looks like kind of useless :roll_eyes:

Can you please make it clear? :sob:
Please…

n is in fact useless for doing this in Python, as

for x in input().split():
  ...

is equivalent to

user_input = input()
user_input_split = user_input.split()
for i in range(len(user_input_split)):
  ...

(My syntax may be a hair off, as I’m working from memory)

Hello,Smith.Actually,i’m not clear yet,Why it’s gives wrong answer? :tired_face:

Well, your if s > n makes no sense. Why is it there?

Also, why are you printing the hash of your tuple?

i was trying to append n number of element in this list.Then make it tuple.

I print hash of tuple cus they want result of hash(t) as print in this problem

Is that a wrong way? :slightly_frowning_face:
I’m sorry if it’s to simple too understand.But i’m trying to understand :pleading_face:

I’d start with printing the tuple and get that right before you print the hash.

Heck, I’d start with printing the list.

Since you have if s > n, you are only converting the input into a list if more than n entries were provided. That isn’t what you want at all. Notice that the solution does not use an if statement like that at all.

1 Like