나는 이렇게 학습한다/Algorithm & SQL

0118. Ordered Count of Characters

daco2020 2023. 1. 18. 23:23
반응형

Count the number of occurrences of each character and return it as a (list of tuples) in order of appearance. For empty output return (an empty list).

Consult the solution set-up for the exact data structure implementation depending on your language.

Example:

ordered_count("abracadabra") == [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)]



Solution:

def ordered_count(inp):
    from collections import Counter
    count_dict = Counter(list(inp))
    return [tuple for tuple in zip(count_dict.keys(), count_dict.values())]
from collections import Counter

def ordered_count(inp):
    return list(Counter(inp).items())


반응형