Examples
Letter Frequencies
An example using a dictionary to count the frequency of letters in a string.
def getLetterCounts(message):
"""Returns a dictionary with keys of single letters and values
of the count of how many times they appear in the message
parameter"""
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letterCount = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0,
'G': 0, 'H': 0, 'I': 0, 'J': 0, 'K': 0, 'L': 0,
'M': 0, 'N': 0, 'O': 0, 'P': 0, 'Q': 0, 'R': 0,
'S': 0, 'T': 0, 'U': 0, 'V': 0, 'W': 0, 'X': 0,
'Y': 0, 'Z': 0}
for letter in message.upper():
if letter in LETTERS:
letterCount[letter] += 1
return letterCount
In the next example, we build a function, countWords()
, that takes as input a text file, the function opens the file and reads it line by line counting the words that appear. Note the use of the .get()
method for incrementing the word count by 1 if the word is already in the dictionary, otherwise adding it to the dictionary with a value of 1. This is a very useful way to update/append items in a dictionary. The function returns a dictionary of word counts. During execution the punctuation is removed, and hyphenated words are split apart.
A second function is also defined, sortwords()
which takes the dictionary of word frequencies and returns a sorted list. (For example, use this text file: py-romeo-short.txt)
import string
def countWords(fname='py-romeo-short.txt'):
try:
fhand = open(fname)
except:
print('File cannot be opened:', fname)
counts = {}
for line in fhand:
line = line.rstrip()
# next replace hyphons with spaces to separate words
line = line.translate(str.maketrans('-',' '))
line = line.translate(str.maketrans('','',string.punctuation))
line = line.lower()
words = line.split()
for word in words:
# a dictionary trick to append/update in one line
counts[word] = counts.get(word,0) + 1
return(counts)
def sortWords(wordsDict,direction):
# direction: 0 = ascending, 1 = descending
lst = []
for key, val in list(wordsDict.items()):
lst.append((val,key))
lst.sort(reverse=direction)
return(lst)
Euclid's Algorithm
The next two functions give examples of mathematical related functions.
Euclid's algorithm for computing the greatest common divisor of two integers is thousands of years old. Notice the elegance in the implementation - no factoring of numbers is reuired. It exploits the fact that if \(d\) divides \(a\) and \(b\) then it must also divide the difference \(a-b\) and hence it must also divide the remainder of \(a\) divided by \(b\).
def gcd(a,b):
"""Return the GCD of a and b using Euclid's Algorithm."""
while b > 0:
a, b = b, a%b
return a
def xgcd(a,b):
"""Extended GCD:
Returns (gcd, x, y) where gcd is the greatest common divisor of
a and b with the sign of b if b is nonzero, and with the sign of
a if b is 0. The numbers x,y are such that gcd = ax+by."""
prevx, x = 1, 0; prevy, y = 0, 1
while b:
q, r = divmod(a,b)
x, prevx = prevx - q*x, x
y, prevy = prevy - q*y, y
a, b = b, r
return a, prevx, prevy