2023. 9. 25. 20:50ㆍHackerRank-Python
Company Logo | HackerRank
Print the number of character occurrences in descending order.
www.hackerrank.com
문제
A newly opened multinational brand has decided to base their company logo on the three most common characters in the company name. They are now trying out various combinations of company names and logos based on this condition. Given a string , which is the company name in lowercase letters, your task is to find the top three most common characters in the string.
- Print the three most common characters along with their occurrence count.
- Sort in descending order of occurrence count.
- If the occurrence count is the same, sort the characters in alphabetical order.
Input Format
A single line of input containing the string S
Output Format
Print the three most common characters along with their occurrence count each on a separate line.
Sort output in descending order of occurrence count.
If the occurrence count is the same, sort the characters in alphabetical order.
=> S의 알파벳중 가장 반복이 많은 3개를 그 횟수와 함께 출력하라. 순서는 반복많은순, 알파벳 순이다.
코드
import math
import os
import random
import re
import sys
if __name__ == '__main__':
s = input()
---------------------------------------------------------------------
from collections import Counter
s_count = Counter(s)
S = sorted(s_count.items(), key=lambda x: (-x[1], x[0]))
for i in range(3):
print(*S[i])
노트
sorted()
sorted(iterable, key= , reverse= )
- 원본유지가 특징임
- iterabel: 정렬할 대상
- key: 정렬기준(함수식 필요)
- reverse: 역순 여부. Default는 오름차순(False)
딕셔너리에서 활용
d = {'e': 1, 'b': 2, 'd': 3, 'a': 2, 'c': 4}
key로 정렬, key리스트 리턴
sorted(d)
# ['a', 'b', 'c', 'd', 'e']
key 역순 정렬, key리스트 리턴
sorted(d, reverse=True)
# ['e', 'd', 'c', 'b', 'a']
- reverse 파라미터 이용
key로 정렬, key-value쌍 리턴
sorted(d.items())
# [('a', 2), ('b', 2), ('c', 4), ('d', 3), ('e', 1)]
- d.items() 이용
value로 정렬, value리스트 리턴
sorted(d.values())
# [1, 2, 2, 3, 4]
- reverse 파라미터 사용 가능
value로 정렬, key-value쌍 리턴
sorted(d.items(), key = lambda x: x[1])
# [('e', 1), ('b', 2), ('a', 2), ('d', 3), ('c', 4)]
value내림차순 정렬, key-value쌍 리턴
sorted(d.items(), key = lambda x: -x[1])
# [('c', 4), ('d', 3), ('b', 2), ('a', 2), ('e', 1)]
value오름차순, key오름차순, key-value쌍 리턴
sorted(d.items(), key = lambda x: (x[1], x[0]))
# [('e', 1), ('a', 2), ('b', 2), ('d', 3), ('c', 4)]
value내림차순, key오름차순, key-value쌍 리턴
sorted(d.items(), key = lambda x: (-x[1], x[0]))
# [('c', 4), ('d', 3), ('a', 2), ('b', 2), ('e', 1)]
???
sorted(d.items(), key = lambda x: x[1], reverse=True)
# [('c', 4), ('d', 3), ('b', 2), ('a', 2), ('e', 1)]
참조
[Python] 딕셔너리(Dictionary) 정렬 방법
🌈 파이썬 딕셔너리(Dictionary) 정렬 하기 파이썬에서 딕셔너리를 정렬하기 위한 가장 쉬운 방법은 sorted 메서드를 사용하는 것이다. 1️⃣ Key 기준 정렬 ◾ Case 1 : Key를 오름차순하여 Key만 리스트
cocobi.tistory.com
[파이썬] sorted() 정렬 함수 파헤치기
이터레이터로부터 새로운 정렬된 리스트를 마드는 sorted() 내장 함수가 있습니다. sorted() 함수의 다양한 사용법을 알아보겠습니다. Syntax 첫번째 인자 - 이터러블 객체 (list, dict, tuple 등) 두번째 인
wellsw.tistory.com
'HackerRank-Python' 카테고리의 다른 글
Prepare > Python > Built-Ins > Input() (0) | 2023.09.27 |
---|---|
Prepare > Python > Built-Ins > Zipped! (0) | 2023.09.26 |
Prepare > Python > Collections > Piling Up! (0) | 2023.09.22 |
Prepare > Python > Collections > Word Order (0) | 2023.09.22 |
Prepare > Python > Itertools > Compress the String! (0) | 2023.09.20 |