Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- SW Expert Academy
- 우분투
- Kaggle
- 더현대서울 맛집
- Git
- 자연어처리
- 편스토랑
- 코로나19
- hackerrank
- 백준
- dacon
- github
- 프로그래머스 파이썬
- 편스토랑 우승상품
- programmers
- gs25
- Docker
- PYTHON
- 캐치카페
- 프로그래머스
- Real or Not? NLP with Disaster Tweets
- AI 경진대회
- 맥북
- ubuntu
- 데이콘
- leetcode
- Baekjoon
- ChatGPT
- 파이썬
- 금융문자분석경진대회
Archives
- Today
- Total
솜씨좋은장씨
[BaekJoon] 25859번 : Sort by Frequency (Python) 본문
728x90
반응형
코딩 1일 1문제! 오늘의 문제는 백준의 Sort by Frequency 입니다.
🧑🏻💻 문제 풀이
입력 받은 단어를 collections 의 Counter 를 활용하여 단어 속에 있는 알파벳을 카운팅 한 다음
카운팅 된 수 내림차순 -> 알파벳 오름차순으로 정렬한 뒤
cnt_items = sorted(Counter(word).items(), key=lambda x: (-x[1], x[0]))
해당 값으로 정답을 만들었습니다.
answer = ""
for item in cnt_items:
answer += item[0] * item[1]
전체 코드는 아래를 참고해주세요.
🧑🏻💻 코드 ( Solution )
from collections import Counter
def sort_by_frequency(word):
answer = ""
cnt_items = sorted(Counter(word).items(), key=lambda x: (-x[1], x[0]))
for item in cnt_items:
answer += item[0] * item[1]
return answer
if __name__ == "__main__":
word = input()
print(sort_by_frequency(word=word))
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[BaekJoon] 24608번 : Average Character (Python) (0) | 2022.11.25 |
---|---|
[BaekJoon] 25773번 : Number Maximization (Python) (0) | 2022.11.24 |
[BaekJoon] 25785번 : Easy-to-Pronounce Words (Python) (0) | 2022.11.21 |
[BaekJoon] 6324번 : URLs (Python) (0) | 2022.11.20 |
[BaekJoon] 26040번 : 특정 대문자를 소문자로 바꾸기 (Python) (0) | 2022.11.19 |
Comments