You can download the sample data at http://www.pythonlearn.com/code/words.txt', "desired" => "WRITING PROGRAMS OR PROGRAMMING IS A VERY CREATIVE AND REWARDING ACTIVITY YOU CAN WRITE PROGRAMS FOR MANY REASONS RANGING FROM MAKING YOUR LIVING TO SOLVING A DIFFICULT DATA ANALYSIS PROBLEM TO HAVING FUN TO HELPING SOMEONE ELSE SOLVE A PROBLEM THIS BOOK ASSUMES THAT {\EM EVERYONE} NEEDS TO KNOW HOW TO PROGRAM AND THAT ONCE YOU KNOW HOW TO PROGRAM, YOU WILL FIGURE OUT WHAT YOU WANT TO DO WITH YOUR NEWFOUND SKILLS WE ARE SURROUNDED IN OUR DAILY LIVES WITH COMPUTERS RANGING FROM LAPTOPS TO CELL PHONES WE CAN THINK OF THESE COMPUTERS AS OUR PERSONAL ASSISTANTS WHO CAN TAKE CARE OF MANY THINGS ON OUR BEHALF THE HARDWARE IN OUR CURRENT-DAY COMPUTERS IS ESSENTIALLY BUILT TO CONTINUOUSLY AS US THE QUESTION WHAT WOULD YOU LIKE ME TO DO NEXT OUR COMPUTERS ARE FAST AND HAVE VASTS AMOUNTS OF MEMORY AND COULD BE VERY HELPFUL TO US IF WE ONLY KNEW THE LANGUAGE TO SPEAK TO EXPLAIN TO THE COMPUTER WHAT WE WOULD LIKE IT TO DO NEXT IF WE KNEW THIS LANGUAGE WE COULD TELL THE COMPUTER TO DO TASKS ON OUR BEHALF THAT WERE REPTITIVE INTERESTINGLY, THE KINDS OF THINGS COMPUTERS CAN DO BEST ARE OFTEN THE KINDS OF THINGS THAT WE HUMANS FIND BORING AND MIND-NUMBING", "code" => '# Use words.txt as the file name fname = raw_input("Enter file name: ") fh = open(fname) ', "xcode" => '# Use words.txt as the file name fname = raw_input("Enter file name: ") fh = open(fname) text = fh.read().strip() print text.upper() ', "checks" => Array( "raw_input" => "You must prompt for the file name using the raw_input() function.", "open" => "You need to use open() to open the file.", "print" => "You must use the print statement to print the lines.", "strip" => "You should use strip() or rstrip() to avoid double newlines. You may need to scroll down to see a mis-match of the output.", "upper" => "You should use the upper() function to convert the lines to upper case.") ), "7.2" => Array( "qtext" => '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.8475Count 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 not use the sum() function or a variable named sum in your solution.
You can download the sample data at http://www.pythonlearn.com/code/mbox-short.txt when you are testing below enter mbox-short.txt as the file name.', "desired" => "Average spam confidence: 0.750718518519", "code" => '# Use the file name mbox-short.txt as the file name fname = raw_input("Enter file name: ") fh = open(fname) for line in fh: if not line.startswith("X-DSPAM-Confidence:") : continue print line print "Done" ', "xcode" => '# Use the file name mbox-short.txt as the file name fname = raw_input("Enter file name: ") fh = open(fname) tot = 0.0 count = 0 for line in fh: if not line.startswith("X-DSPAM-Confidence:") : continue words = line.split() tot = tot + float(words[1]) count = count + 1 print "Average spam confidence:", tot/count ', "checks" => Array( "raw_input" => "You must prompt for the file name using the raw_input() function.", "open" => "You need to use open() to open the file.", "!sum" => "You should not use the sum() function and avoid using sum as a variable.", "float" => "You should use the float() function to convert from a string to an integer.", '!18518' => "You must actually pull the data from the strings and convert it.", "/" => "Average is usually a total / count.") ), "8.4" => Array( "qtext" => '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 the program completes, sort and print the resulting words in alphabetical order.
You can download the sample data at http://www.pythonlearn.com/code/romeo.txt', "desired" => "['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']", "code" => 'fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: print line.rstrip() ', "xcode" => 'fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: words = line.split() for word in words: if word in lst: continue lst.append(word) lst.sort() print lst ', "checks" => Array( "split" => "You should use split() to break each line into words.", "append" => "You should use append() to add the word to the list if it is not there.", "open" => "You need to use open() to open the file.", "sort" => "You need to use sort() to sort the list before you print it out.", "!'yonder'" => "You should not put the output data in strings", "for" => "You need two for loops. One for the lines and one for the words on each line.") ), "8.5" => Array( "qtext" => "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 2008You 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 the message). Then print out a count at the end.
Hint: make sure not to include the lines that start with 'From:'.". '
You can download the sample data at http://www.pythonlearn.com/code/mbox-short.txt', "desired" => "stephen.marquard@uct.ac.za louis@media.berkeley.edu zqian@umich.edu rjlowe@iupui.edu zqian@umich.edu rjlowe@iupui.edu cwen@iupui.edu cwen@iupui.edu gsilver@umich.edu gsilver@umich.edu zqian@umich.edu gsilver@umich.edu wagnermr@iupui.edu zqian@umich.edu antranig@caret.cam.ac.uk gopal.ramasammycook@gmail.com david.horwitz@uct.ac.za david.horwitz@uct.ac.za david.horwitz@uct.ac.za david.horwitz@uct.ac.za stephen.marquard@uct.ac.za louis@media.berkeley.edu louis@media.berkeley.edu ray@media.berkeley.edu cwen@iupui.edu cwen@iupui.edu cwen@iupui.edu There were 27 lines in the file with From as the first word", "code" => 'fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 print "There were", count, "lines in the file with From as the first word" ', "xcode" => 'fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: wds = line.split() if len(wds) < 2 : continue if wds[0] != "From" : continue print wds[1] count = count + 1 print "There were", count, "lines in the file with From as the first word" ', "checks" => Array( "for" => "You need a for loop to read the lines in the file.", "split" => "You should use split() to break each line into words.", "if" => "You need to use one or more if statements to skip the lines that do not start with 'From '.", "open" => "You need to use open() to open the file.") ), "9.4" => Array( "qtext" => "9.4 Write a program to read through the mbox-short.txt and figure out who has the 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 of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.", "desired" => "cwen@iupui.edu 5", "code" => 'name = raw_input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) ', "xcode" => 'name = raw_input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) counts = dict() for line in handle: wds = line.split() if len(wds) < 2 : continue if wds[0] != "From" : continue email = wds[1] counts[email] = counts.get(email,0) + 1 bigcount = None bigname = None for name,count in counts.items(): if bigname is None or count > bigcount: bigname = name bigcount = count print bigname, bigcount ', "checks" => Array( "for" => "You need a for loop to read the lines in the file.", "split" => "You should use split() to break each line into words.", "if" => "You need to use one or more if statements to skip the lines that do not start with 'From '.", "open" => "You need to use open() to open the file.") ), "10.2" => Array( "qtext" => "10.2 Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon.
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.", "desired" => "04 3 06 1 07 1 09 2 10 3 11 6 14 1 15 2 16 4 17 2 18 1 19 1", "code" => 'name = raw_input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) ', "xcode" => 'name = raw_input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) counts = dict() for line in handle: wds = line.split() if len(wds) < 5 : continue if wds[0] != "From" : continue when = wds[5] tics = when.split(":") if len(tics) != 3 : continue hour = tics[0] counts[hour] = counts.get(hour,0) + 1 lst = counts.items() lst.sort() for key, val in lst : print key, val ', "checks" => Array( "for" => "You need a for loop to read the lines in the file.", "sort" => "You need to use list sort() method to sort the list of times.") ), "11.1" => Array ( "qtext" => '11.1 Sadly, the autograder does not support the regular expression library. So please write a program that computes the Answer to the Ultimate Question of Life, the Universe, and Everything [more detail]. Sample output is below.', "desired" => "42", "code" => '', "checks" => Array( "print" => "By now you should know that a print statement would be helpful here.", "*" => "I think that multiplication is involved..." )), "11.9" => Array( "qtext" => "11.9 Write a program to prompt the user for a regular expression and read through the mbox-short.txt and count the number of lines that match the regular expression using re.search().", "desired" => "04 3 19 1", "code" => 'import re string = raw_input("Enter a regular expression:") if len(name) < 1 : name = "mbox-short.txt" handle = open("mbox-short.txt") count = 0 for line in handle: if re.search(string) : count = count + 1 print "mbox-short.txt had ", count, "lines that matched", string ', "xcode" => 'name = raw_input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) counts = dict() for line in handle: wds = line.split() if len(wds) < 5 : continue if wds[0] != "From" : continue when = wds[5] tics = when.split(":") if len(tics) != 3 : continue hour = tics[0] counts[hour] = counts.get(hour,0) + 1 lst = counts.items() lst.sort() for key, val in lst : print key, val ', "checks" => Array( "for" => "You need a for loop to read the lines in the file.", "sort" => "You need to use list sort() method to sort the list of times.") ) );