HackerRank-Python

Prepare > Python > Sets > Check Strict Superset

stem_sw 2023. 7. 28. 19:51
 

Check Strict Superset | HackerRank

Check if A is a strict superset of the other given sets.

www.hackerrank.com

 

문제


You are given a set A and n other sets.
Your job is to find whether set A is a strict superset of each of the n sets.

Print True, if A is a strict superset of each of the N sets. Otherwise, print False.

A strict superset has at least one element that does not exist in its subset.

 

Input Format

The first line contains the space separated elements of set A.
The second line contains integer n, the number of other sets.
The next n lines contains the space separated elements of the other sets.

 

Output Format

Print True if set A is a strict superset of all other N sets. Otherwise, print False.

 

 

=> 집합 A, 정수 n, n개의 집합을 차례로 input.

A가 n개의 집합들 모두에 대해 진부분집합인지 판별하라

  •  진부분집합: A⊂B이고 A≠B

 

 

 

 

코드


A = set(int(x) for x in input().split())
n = int(input())


judge = True

for i in range(n):
    other_set = set(int(x) for x in input().split())
    
    if A != other_set:
        judge = A.issuperset(other_set) & judge
    

print(judge)

 

 

 

 

노트


 

a = set([1, 2, 3, 4])
b = set([2, 3, 4])
c = set([1, 2, 3, 4])

print(a.issuperset(b))
print(a.issuperset(c))
print(a != c)
True
True
False

 

 

처음 짠 코드

A = set(int(x) for x in input().split())
n = int(input())


judge = True

for i in range(n):
    other_set = set(int(x) for x in input().split())
	
    judge = A.issuperset(other_set) & judge
    

print(judge)
  • 진부분집합이 아니라 부분집합인지만 판별했는데, 신기하게도 hackerrank에서는 옳다고 해줌

 

 

ANOTHER CODE

A = set(map(int, input().split()))
for _ in range(int(input())):
    X = set(map(int, input().split()))
    if A.issuperset(X) != True or len(A) == len(X): 
        print(False)
        break 
else: print(True)
  • if를 이용해 불린값을 만들고, 결과는 True | False 로 지정해둔게 신기했음. 생각하는 순서의 차이? 가 느껴짐

 

 

두 번째 시도

A = set(map(int, input().split()))
n = int(input())

judge = True
for i in range(n):
    n_i = set(map(int, input().split()))
    judge = (A.issuperset(n_i)) & (A != n_i) & judge
    
print(judge)

 

 

 

 

참조


 

부분집합, 진부분집합, 상위집합, 진상위집합, 포함관계 - 제타위키

다음 문자열 포함...

zetawiki.com

https://github.com/johnjullies/Algorithms/blob/master/hackerrank/python/Check%20Strict%20Superset.py