Skip to content

Latest commit

 

History

History
59 lines (34 loc) · 897 Bytes

Count_letters_in_string.md

File metadata and controls

59 lines (34 loc) · 897 Bytes

CodeWars Python Solutions


Count letters in string

In this kata, you've to count lowercase letters in a given string and return the letter count in a hash with 'letter' as key and count as 'value'. The key must be 'symbol' instead of string in Ruby and 'char' instead of string in Crystal.

Example

letter_count('arithmetics') #=> {"a": 1, "c": 1, "e": 1, "h": 1, "i": 2, "m": 1, "r": 1, "s": 1, "t": 2}

Given Code

def letter_count(s):
    pass

Solution 1

def letter_count(s):
    return {c: s.count(c) for c in s}

Solution 2

def letter_count(s):
    letters = {}
    for i in list(s):
        if i in letters:
            letters[i] += 1
        else:
            letters[i] = 1
    return letters

See on CodeWars.com