Assignment 1: Python Data Objects and Control Structures
CSE 341: Programming Languages
The University of Washington, Seattle, Winter 2012
Reading: The reading for this assignment is Chapters 1-4 in Python as a Second Language.
Purposes: The purposes of this assignment are to help you get started with Python, learn the basics of the language, and explore its facilities for working with numbers, strings, lists, dictionaries and basic control structures.
Due: Friday, January 13, at 5:00 PM via Catalyst CollectIt . (Although the due date is the 13th, a better target date for turning the assignment in is Wednesday, January 11, not only because Assignment 2 will be coming out then, but leaving the turn-in until Friday the thirteenth may be inviting bad luck! And besides, you wouldn't want to have to use one of your precious late days on the first, and probably easiest assignment of the course!)
What to Turn In: You should turn in the following files:
Each file should begin with a multiline string that serves as a comment and that gives the name of the file, a program name (that is more descriptive and English-like than the filename), the author's (your) name, followed by a brief description (2-5 lines) of what the program does.
  1. Numbers:
    a. Write a program that will accept as input a string representing a nonnegative integer in binary and output the number in decimal. It is fine to use the built-in function for converting a string to and integer here.
    Input a binary number: 100111
    In decimal, that comes to 39
    b. Write a program that will first prompt for and accept a string representing an integer (in decimal) greater than or equal to 1 and less than or equal to 36 then prompt for and accept a string representing a nonnegative integer in the base given. It will then print out the value in decimal. It should print an error message if the strings given are not compatible and in that case it should then should repeat the prompts. Your code should perform the conversion operation without resorting to any library routine for the conversion.
    Input the radix that you intend to use: 5
    Now input a number in base 5:  406
    That is not a valid number in base 5.
    Now input a number in base 5:  403
    In decimal, that comes to 103
  2. Lists and numbers:
    a. Write a program that inputs a number from the user and prints out a list of the squares of all prime numbers less than or equal to the number. If the input is not a number, your program can throw an exception. If the number is less than 2, then the output should be the empty list []. Here's an example:
    Please input an integer: 23
    [4, 9, 25, 49, 121, 169, 289, 361, 529]
    Use the following constructs in your implementation:
    -- a user-defined function called isPrime that itself includes a while loop or a for loop.
    -- a list comprehension.

  4. Strings:
    Write a program that runs its own read-eval-print loop in which evaluation means translating into Pig Latin. You should implement the translation using string methods. (Do not use regular expressions in this assignment.) Here is an Example:
    Enter your English phrase or "bye": I love languages.
    Pig Latin: Iway ovelay anguageslay.
    Enter your English phrase or "bye": except Pig Latin.
    Pig Latin: exceptway Igpay Atinlay.
    Enter your English phrase or "bye": bye.
    eyebay, eyebay!
    The example illustrates the way to handle words with capital letters. Words with leading capitals should still have leading capitals after translation. Any other capital letters should be carried through translation. If an input word is entirely capitalized, then its translation should also be entirely capitalized, except if it is the word "I".

    The rules for translating to Pig Latin are as follows. If the word begins with a vowel, simply append "way" to it. If the word starts with a consonant, pair of consonants or three consonants (for now let's call the longest consonant prefix of the word *), remove * from the beginning, putting it at the end, and then append "ay". For example "straight" becomes "aightstray". A word like "bye" is a special case, because here "ye" functions as a vowel, and yet the translation to "yebay" would not sound right when read by an untrained reader. You do not have to handle special cases such as this.

  5. Dictionaries, Strings, and Lists of Numbers:
    Write a program that reads in a text file (containing English text such as Lincoln's Gettysburg address) and outputs a sorted index of the words that occur in the text and what lines they occur in. For example, if the input file is as follows, then the index starts as shown after that. All words are converted to lower case as they are indexed. Non-letter characters such as whitespace, newlines, punctuation and digits are treated as separators. Multiple occurrences of the same word have their index entries combined to form a sorted, comma-separated sequence of line numbers. If two or more occurrences of the same word occur on the same line, that line number should occur only once in the word's entry.
      Four score and
    seven years ago,
    our fathers
    brought forth on this continent, a new nation
    conceived in liberty, and dedicated to the
    proposition that all men are created equal.
    a: 4
    ago: 2
    all: 6
    and: 1, 5
    are: 6
    brought: 4
    conceived: 5
    continent: 4
    created: 6
    dedicated: 5
    equal: 6
    fathers: 3
    forth: 4
    four: 1
    Your program should start by prompting the user for the name of the file to be read in. If it cannot open the file, it should report that politely and then quit. You should use the following constructs in implementing this program:
    --a dictionary whose keys are words.
    --the try-except construct.
    --lists of line numbers.
    --the sort method for lists.
Last updated: January 9 at 10:12 PM. Changed "page numbers" to "line numbers" in Problem 4. On January 6 at 4:45 PM, fixed "forth" and "four" being out-of-order in Problem 4. Also, at 4:13 PM, added exception to the all-caps rule in Problem 3 for the word "I" translating to Iway rather than IWAY.