HackerRank-Python
Prepare > Python > Sets > Set .add()
stem_sw
2023. 7. 21. 21:43
Set .add() | HackerRank
Add elements to set.
www.hackerrank.com
문제
Task
Apply your knowledge of the .add() operation to help your friend Rupal.
Rupal has a huge collection of country stamps. She decided to count the total number of distinct country stamps in her collection. She asked for your help. You pick the stamps one by one from a stack of N country stamps.
Find the total number of distinct country stamps.
Input Format
The first line contains an integer N, the total number of country stamps.
The next N lines contains the name of the country where the stamp is from.
=> Rupal은 여러나라 stamps 컬렉션을 갖고 있는데 개별국가의 stamps의 수를 구해라
첫번째 Input은 정수 N, 그 다음 N줄은 stamp 나라 이름들
코드
a = input()
stamps = set()
for i in range(int(a)):
stamps.add(input())
print(len(stamps))
노트
set.add()
s = set('HackerRank')
s.add('H')
print(s)
# set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])
print(s.add('HackerRank'))
# None
print(s)
set(['a', 'c', 'e', 'HackerRank', 'H', 'k', 'n', 'r', 'R'])
myset = set(['a', 'b'])
myset.add('c')
print(myset)
# {'a', 'c', 'b'}
myset.add('a') # As 'a' already exists in the set, nothing happens
myset.add((5, 4))
print(myset)
{'a', 'c', 'b', (5, 4)}
- set 자료형에 한 개 요소를 추가하는 메소드
- 어떤 인자들 하나의 요소로 넣음
- 넣으려는 값이 이미 존재하면 아무일도 일어나지 않음
- None을 리턴함