Programming Exercise 3-1
Programming Exercise 9-5
def main():
# Set up empty dictionary
counter = {}
# Get input text
input_name = input(‘Enter the name of the input file: ‘)
input_file = open(input_name, ‘r’)
text = input_file.read()
words = text.split()
# Add each unique word to dictionary with a counter of 0
unique_words = set(words)
for word in unique_words:
counter[word] = 0
# For each word in the text increase its counter in the dictionary
for item in words:
counter[item] += 1
# Display results
print(format(‘word’, ’15’),’\t’,format(‘occurrences’,’15’))
print(‘———————————————-‘)
while len(counter)>0:
pair = counter.popitem()
print(format(pair[0],’15’), format(pair[1], ’15’))
# Call the main function.
main()