Prepare > Python > Basic Data Types > Tuples
Tuples | HackerRank
Learn about tuples and compute hash(T).
www.hackerrank.com
문제
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).
=> 정수 n과 공백으로 구분된 n개의 정수가 입력값으로 주어지면 해당 n개의 정수로 구성된 튜플 t를 생성합니다. 그런 다음 hash(t)의 결과를 계산하고 인쇄합니다.
코드
if __name__ == '__main__':
n = int(input())
integer_list = map(int, input().split())
print(integer_list)
print(hash(tuple(integer_list)))
노트
map함수
map([적용시킬 함수], [적용할 값들])
두 번째 파라미터로 받은 반복 가능한 자료형(리스트 | 튜플)을 첫 번째 파라미터로 받은 함수에 적용시킨 후
map 객체로 반환
import math # math.ceil 함수 사용
# 예제1) 리스트의 값을 정수 타입으로 변환
result1 = list(map(int, [1.1, 2.2, 3.3, 4.4, 5.5]))
print(f'map(int, 리스트) : {result1}')
# 예제2) 리스트 값 제곱
def func_pow(x):
return pow(x, 5) # x 의 5 제곱을 반환
result2 = list(map(func_pow, [1, 2, 3, 4, 5]))
print(f'map(func_pow, 리스트) : {result2}')
# 예제3) 리스트 값 소수점 올림
result3 = list(map(math.ceil, [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]))
print(f'map(func_ceil, 리스트) : {result3}')
tuple
tuple_1 = (1, 2, 3, "It is me")
del tuple_1[2]
# OUTPUT
TypeError: 'tuple' object does not support item deletion
- 리스트와 같은 자료형 중 하나
- 괄호 () 로 묶고, 쉼표(,)로 구분
- 한 번 지정하면 값을 변경할 수 없음
- 삭제, 수정, 추가 하고싶다면 리스트를 이용
hash 함수
- 파이썬 딕셔너리와 유사함
- 해시함수를 통해 hash() 입력값에 단 하나의 무작위 정수를 리턴함
참조
HackerRank Python Solution / Tuples
Tuples if __name__ == '__main__': n = int(raw_input()) integer_list = map(int, raw_input().split()) t = tuple(integer_list) print(hash(t)) 입력한 숫자형 타입을 튜플 타입으로 변환한 후, 해시함수를 적용하는 코드이다. 해설 ma
bohemihan.tistory.com
[python] 파이썬 map 함수 사용법과 예제
안녕하세요. BlockDMask입니다. 오늘은 파이썬 map 함수에 대한 이야기를 해보려 합니다. 1. 파이썬 map 함수 설명과 사용법 2. 파이썬 map 함수 예제 1. map 함수 설명과 사용법 1-1) 파이썬 맵 함수 기본
blockdmask.tistory.com
[자료구조] 해시(Hash)란 무엇인가
오늘은 보안에서 가장 자주 사용되는 해시함수의 근본인 자료구조 해시를 배워보겠다 📚 해시 테이블이란 컴퓨팅에서 키를 값에 매핑할 수 있는 구조인, 연관 배열 추가에 사용되는 자료 구조
ablue-1.tistory.com