Exercise 9.4 Python For Everybody

9.4 Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for ‘From ‘ lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender’s mail address to a count…

Exercise 8.5 Python For Everybody

8.5 Open the file mbox-short.txt and read it line by line. When you find a line that starts with ‘From ‘ like the following line: From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent…

Exercise 8.4 Python For Everybody

8.4 Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When…

Exercise 7.2 Python For Everybody

7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: X-DSPAM-Confidence: 0.8475 Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do…

Exercise 7.1 Python For Everybody

7.1 Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the file words.txt to produce the output below. You can download the sample data at http://www.py4e.com/code3/words.txt # Use words.txt as the file name fname = input(«Enter file name: «)…

Exercise 6.5 Python For Everybody

6.5 Write code using find() and string slicing (see section 6.10) to extract the number at the end of the line below. Convert the extracted value to a floating point number and print it out. Solution:  text = «X-DSPAM-Confidence: 0.8475» pos = text.find(‘0’) word = (text[pos:pos+6]) fword = float(word) print(fword)    

Exercise 4.6 Python For Everybody

4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function…