๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[Programmers] [2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ] [Python] Level2_ํŠœํ”Œ

Dbswnstjd 2022. 10. 14. 19:14

๋ฌธ์ œ

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ’€์ด

# ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๋‹จ๊ณ„ - 2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ - ํŠœํ”Œ
def solution(s):
    answer = []
    s_list = list(map(int, s.replace("{", "").replace("}", "").split(",")))
    
    number = {}
    for i in s_list:
        if i not in number:
            number[i] = 1
        else:
            number[i] += 1
    
    snum = sorted(number.items(), key=lambda x:x[1], reverse=True)
    for k in snum:
        answer.append(k[0])
    
    return answer

solution("{{2},{2,1},{2,1,3},{2,1,3,4}}")