CS21 Lab 2: Numbers and Strings

Due by 11:59pm Tuesday, Sep 18, 2012

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
/home/your_user_name/cs21/labs/02
The program handin21 will only submit files in this labs directory, so make sure your programs are in this directory!

1. Double Speak
Write a program that prompts the user for a phrase and then constructs a new phrase with each character of the phrase repeated twice. Examples are shown below

$ python double.py

This program doubles every character in a string entered by a user
Enter a phrase: woot

The doubled phrase is: 
wwoooott

$ python double.py

This program doubles every character in a string entered by a user
Enter a phrase: honey badger

The doubled phrase is: 
hhoonneeyy  bbaaddggeerr

2. Standard Deviation
Write a program that prompts a user to enter n values and computes the average and standard deviation of the values. Your program should first prompt the user for the number of values he/she plans to enter.

We discussed how to compute the average in class. To compute standard deviation, use the following outline

  1. sum the n values.
  2. sum the squares of the n values.
  3. divide the result in step 1. by n. This is the average
  4. divide the result in step 2. by n and subtract the square of the average from step 3.
  5. take the square root of the result in step 4. This is the standard deviation.

Remember to add

from math import *
near the top of your program to get access to the
sqrt
function.
$ python stddev.py 
Enter the number of values you wish to analyze: 3 
Enter a number: 2 
Enter a number: 2  
Enter a number: 2 
Average:  2.0
Standard Deviation:  0.0

$ python stddev.py 
Enter the number of values you wish to analyze: 3 
Enter a number: 1 
Enter a number: 2  
Enter a number: 3 
Average:  2.0
Standard Deviation: 0.816496580928
As an aside, the following is a bit of a tongue twister, but is a valid definition of the standard deviation: The square of the standard deviation is the average of the squares minus the square of the average.
3. Clunky Pig Latin
In standard Pig Latin, new words are created from valid English words by moving the first consonant sound of the original word to the end, and adding "ay". For example, "school" becomes "oolschay". We don't quite have the tools to identify the first consonant sound, but we can write a clunky version of Pig Latin using string slicing and string addition/concatenation. In this version, prompt the user to enter a single word. Then form a new word by taking the first letter of the original word, moving it to the end, and adding "ay". Thus "school" becomes "choolsay". Some more examples are below.
$ python piglatin.py
Enter a word: swarthmore
In clunky pig latin, that word is warthmoresay


$ python piglatin.py
Enter a word: corgi
In clunky pig latin, that word is orgicay


4. Pattern of Stars
Write a program in a file named stars.py, that prompts the user to enter a value for n, and the prints out n rows of a pattern of stars that looks like the following examples:
$ python stars.py
This program prints out a pattern of stars
Enter a value for the size of the pattern: 5

- - - - -  * 
- - - -  * * 
- - -  * * * 
- -  * * * * 
-  * * * * * 


$ python stars.py
This program prints out a pattern of stars
Enter a value for the size of the pattern: 8

- - - - - - - -  * 
- - - - - - -  * * 
- - - - - -  * * * 
- - - - -  * * * * 
- - - -  * * * * * 
- - -  * * * * * * 
- -  * * * * * * * 
-  * * * * * * * * 
Hint: start by writing a program that prints out these two patterns of stars, then think about how you can use these solutions to lead you to solving the above problem:
This program prints out a pattern of stars
Enter a value for the size of the pattern: 5

* 
* * 
* * * 
* * * * 
* * * * * 


* * * * * 
* * * * 
* * * 
* * 
* 
Submit

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
Extra Challenge Problem

This is not part of the lab assignment, but just a fun extra problem to challenge you. Please don't try this until you have completed all of the regular problems for this lab. And it's just a fun exercise -- no bonus points or extra credit. :(

Write a program that takes as input the size of the pattern to print, and prints the following box of stars:

$ python starbox.py 
This program prints out a box of stars.
Enter a value for the size of the box: 8 

* * * * * * * *   |   * * * * * * * *
* * * * * * *  ## | ##  * * * * * * *
* * * * * *  #### | ####  * * * * * *
* * * * *  ###### | ######  * * * * *
* * * *  ######## | ########  * * * *
* * *  ########## | ##########  * * *
* *  ############ | ############  * *
*  ############## | ##############  *
*  ############## | ##############  *
* *  ############ | ############  * *
* * *  ########## | ##########  * * *
* * * *  ######## | ########  * * * *
* * * * *  ###### | ######  * * * * *
* * * * * *  #### | ####  * * * * * *
* * * * * * *  ## | ##  * * * * * * *
* * * * * * * *   |   * * * * * * * *

$ python starbox.py 
This program prints out a box of stars.
Enter a value for the size of the box: 5 

* * * * *   |   * * * * *
* * * *  ## | ##  * * * *
* * *  #### | ####  * * *
* *  ###### | ######  * *
*  ######## | ########  *
*  ######## | ########  *
* *  ###### | ######  * *
* * *  #### | ####  * * *
* * * *  ## | ##  * * * *
* * * * *   |   * * * * *