Posts Set.add() (Python 3)
Post
Cancel

Set.add() (Python 3)

  • URL https://www.hackerrank.com/challenges/py-set-add/problem
  • 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 country stamps.
    • Find the total number of distinct country stamps.
  • Input Format
    • The first line contains an integer , the total number of country stamps.
    • The next lines contains the name of the country where the stamp is from.
  • Constraints
    • 0 < N < 1000
  • Output Format
    • Output the total number of distinct country stamps on a single line.

문제풀이

  • set 형태에 add를 하는것
  • set의 중복제거 성질을 이용하여 중복을 제거하고 들어온 나라의 이름 갯수를 출력
  • 굳이 중복제거 함수를사용하지 않아도됨
1
2
3
4
5
6
7
n = int(input())
dict_set = set()

for _ in range((n)):
    dict_set.add(input())
    
print(len(dict_set))
1
2
3
4
5
6
7
8
9
10
11
 7
 UK
 China
 USA
 France
 New Zealand
 UK
 France


5
This post is licensed under CC BY 4.0 by the author.