Docsity

Section Handout: Python - Programming Paradigms - 39, Exercises of Programming Paradigms

Toggle sidebar
bg1
CS107 Handout 39
Spring 2008 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):
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Section Handout: Python - Programming Paradigms - 39 and more Exercises Programming Paradigms in PDF only on Docsity!

CS107 Handout 39 Spring 2008 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):

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:

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.)

Python mutable sequence API

Python dictionary API

The above tables were lifted from http://docs.python.org/lib/typesseq-

mutable.html and http://docs.python.org/lib/typesmapping.html.

CS107 Handout 39S Spring 2008 June 2, 2008

Section Solution: Python

Solution 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.)

Sample Solution

#!/usr/bin/env python

from urllib2 import urlopen

The urlopen function (in the urllib2 module--they tried

real hard to come up with a good name from the second iteration

of the urllib module, eh?) takes a url string (like "http://www.google.com")

and returns a file object with some address attributes (like the 'code'

attribute, which is the response code of the HTTP request).

Note the use of the map built-in, and the use of a lambda function to

generates a new sequence which is identical to the old one, save that

all of the "\n" have been chomped off (That's what the rstrip--short

for reverse-strip--method takes care of)

def pullLines(url): instream = urlopen(url)

Solution 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:

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.)

So, if the following were fed to standard input:

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: