기억은 짧고 기록은 길다
[프로그래머스/Programmers] 튜플 - Python 본문
Link
코딩테스트 연습 - 튜플
"{{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
Solution
def solution(s):
answer = []
s = s.lstrip('{').rstrip('}').split('},{')
t = []
for i in range(len(s)):
t.append(s[i].split(','))
d = {}
for i in range(len(t)):
for j in range(len(t[i])):
if t[i][j] in list(d.keys()):
d[t[i][j]] += 1
else:
d[t[i][j]] = 1
d = sorted(d.items(), key=lambda x: x[1], reverse=True)
for i in range(len(d)):
answer.append(int(d[i][0]))
return answer
🔑 key point: s에서 같은 숫자가 중복된 횟수가 많은 순으로 반환
📌 Tip: lstrip(), rstrip()
Other Solution
def solution(s):
answer = []
s = s.lstrip('{').rstrip('}').split('},{')
t = []
for i in s:
t.append(i.split(','))
t.sort(key=len)
for i in t:
for j in range(len(i)):
if int(i[j]) not in answer:
answer.append(int(i[j]))
return answer
🔑 key point: s에 들어있는 집합을 길이 순으로 정렬하여 새로운 값 순으로 반환
📌 Tip: lstrip(), rstrip()
from collections import Counter
def solution(s):
answer = []
s = Counter(s)
s = sorted(s.items(), key=lambda x: x[1], reverse=True)
for i in s:
if i[0].isdigit() == True:
answer.append(int(i[0]))
return answer
🔑 key point: s에서 같은 숫자가 중복된 횟수가 많은 순으로 반환
📌 Tip: Counter(), isdigit()'CodingTest > programmers' 카테고리의 다른 글
| [프로그래머스/Programmers] 피보나치 수 - Python (0) | 2021.09.23 |
|---|---|
| [프로그래머스/Programmers] 타겟 넘버 - Python (0) | 2021.09.23 |
| [프로그래머스/Programmers] 최솟값 만들기 - Python (0) | 2021.09.22 |
| [프로그래머스/Programmers] 최댓값과 최솟값 - Python (0) | 2021.09.22 |
| [프로그래머스/Programmers] 짝지어 제거하기 - Python (0) | 2021.09.21 |
Comments