문자열 내 마음대로 정렬하기 (Level 1)

정은경·2020년 8월 25일
0

1. 문제

2. 나의 풀이

import heapq

def solution(strings, n):
    answer = []
    temp = []
    for s in strings:
        heapq.heappush(temp, (s[n], s))
    
    while temp:
        answer.append(heapq.heappop(temp)[1])
    return answer

3. 남의 풀이

def strange_sort(strings, n):
    '''strings의 문자열들을 n번째 글자를 기준으로 정렬해서 return하세요'''
    return sorted(strings, key=lambda x: x[n])

strings = ["sun", "bed", "car"] 
print(strange_sort(strings, 1))
  • sorted에 key 옵션을 주어서 해결하다니!
profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글