코딩테스트

백준 1181

eunGI 2022. 9. 22. 12:39
import sys


def setting(data):
    return len(data)


count = int(input())

word_list = []

for _ in range(count):
    word_list.append(sys.stdin.readline().strip())

word_list = list(set(word_list))
word_list.sort()
word_list.sort(key=setting)

for i in word_list:
    print(i)

1. set 자료형을 이용해서 중복을 제거만 한 뒤, 다시 리스트로 바꿔준다.

* set 자료형

출처: https://velog.io/@daybreak/Python%EC%97%90%EC%84%9C-%EB%A6%AC%EC%8A%A4%ED%8A%B8-%EC%A4%91%EB%B3%B5%EC%A0%9C%EA%B1%B0

 

2. 사전 순으로 정렬을 먼저 해준다. 

3. 사전 순으로 정렬되어있는 상태에서 단어 길이를 기준으로 정렬해준다.

 

*sys.stdin.readline()은 개행문자를 제거해주지 않고 포함하여 입력받기 때문에 strip() 함수를 이용해 공백을 제거해주었다.

'코딩테스트' 카테고리의 다른 글

백준 1764  (0) 2022.09.27
백준 1427  (0) 2022.09.22
백준 10989 [계수 정렬]  (0) 2022.09.22
백준 1015번  (0) 2022.09.19
백준 1010번  (0) 2022.09.15