ブリブリ備忘録 おっ、python

HackerRankの問題とコメント(python3) 拙いですが...

Tuples 配列とタプルの違いや、標準入力から配列を作成する方法

・問題

Task 
Given an integer, , and  space-separated integers as input, create a tuple, , of those  integers. Then compute and print the result of .

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, , denoting the number of elements in the tuple. 
The second line contains  space-separated integers describing the elements in tuple .

Output Format

Print the result of .

Sample Input 0

2
1 2

Sample Output 0

3713081631934410656

ソースコード

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

・コメント

問題内で出てくるhash()という関数の意味が分からないという点に一番苦労する問題。

1 2という入力に対して3713081631934410656という意味のわからない出力が得られてしまうので余計に混乱をしてしまう。

しかし、この問題の本質はhash()を理解するということではないし、理解できていなくても解ける問題なので、hash()については詳しく説明はしない。

問題文の中に

The second line contains  space-separated integers describing the elements in tuple .

との記述があるのでtはtupleである。

さらに、出力は hash(t)なのでhashはtupleを括弧内にとる関数であると分かる。

そこで、入力からtupleを作成する方法をまず考えることにする。

与えられる入力を見れば、スペースで区切られただけの文字列である。

前にもやったようだが、python3では、スペースで区切られた文字列から配列を取得するには

list =list(map(int, input().split()))

のようにするらしい...(間隔が空きすぎて覚えていない)

また、list =[int(x) for x in input().split()]

という形にしてもよい。

・URL

https://www.hackerrank.com/challenges/python-tuples/problem