Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def count_char(input):
- '''
- Counts the number of occurrences of each character in a string. The result should be a dictionary where the key is the character and the dictionary is its count.
- For example,
- >>> count_char("HelloOo!")
- {'H': 1, 'e': 1, 'l': 2, 'o': 2, 'O': 1, '!': 1}
- '''
- d = {}
- # d = {'H': 1
- for c in input:
- if c in d: # Is a key named 'H' inside the d?
- d[c] = d[c] + 1 #then count 'H'
- else:
- d[c] = 1 #if there's no 'H' then make the value as 1
- return d
- #pass
- """
- def test_count_char():
- assert count_char("HelloOo!") == {'H': 1, 'e': 1, 'l': 2, 'o': 2, 'O': 1, '!': 1}
- def test_empty():
- assert count_char("") == {}
- def test_simple():
- assert count_char("abc") == {"a": 1, "b": 1, "c": 1}
- def test_double():
- assert count_char("aa") == {"a": 2}
- def test_uppercase():
- assert count_char("aA") == {"a": 1, "A": 1}
- def test_space():
- assert count_char("Hi a") == {"H": 1, "i": 1, " ": 1, "a": 1}
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement