CS21 Lab 2: Numbers and Strings

Due Saturday, Feb 8th 11:59pm

This lab assignment requires you to write three 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
We will only grade files submitted by handin21 in this labs directory, so make sure your programs are in this directory!

1. Average word length

In this problem you will calculate the average word length in a phrase entered by the user. Edit the file avgLength.py to create your solution. Your program should produce the following sort of interaction with the user:

$ python avgLength.py 
This program calculates the average word length in a phrase.

Enter a phrase: help is on the way
Average word length: 2.8

In the phrase above, the word lengths are 4, 2, 2, 3, 3, for a total of 14 and an average of 14 divided by 5 which is 2.8.



2. Box

Write a program, in a file named box.py, that asks the user to enter two characters (outline and fill) and two integers (width and height). Your program should then draw a box, using the characters (one as outline, the other to fill), in the terminal window, with the given width and height.

Here are two examples of the box.py program:

$ python box.py

This program draws a box. Please enter the characters
to use and the width and height of the box.

  Enter box character: $
 Enter fill character: -
  
  width: 20
 height: 10

 $$$$$$$$$$$$$$$$$$$$
 $------------------$
 $------------------$
 $------------------$
 $------------------$
 $------------------$
 $------------------$
 $------------------$
 $------------------$
 $$$$$$$$$$$$$$$$$$$$


$ python box.py

This program draws a box. Please enter the characters
to use and the width and height of the box.

  Enter box character: !
 Enter fill character: o
  
  width: 25
 height: 4

 !!!!!!!!!!!!!!!!!!!!!!!!!
 !ooooooooooooooooooooooo!
 !ooooooooooooooooooooooo!
 !!!!!!!!!!!!!!!!!!!!!!!!!

You may assume that the user enters reasonable values for the width and height of the box. Your program does not need to work correctly if the user enters a non-number or other invalid input.

Note/Hint: since the above boxes are being output to a computer terminal, they must be created one line at a time, from top to bottom. You can't print out the left side of the box, then go back up and print out the right side of the box.

3. More Patterns
(Special thanks to Tom Wexler at Oberlin College for the pattern examples provided here.)

In this section, you will actually write three small programs to solve three small problems. For each problem, a few patterns are given. Each pattern is comprised solely of numbers and spaces. The pattern that gets printed depends on the number that the user enters. Your job is to first determine the pattern that dictates how a particular figure in the sequence relates to the corresponding number, then write the program that generates the pattern shown.

Pattern A (patternA.py)

For the first pattern, pattern A, write a program called patternA.py. You should read an integer from the user and then print out the pattern.

$ python patternA.py
Enter a number to generate a cool pattern: 3
1 2 3
1 2 3
1 2 3

$ python patternA.py
Enter a number to generate a cool pattern: 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

$ python patternA.py
Enter a number to generate a cool pattern: 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

You should be able to deduce the pattern from the examples above. Your program will first get an integer from the user and then print the appropriate pattern.

Pattern B (patternB.py)

For the second pattern, pattern B, write a program called patternB.py. You should read an integer from the user and then print out the pattern.

$ python patternB.py
Enter a number to generate a cool pattern: 3
1 1 1
2 2 2
3 3 3

$ python patternB.py
Enter a number to generate a cool pattern: 4
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4 

$ python patternB.py
Enter a number to generate a cool pattern: 5
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

Pattern C (patternC.py)

For the last pattern, pattern C, write a program called patternC.py. You should read an integer from the user and then print out the pattern.

$ python patternC.py
Enter a number to generate a cool pattern: 3
1 2 3
2 3
3

$ python patternC.py
Enter a number to generate a cool pattern: 4
1 2 3 4
2 3 4
3 4
4

$ python patternC.py
Enter a number to generate a cool pattern: 5
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Hacker's Challenges

These patterns are an optional bonus problem. They are challenging and are not required, and you should not attempt them until you are completely finished with the other lab problems. It will also not get you any extra points -- you will only be rewarded with extra knowledge and satisfaction. :)

Pattern D (OPTIONAL)

$ python patternD.py
Enter a number to generate a cool pattern: 3
1 
1 2 2 
1 2 2 3 3 3

$ python patternD.py
Enter a number to generate a cool pattern: 4
1 
1 2 2 
1 2 2 3 3 3
1 2 2 3 3 3 4 4 4 4

$ python patternD.py
Enter a number to generate a cool pattern: 5
1 
1 2 2 
1 2 2 3 3 3
1 2 2 3 3 3 4 4 4 4
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Pattern N (OPTIONAL)

Draws a large capital N.

$ python patternN.py
Enter a number to generate a capital N: 2
*  *
** *
* **
*  *

$ python patternN.py
Enter a number to generate a capital N: 3
*   *
**  *
* * *
*  **
*   *

$ python patternN.py
Enter a number to generate a capital N: 4
*    *
**   *
* *  *
*  * *
*   **
*    *


Submit

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

You may run handin21 as many times as you like. We will grade the most recent submission submitted prior to the deadline

Remember: for this lab, programs must appear in 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:

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