https://rosalind.info/problems/seto/
ROSALIND | Introduction to Set Operations
It appears that your browser has JavaScript disabled. Rosalind requires your browser to be JavaScript enabled. Introduction to Set Operations solved by 2192 2012년 7월 19일 12:00:00 오전 by Rosalind Team Topics: Set Theory Forming New Sets Just as num
rosalind.info
Problem
If AA and BB are sets, then their union A∪BA∪B is the set comprising any elements in either AA or BB; their intersection A∩BA∩B is the set of elements in both AA and BB; and their set difference A−BA−B is the set of elements in AA but not in BB.
Furthermore, if AA is a subset of another set UU, then the set complement of AA with respect to UU is defined as the set Ac=U−AAc=U−A. See the Sample sections below for examples.
Given: A positive integer nn (n≤20,000n≤20,000) and two subsets AA and BB of {1,2,…,n}{1,2,…,n}.
Return: Six sets: A∪BA∪B, A∩BA∩B, A−BA−B, B−AB−A, AcAc, and BcBc (where set complements are taken with respect to {1,2,…,n}{1,2,…,n}).
Sample Dataset
10
{1, 2, 3, 4, 5}
{2, 8, 5, 10}
Sample Output
{1, 2, 3, 4, 5, 8, 10}
{2, 5}
{1, 3, 4}
{8, 10}
{8, 9, 10, 6, 7}
{1, 3, 4, 6, 7, 9}
이 문제는 두 집합의 합집합,교집합,서로 뺀 집합, 여집합을 구하는 문제이다. 이는 간단하게 파이썬의 집합 자료형을 사용하여 풀 수 있다.
if __name__=='__main__':
with open(r"파일경로",'r') as fi:
temp=[]
for i in fi.readlines():
temp.append(i.strip())
n=int(temp[0])
total_set={i for i in range(1,n+1)}
a_set=set(map(int,temp[1][1:-1].split(', ')))
b_set=set(map(int,temp[2][1:-1].split(', ')))
with open(r'파일경로','w',encoding='utf-8') as f:#출력을 그대로 파일에 쓰기
print(a_set|b_set,file=f)
print(a_set&b_set,file=f)
print(a_set-b_set,file=f)
print(b_set-a_set,file=f)
print(total_set-a_set,file=f)
print(total_set-b_set,file=f)
'문제해결(PS) > ROSALIND' 카테고리의 다른 글
Introduction to Pattern Matching (9) | 2024.10.16 |
---|---|
Sorting by Reversals (1) | 2024.10.03 |
Interleaving Two Motifs (2) | 2024.09.25 |
Distances in Trees (2) | 2024.09.24 |
Motzkin Numbers and RNA Secondary Structures (1) | 2024.09.22 |