10.10. Write Code Questions

  1. Write a program that categorizes each mail message by which day of the week the commit was done. To do this, look for lines that start with “From”. Then, look for the third word, and keep a running count of each of the days of the week. At the end of the program, print out the contents of the dictionary mail_count (order does not matter). For example, mail_count['Mon'] should be 2.

    Write a program that categorizes each mail message by which day of the week the commit was done. To do this, look for lines that start with “From”. Then, look for the third word, and keep a running count of each of the days of the week. At the end of the program, print out the contents of the dictionary mail_count (order does not matter). For example, mail_count['Mon'] should be 2.

  2. Write a program to read through a mail log, build the dictionary user_count to count how many messages have come from each email address, and print the dictionary. For example, user_count['stephen.marquard@uct.ac.za'] should be 4.

  3. Write a program that creates a dictionary letter_count that keeps track of the amount of times each letter appears in the given phrase. Assign the number of times “e” appears in the phrase to the variable e_counter. Make sure to account for each letter in its lowercase form. For example, e_counter should be 10, and letter_count['e'] should be 10.

    Write a program that creates a dictionary letter_count that keeps track of the amount of times each letter appears in the given phrase. Assign the number of times “e” appears in the phrase to the variable e_counter. Make sure to account for each letter in its lowercase form. For example, e_counter should be 10, and letter_count['e'] should be 10.

  4. Write a program that reads the words in the string phrase and counts how many times each word appears. Store the words as keys in the dictionary word_dictionary, and then use the in operator as a fast way to check whether the string is in the dictionary. For example, word_dictionary['Writing'] should be 1. (Note: ‘Writing’ and ‘writing’ would be counted as two separate words for this question.)

  1. Write code that reads in the text from the file words.txt and uses the dictionary word_count to count the amount of times a word appears in the file. Watch out for repetition using the .lower() function. For example, word_count['and'] should be 5.

    Write code that reads in the text from the file words.txt and uses the dictionary word_count to count the amount of times a word appears in the file. Watch out for repetition using the .lower() function. For example, word_count['and'] should be 5.

  2. Write a program that reads the words in the string phrase and counts how many times each word appears. Store the words as keys in the dictionary word_dictionary, and then use the in operator as a fast way to check whether the string is in the dictionary. Make sure to turn all letters in words into lowercase letters in order to avoid any repetition. For example, word_dictionary.get('Writing', 0) should be 0, and word_dictionary['writing'] should be 1.

Below is the romeo3.txt file used in Question 7.

  1. Write code to read through the lines of the file, break each line into a list of words, and then loop through each of the words in the line and count each word using the dictionary counts. For example, counts['is'] should be 3.

    Write code to read through the lines of the file, break each line into a list of words, and then loop through each of the words in the line and count each word using the dictionary counts. For example, counts['is'] should be 3.

  2. Write code that adds the key ‘two’ with a value of ‘dos’ to the dictionary eng2sp. For example, eng2sp['two'] should be 'dos'.

The next two questions are associated with the following text file which has an email address followed by the number of messages from that address.

  1. Add code to the program below to figure out who has the most messages in the file. After all the data has been read and the dictionary message_count has been created, look through the dictionary using a maximum loop (see Chapter 5: Maximum and minimum loops) to find who has the most messages, and print how many messages the person has. For example, message_count['cwen@iupui.edu'] should be 5.

    Add code to the program below to figure out who has the most messages in the file. After all the data has been read and the dictionary message_count has been created, look through the dictionary using a maximum loop (see Chapter 5: Maximum and minimum loops) to find who has the most messages, and print how many messages the person has. For example, message_count['cwen@iupui.edu'] should be 5.

  2. Write a program to record in the dictionary message_count the total number of messages from each domain name (not the whole address, just the part after the @ and before the space). At the end of the program, print out the contents of your dictionary. The domains should be the keys of the dictionary, and the counts of the domains should be the values of the dictionary. For example, message_count['iupui.edu'] should be 8.

You have attempted of activities on this page