Skip to content

Latest commit

 

History

History
40 lines (24 loc) · 770 Bytes

Frequency_sequence.md

File metadata and controls

40 lines (24 loc) · 770 Bytes

CodeWars Python Solutions


Frequency sequence

Return an output string that translates an input string s/$s by replacing each character in s/$s with a number representing the number of times that character occurs in s/$s and separating each number with the character(s) sep/$sep.

freq_seq("hello world", "-"); // => "1-1-3-3-2-1-1-2-1-3-1"
freq_seq("19999999", ":"); // => "1:7:7:7:7:7:7:7"
freq_seq("^^^**$", "x"); // => "3x3x3x2x2x1"

Given Code

def freq_seq(s, sep):
    pass

Solution

def freq_seq(s, sep):
    counter = {c:str(s.count(c)) for c in s}
    return f"{sep}".join([counter[c] for c in s])

See on CodeWars.com