Prepare > Python > Sets > Check Subset

2023. 7. 27. 19:36HackerRank-Python

 

Check Subset | HackerRank

Verify if set A is a subset of set B.

www.hackerrank.com

 

문제


Input Format

The first line will contain the number of test cases, T.
The first line of each test case contains the number of elements in set A.
The second line of each test case contains the space separated elements of set A.
The third line of each test case contains the number of elements in set B.
The fourth line of each test case contains the space separated elements of set B.

Output Format

Output True or False for each test case on separate lines.

 

If set  A is subset of set B, print True.
If set A is not a subset of set B, print False.

 

 

=> T는 테스트 횟수

집합A의 원소 수, 집합 A의 원소들, 집합 B의 원소 수, 집합 B의 원소들 이 T번 INPUT된다

A가 B의 부분집합인지 판별하라

 

 

 

 

코드


T = int(input())

for i in range(T):
    a = input()
    A = set(map(int,input().split()))
    b = input()
    B = set(map(int, input().split()))
    
    print(A.issubset(B))

 

 

 

 

노트


issubset() 함수

a.issubset(b) # a가 b의 부분집합인지 불린(boolean)값으로 리턴
b.issubset(a) # b가 a의 부분집합인지 불린(boolean)값으로 리턴

 

 

issuperset() 함수

a.issuperset(b) # a의 부분집합이 b인지
b.issuperset(a) # b의 부분집합이 a인지
  • issubset() 함수와 반대

 

 

isdisjoint()

a.isdisjoint(b) # a와 b의 교집합이 있으면 True
b.isdisjoint(a) # b와 a의 교집합이 있으면 True

 

 

 

두 번째 시도

T = int(input())

for _ in range(T):
	a = int(input())
	A = set(int(x) for x in input().split())
    b = int(input())
    B = set(int(x) for x in input().split())
    print(A & B == A)

 

 

 

참조


 

18. set(집합)

## 1. set(집합) - set은 수학에서 이야기하는 집합과 비슷합니다. - 순서가 없고, 집합안에서는 unique한 값을 가집니다. - 그리고 mutable 객체입니다.…

wikidocs.net