[2019 카카오 개발자 겨울 인턴십] 코딩테스트 문제 : 튜플

chanto11

·

2020. 5. 16. 12:48

문제 : https://programmers.co.kr/learn/courses/30/lessons/64065

 

코딩테스트 연습 - 튜플

"{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4] "{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4] "{{4,2,3},{3},{2,3,4,1},{2,3}}" [3, 2, 4, 1]

programmers.co.kr

풀이 : 

def solution(s):
    
    new_s = s[2:-2].split('},{')
    numbers = []
    for z in new_s:
        for y in z.split(','):
            numbers.append(int(y))
            
    data = set(numbers)
    counter = {}
    for sd in data:
        counter[sd] = numbers.count(sd)
    
    answer = []
    for k, v in sorted(counter.items(), key=lambda x: x[1], reverse=True):
        answer.append(k)
 
    return answer

입출력 예제 :

s                                                                            result

"{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4]
"{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4]
"{{20,111},{111}}" [111, 20]
"{{123}}" [123]
"{{4,2,3},{3},{2,3,4,1},{2,3}}" [3, 2, 4, 1]