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/02The program handin21 will only submit files in this labs directory, so make sure your programs are in this directory!
$ 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
We discussed how to compute the average in class. To compute standard deviation, use the following outline
Remember to add
from math import *near the top of your program to get access to the
sqrtfunction.
$ 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.816496580928As 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.
$ 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
$ 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
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
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
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 * * * * * | * * * * * * * * * ## | ## * * * * * * * #### | #### * * * * * ###### | ###### * * * ######## | ######## * * ######## | ######## * * * ###### | ###### * * * * * #### | #### * * * * * * * ## | ## * * * * * * * * * | * * * * *