alphabet = "abcdefghijklmnopqrstuvwxyz" testdups =…
- User
alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]
From Section 11.2 of:
Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press.
def histogram(s): d = dict() for c in s: if c not in d: d[c] = 1 else: d[c] += 1 return d
- Assistant
The function
histogram(s)takes a stringsas input and returns a histogram of the characters in the string. The histogram is represented as a dictionary, where the keys are the unique characters in the string and the values are the frequencies of those characters.To create the histogram, the function iterates over each character in the string. If the character is not already in the dictionary
d, a new key-value pair is added with the character as the key and the value 1. If the character is already in the dictionary, the value for that key is incremented by 1.The function
histogram(s)can be used to count the number of occurrences of each character in a string.