HackerRank Python: Tuples Solution

Table of Contents

Question

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).

Note: hash() is one of the functions in the __builtins__ module, so it need not be imported.

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).

Sample Input 0

2
1 2

Sample Output 0

3713081631934410656

Solution

				
					n = int(input())  # Read the integer n
integer_list = map(int, input().split())  # Read the space-separated integers and convert them to integers

t = tuple(integer_list)  # Create a tuple from the integers

result = hash(t)  # Compute the hash of the tuple

print(result)  # Print the result


				
			
  1. We start by reading the integer n from input.
  2. Then, we read the space-separated integers using input().split(), and map(int, ...) converts them to integers.
  3. Next, we create a tuple t from the integers using tuple(integer_list).
  4. Finally, we compute the hash of the tuple using hash(t) and print the result.

Note: The hash() function is a built-in function in Python that returns the hash value of an object. In this case, it returns a unique hash value for the tuple t.

If you find anything wrong in this Solution, feel free to reach us in the comment section.

Sharing Is Caring:

Leave a Comment