CS21 Lab 2: Strings

Due by 11:59pm Friday, Feb 8, 2013

This lab assignment requires you to write four programs in python. First, run update21. This will create the cs21/labs/02 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/02 directory and begin working on the python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ update21
$ cd cs21/labs/02
$ pwd
The program handin21 will only submit files in this labs directory, so make sure your programs are in this directory!

1. n letters
Write a program that prompts the user for a phrase and an integer (n), then constructs a new phrase with each character of the phrase repeated n times. Examples are shown below.

$ python nletter.py

input text: this is fun!!
         n: 4

tttthhhhiiiissss    iiiissss    ffffuuuunnnn!!!!!!!!

$ python nletter.py

input text: SWARTHMORE
         n: 6


2. boxed text
Write a program that prompts the user for a phrase and a character, then displays the phrase surrounded by a box of that character. Examples are shown below.
$ python boxtext.py 
input text: Happy Birthday!
      char: $

$ Happy Birthday! $

$ python boxtext.py 
input text: we love computer science!!!
      char: *

* we love computer science!!! *

3. deletions

In programs that check your spelling, there is often a function that, given a word, looks up all possible single-letter deletions. For example, given then word 'helpo', all single-letter deletions would be: elpo (deleted the first letter), hlpo (deleted the second letter), hepo, helo, and help (deleted the last letter). Such a function would then go on to check if any of these were valid English words.

Write a program to ask the user for a word and then generate and display all of the single-letter deletions. Examples are shown below.

$ python deletions.py

Input WORD: helpo

Here's that word with all possible single-letter deletions: 
 0: elpo
 1: hlpo
 2: hepo
 3: helo
 4: help

$ python deletions.py

Input WORD: 123456789

Here's that word with all possible single-letter deletions: 
 0: 23456789
 1: 13456789
 2: 12456789
 3: 12356789
 4: 12346789
 5: 12345789
 6: 12345689
 7: 12345679
 8: 12345678

4. pyramid text
Write a program that asks the user for some text and then displays that text in a pyramid-like pattern (see below). Don't worry too much about finding the exact middle of the given text if given an even number of characters....
$ python pyramid-text.py
input text: we love computer science
        computer s
      e computer sc
     ve computer sci
    ove computer scie
   love computer scien
   love computer scienc
 e love computer science
we love computer science

$ python pyramid-text.py 
input text: 12345

$ python pyramid-text.py 
input text: 123456


Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt.

You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize, after handing in some programs, that you'd like to make a few more changes to them.

Remember: for this lab, handin21 will only submit files from your cs21/labs/02 directory. If you create your programs in a different directory, use the unix mv or cp commands to move or copy them into the cs21/labs/02 directory. For example:

 cp  myprog.py  ~/cs21/labs/02/myprog.py