P. 1
39-section-handout

|Views: 6|Likes:

See more
See less

05/10/2014

pdf

text

original

# CS107 Spring 2008

Handout 39 June 2, 2008

Section Handout: Python
Problem 1: Jane Austen’s Favorite Word Project Gutenberg is an open-source effort intended to legally distribute electronic copies of thousands of books no longer under copyright. So, if you’re tired of coding in spite of the strong Internet connection, you might download yourself a little Franz Kafka by visiting: http://www.gutenberg.org/files/5200/5200.txt and e-flip through Metamorphosis to pass the time. A quick scan of www.gutenberg.org reveals that virtually all of Jane Austen’s novels—Emma, Lady Susan, Love and Friendship, Northanger Abbey, Persuasion, Price and Prejudice, and Sense and Sensibility—are available in plain text format. Naturally, this prompts you to wonder what everyone else is wondering: What’s Jane Austen’s favorite word? Given an array of URLs, write a Python function getFavoriteWord that reads in all of the plain text documents addressed by the URLs, compiles a huge frequency map of all words she uses anywhere in any of the texts, and returns the most commonly occurring word. Only count a word if it’s all lowercase letters, and assume the favorite word is the one with the highest score, where the score of a word is equal to freqlen, where freq is the word’s frequency and len is the word’s length. For simplicity, assume all punctuation marks are separated from real words by whitespace. See the last page of the section handout for a list of all dictionary methods. (You’ll want to focus on the get and operator[] methods.)
# # Pulls all of the contents from each of the URLs in the 'urls' list, # and returns what appears to be the favorite word, where the # metric for what favorite means is outlined above # def getFavoriteWord(urls):

2 Problem 2: Maximizing Points You’re given an unlimited number of pebbles to distribute across an N x N game board (N drawn from [3, 15]), where each square on the board contains some positive point value between 10 and 99, inclusive. A 6 x 6 board might look like this:

33 67 44 12 81 38

74 56 64 32 65 78

26 91 22 76 56 50

55 72 91 50 38 92

79 44 29 50 96 90

54 32 61 32 36 75

The player distributes pebbles across the board so that: • • At most one pebble resides in any given square. No two pebbles are placed on adjacent squares. Two squares are considered adjacent if they are horizontal, vertical, or even diagonal neighbors. There’s no board wrap, so 44 and 61 aren’t neighbors. Neither are 33 and 75.

The goal is to maximize the number of points claimed by your placement of pebbles. Write a program that reads in a sequence of boards from standard input and posts the maximum number of points attainable by an optimal pebble placement for each. Each board is expressed as a series of lines, where each line is a space-delimited series of numbers. A blank line marks the end of each board (including the last one.) The better solution to this problem uses memoization to reduce the running time of the solution from something very exponential in running time to something noticeably less exponential (though still exponential.) Don’t worry too much about the file reading portion of the solution—focus instead on the ideal distribution of pebbles and the Python code that helps to discover it. (See the last page of this handout for a list of all dictionary and mutable sequence methods.)

3 So, if the following were fed to standard input:
71 85 92 23 64 78 98 12 13 85 40 33 80 49 36 77 33 75 29 62 61 45 15 94 85 84 18 26 34 29 66 49 54 31 24 50 96 61 33 78 54 15 79 59 63 49 21 66 53 27 85 88 34 57 35 46 95 15 12 61 72 94 84 80 64 15 91 62 95 74 23 31 32 11 81 79 83 61 97 78 32 33 35 25 33 10 43 91 36 89 24 84 43 77 97 78 53 14 88 59 47 32 56 94 71 30 95 55 43 99 65 12 85 79 71 19 33 42 59 57 46 85 20 53 35 42 39 96 94 87 98 79 82 21 29 97 54 28 10 46 89 20 39 58 34 58 66 30 89 45 88 70 47 85 75 89 38 83 79 88 41 49 43 78 83 15 21 37 38 42 11 97 10 17 97 90 16 63 61 90 36 46 71 28 70 18 59 35 83 23 38 18 92 49 18 56 27 67 93 34 39 31 19 35 63 34 47 80 89 48 55 64 35 75 59 59 60 94 16 78 52 43 88 52 29 23 91 75 10 63 30 64 45 66 50 97 95 78 83 11 39 41 41 92 79 54 90 84 76 17 98 59 48 19 63 87 91 22 54 16 33 26 55 69 57 38 81 88 39 14 98 23 79 96 84 16 38 88 98 95 99 98 71 80 88 17 24 74 82 99 44 26 89 58 76 43 55 45 19 14 83 21 86 13 18 22 68 91 51 93 51 19 11 54 87 32 85 14 59 91 75 41 79 62 27 48

then your program would print the maximum number of points one can get by optimally distributing pebbles while respecting the two rules, which would be this:
572 683 2096 2755

4 Python mutable sequence API

Python dictionary API

The above tables were lifted from http://docs.python.org/lib/typesseqmutable.html and http://docs.python.org/lib/typesmapping.html .

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->