STUDY/Algorithm

[프로그래머스] LEVEL 1 문자열 내 마음대로 정렬하기, python3

sinawi95 2019. 10. 16. 11:01
728x90

import collections
def solution(strings, n):
    answer = []
    cp_str=[]
    len_str=len(strings)
    for i in range(len_str):
        cp_str.append(strings[i][n])
    cp_str.sort()                                   # 인덱스 값끼리 정렬
    cp_str=list(collections.Counter(cp_str).keys()) # 중복값 제거
    
    for i in range(len(cp_str)):
        cnt=0
        ex=[]                               # 인덱스 값이 중복되는 strings 저장
        for j in range(len_str):
            if strings[j][n]==cp_str[i]:    # strings의 n번째와 cp_str이 같으면 ex에 추가
                cnt+=1
                ex.append(strings[j])
            if cnt:                         # 중복값 있는 경우 그 값끼리 정렬
                ex.sort()
        answer= answer+ex                   # answer에 값 저장

    return answer