2023. 7. 25. 20:39ㆍHackerRank-Python
Set Mutations | HackerRank
Using various operations, change the content of a set and output the new sum.
www.hackerrank.com
문제
TASK
You are given a set A and N number of other sets. These N number of sets have to perform some specific mutation operations on set A.
Your task is to execute those operations and print the sum of elements from set A.
Input Format
The first line contains the number of elements in set A.
The second line contains the space separated list of elements in set A.
The third line contains integer N, the number of other sets.
The next 2*N lines are divided into M parts containing two lines each.
The first line of each part contains the space separated entries of the operation name and the length of the other set.
Output Format
Output the sum of elements in set A.
=> A와 N으로 set과 명령어들을 줄건데 그대로 수행해라
코드
n = int(input())
A = set(map(int, input().split()))
N = int(input())
for _ in range(N):
s = input().split()
t = set(map(int,input().split()))
if s[0] == 'update':
A.update(t)
elif s[0] == 'intersection_update':
A.intersection_update(t)
elif s[0] == 'difference_update':
A.difference_update(t)
elif s[0] == 'symmetric_difference_update':
A.symmetric_difference_update(t)
print(sum(A))
노트
ANOTHER SOLUTION
_ = input()
a = set(int(x) for x in input().split(' ')) # map()대신 쓰는 방법
n = int(input())
for _ in range(n):
op, _ = input().split(' ') # 튜플로 자료를 받을 수 있음
b = set(int(x) for x in input().split(' '))
if op == "update":
a |= b # 연산자 사용
elif op == "intersection_update":
a &= b
elif op == "difference_update":
a -= b
elif op == "symmetric_difference_update":
a ^= b
print(sum(a))
해석
for 루프를 사용하여 input() 함수에서 입력받은 문자열을 공백으로 구분하여 리스트로 변환하고,
리스트에 있는 각 요소를 x에 할당한 후,
int(x) 함수를 사용하여 x를 정수로 변환하고,
set() 함수를 사용하여 중복을 제거하여 집합을 생성하고 반환하는 코드입니다.
예를 들어, 사용자가 "1 2 3 4 5"를 입력하면,
input() 함수는 "1 2 3 4 5"를 문자열로 반환합니다.
split() 함수는 "1 2 3 4 5"를 공백으로 구분하여 ['1', '2', '3', '4', '5']를 리스트로 반환합니다.
int() 함수는 '1'을 1로, '2'를 2로, '3'를 3으로, '4'를 4로, '5'를 5로 변환합니다.
set() 함수는 [1, 2, 3, 4, 5]에서 중복을 제거하여 {1, 2, 3, 4, 5}를 집합으로 반환합니다.
따라서 set(int(x) for x in input().split(' '))는 {1, 2, 3, 4, 5}를 반환합니다.
이건 좀 달라보이는...ㅋㅋㅋ 천천히 뜯어보는걸로 하자
if **name** == '**main**':
(_, A) = (int(input()),set(map(int, input().split())))
B = int(input())
for _ in range(B):
(command, newSet) = (input().split()[0],set(map(int, input().split())))
getattr(A, command)(newSet)
print (sum(A))
update() 함수
H = set("Hacker")
R = set("Rank")
H.update(R)
print (H)
# {'a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'}
- 집합에 원소를 추가할 때 사용
- 연산자(|=)로도 가능
intersection_update() 함수
H = set("Hacker")
R = set("Rank")
H.intersection_update(R)
print (H)
# {'a', 'k'}
- 집합을 교집합 원소들로 업데이트(수정)
- 연산자(&=)로도 가능
difference_update() 함수
H = set("Hacker")
R = set("Rank")
H.difference_update(R)
print (H)
# {'c', 'e', 'H', 'r'}
- 집합을 차집합 원소들로 업데이트(수정)
- 연산자(-=)로도 가능
symmetric_difference_update() 함수
H = set("Hacker")
R = set("Rank")
H.symmetric_difference_update(R)
print(H)
# {'c', 'e', 'H', 'n', 'r', 'R'}
- 집합을 합집합과 교집합의 차집합으로(두 차집합의 합집합) 업데이트(수정)
- 연산자(^=)로도 가능
두 번째 시도
n = int(input())
A = set(map(int, input().split()))
N = int(input())
for i in range(N):
order = input().split()
if order[0] == 'intersection_update':
A &= set(map(int, input().split()))
elif order[0] == 'update':
A |= set(map(int, input().split()))
elif order[0] == 'symmetric_difference_update':
A ^= set(map(int, input().split()))
elif order[0] == 'difference_update':
A -= set(map(int, input().split()))
print(sum(A))
참조
Set Mutations Python | HackerRank Solutions
We have seen the applications of union, intersection, difference and symmetric difference operations, but these operations do not make any …
hackerranksolution.in
Bard로 생성됨
bard.google.com
(PYTHON) Day - 11 Sets(3)
Reference 문제 출처 - HackerRank 파이썬 연습 - Practice - Python 개인적인 생각과 상상으로 작성한 내용들이 포함되어 있습니다문제를 풀고 Discussion Tab을 참고하며 코드 스타일을 개선하려고 노력하고
taejin0527.github.io
'HackerRank-Python' 카테고리의 다른 글
Prepare > Python > Sets > Check Subset (0) | 2023.07.27 |
---|---|
Prepare > Python > Sets > The Captain's Room (0) | 2023.07.26 |
Prepare > Python > Sets > Set .symmetric_difference() Operation (0) | 2023.07.24 |
Prepare > Python > Sets > Set .difference() Operation (0) | 2023.07.24 |
Prepare > Python > Sets > Set .intersection() Operation (0) | 2023.07.24 |