CS21 Lab3: Boolean and Strings

Due 11:59pm Tuesday night, September 23

Run update21, if you haven't already, to create the cs21/labs/03 directory. handin21 looks for your lab 3 assignments in the cs21/labs/03 directory, so make sure you create your python programs in there:

$ cd cs21/labs/03
$ vi grades.py

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

1. Grade Calculator

A professor uses the following scale to compute grades:

A: 92-100
B: 82-91
C: 72-81 
D: 60-71
F: below 60

Write a program that takes as input a numeric score and prints out its letter grade equivalent. Here are some sample runs of a working program:

$ python grades.py
This program computes a letter grade given a numeric score.
Enter the numeric score: 91 
The letter grade equivalent is: B

$ python grades.py
This program computes a letter grade given a numeric score.
Enter the numeric score: 63 
The letter grade equivalent is: D
2. Cyclic Cypher

Write a program, in a file named encode.py, that asks the user to enter a phrase and a cyclic shift value, and produces an encoded string by applying the cyclic cipher method to just the alphabetic characters in the phrase. Your solution should encode ONLY upper and lower case characters in the phrase.

A cyclic cipher, with a shift value of 3 will encode the character 'a' as the character 'd', the character 'b' as 'e', and so on (each character's encoding is shifted 3 characters to the right). A cyclic cipher wraps the encoding around the alphabetic characters, so that in this example, 'w' is encoded as 'z', 'x' is encoded as 'a', 'y' is encoded as 'b', and 'z' is encoded as 'c'. You can think of this as encoding where the alphabet is arranged in a circle, and the shift amount tells you how many places in one direction to move to find the encoding of any letter of the alphabet.

Here is what a few runs of your program might look like:

$ python encode.py
This program encodes a phrase using a cyclic cipher
Enter a phrase: Hello there, Zoe!  What is happening???
Enter a shift value: 4
The encoded phrase is: Lipps xlivi, Dsi!  Alex mw lettirmrk??? 

$ python encode.py
This program encodes a phrase using a cyclic cipher 
Enter a phrase: yee ha!!!
Enter a shift value: 1000
The encoded phrase is: kqq tm!!!
3. Pattern of Stars

Write a program that prints the two patterns of stars shown below. Your program should take input from the user specifying the size of the pattern to print. Here is what a few runs of your program might look like:

$ python stars.py
This program prints out two different patterns of stars.
Enter a value for the size of the pyramid of stars: 10

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

Enter a value for the size of the diamond:  4 

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

$ python stars.py
This program prints out two different patterns of stars.
Enter a value for the size of the pyramid of stars: 6

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

Enter a value for the size of the diamond:  9 

         *
        * *
       * * *
      * * * *
     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *
      * * * *
       * * *
        * *
         *
Extra Credit Challenge Problem

This is not part of the regular Lab 3 assignment, but if you love pattern of stars problems like I do, see if you can solve the following problem for a few extra credit points. Don't try this until you have completed all the regular problems for Lab 3.

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 

* * * * *   |   * * * * *
* * * *  ## | ##  * * * *
* * *  #### | ####  * * *
* *  ###### | ######  * *
*  ######## | ########  *
*  ######## | ########  *
* *  ###### | ######  * *
* * *  #### | ####  * * *
* * * *  ## | ##  * * * *
* * * * *   |   * * * * *
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.