CS21 Lab2: Numbers and Strings

Due 11:59pm Tuesday, 15 September

Run update21, if you haven't already, to create the cs21/labs/02. Then cd into your cs21/labs/02 directory and create the python programs for lab 2 in this directory (handin21 looks for your lab 2 assignments in your cs21/labs/02 directory):


$ update21
$ cd cs21/labs/02
$ pwd
  /home/your_user_name/cs21/labs/02

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.

1. Computing volume and surface area of a cone

Write a program, in a file named cone.py, that asks the user to enter the value of the radius and the value of the hight of a cone. Your program will then compute and print out the volume and the surface area of the cone using these formulas:

Here is what two runs of your program might look like:


$ python cone.py

This program computes the volume and surface area of a cone
given values for the height and radius

Enter the radius value: 4
Enter the height of the cone:3

A cone with radius 4 and height 3
has a volume of  50.265
and a surface area of  62.832

$ python cone.py

Enter the radius value: 2.3
Enter the height of the cone:0.8

A cone with radius 2.3 and height 0.8
has a volume of  4.432
and a surface area of  17.596
Try using string formating to limit the number of places printed beyond the decimal point. Actually, don't worry about this. We haven't covered it yet.

To use math library functions, remember to add this to the top of your program:

from math import *


2. Computing the sum of the first n odd integers

Write a program named oddsum.py, that takes as input a positive integer value n, and then computes the sum of the first n odd integer values. For example, if the user enters 5, your program will compute the sum of 1, 3, 5, 7 and 9.

A couple runs of your program may look like this:


$ python oddsum.py
This program computes the sum of the first n odd integers
Enter a value a value for n: 5

the sum of odd integers between 1 and 10 is  25

$ python oddsum.py
This program computes the sum of the first n odd integers
Enter a value a value for n: 30

the sum of the odd integers between 1 and 60 is  900
It is easy to test your program for correctness because the sum of the first n odd integers is equal to n squared. However, your program should not just compute n squared. Instead, you must use a loop to compute the sum using successive additions.

3. Reverse a phrase
Write a program in a file named reverse.py, that prompts the user to enter a phrase, and then creates a new string consisting of the phrase in reverse. The reversed phrase is then printed out. Your program must use a loop. Do not use built-in short cuts.
$ python reverse.py
This program produces the reverse of a string entered by the user
Enter a phrase: hello there
The phrase reversed is  ereht olleh

$ python reverse.py
This program produces the reverse of a string entered by the user
Enter a phrase: a man a plan a canal panama
The phrase reversed is amanap lanac a nalp a nam a


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

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


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


Extra Challenge
This is not a required part of the lab 2 assignment, but is an extra challenge that you can try. Only try this after you have completed all the regular problems in lab 2.

Write a program, morestars.py, that prints out the following patterns of stars given user input values for the size of the patterns:

This program prints out a pyramid of stars
Enter a value of for the size of the pyramid: 8

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

This program prints out a diamond of stars
Enter a value of for the size of the diamond: 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.