[Algorithm] 백준 1181번 Python

Serin Yoon·2021년 2월 20일
1

Algorithm

목록 보기
2/7
post-thumbnail

Q. 알파벳 소문자로 이루어진 N개의 단어가 들어오면 아래와 같은 조건에 따라 정렬하는 프로그램을 작성하시오.

  • 길이가 짧은 것부터
  • 길이가 같으면 사전 순으로
import sys #시간초과 방지 목적

input = sys.stdin.readline

words = []

for _ in range(int(input())):
    data = input()[:-1] #\n 제거
    words.append((data, len(data))) #단어와 단어의 길이를 함께 저장

words = list(set(words)) #set 함수로 중복 요소 제거 후 리스트로 형변환

words.sort(key=lambda word: (word[1], word[0])) #단어 길이순 정렬 후 알파벳순 정렬

for word in words:
    print(word[0])
profile
티스토리로 이사했습니다 🏠

0개의 댓글