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 | 31 |
Tags
- Kaggle
- leetcode
- 맥북
- 파이썬
- 자연어처리
- AI 경진대회
- 프로그래머스 파이썬
- gs25
- ubuntu
- 금융문자분석경진대회
- SW Expert Academy
- 더현대서울 맛집
- Git
- 프로그래머스
- 우분투
- github
- 백준
- ChatGPT
- dacon
- 캐치카페
- programmers
- Docker
- Baekjoon
- 편스토랑 우승상품
- 편스토랑
- PYTHON
- Real or Not? NLP with Disaster Tweets
- 코로나19
- 데이콘
- hackerrank
Archives
- Today
- Total
솜씨좋은장씨
[BaekJoon] 25859번 : Sort by Frequency (Python) 본문
728x90
반응형
코딩 1일 1문제! 오늘의 문제는 백준의 Sort by Frequency 입니다.
25859번: Sort by Frequency
The input consists of a single string, appearing on a line by itself, starting in column 1 and not exceeding column 70. The input will contain only lowercase letters (at least one letter).
www.acmicpc.net
🧑🏻💻 문제 풀이
입력 받은 단어를 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))
GitHub - SOMJANG/CODINGTEST_PRACTICE: 1일 1문제 since 2020.02.07
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'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