문제
https://www.acmicpc.net/problem/1181
내가 생각했던 풀이과정
1. 단순하게 리스트 안에 단어가 있다면, 추가를 하지 않는다.
2. sort() 함수를 통해 알파벳으로 정렬
3. sorted 람다 함수를 통해 길이별로 정렬
나의 풀이
N = int(input()) temp = [] for i in range(N): word = input() if word in temp: continue else: temp.append(word) temp.sort() temp = sorted(temp, key = lambda x: len(x)) for i in temp: print(i)
다른 사람의 풀이
words_num = int(input()) words_list = [] for _ in range(words_num): word = str(input()) word_count = len(word) words_list.append((word, word_count)) #중복 삭제 words_list = list(set(words_list)) #단어 숫자 정렬 > 단어 알파벳 정렬 words_list.sort(key = lambda word: (word[1], word[0])) for word in words_list: print(word[0])
1. 리스트를 추가할 때 튜플 형식(문자, 문자길이)으로 추가
2. set을 이용하여 중복 제거
3. lambda를 이용하여 단어, 숫자 정렬
새로 알게된 점, 확인해야할 점
'알고리즘 > 백준 알고리즘' 카테고리의 다른 글
[Baekjoon] 백준 알고리즘: 14888 - 연산자 끼어넣기 (0) | 2024.12.05 |
---|---|
[Baekjoon] 백준 알고리즘: 1269 - 대칭 차집합 (0) | 2021.11.23 |
[Baekjoon] 백준 알고리즘: 11899 - 괄호 끼워넣기 (0) | 2021.06.19 |
[Baekjoon] 백준 알고리즘: 1302 - 베스트셀러 (2) | 2021.06.14 |
[Baekjoon] 백준 알고리즘: 1543 - 문서검색 (0) | 2021.06.07 |
댓글