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. Then cd into your cs21/labs/03 directory and create the python programs for lab 3 in this directory (handin21 looks for your lab 3 assignments in your cs21/labs/03 directory):

$ cd cs21/labs/03
$ pwd
  /home/your_user_name/cs21/labs/03

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

This program computes a letter grade given a numeric score
Enter the numeric score: 63 
The letter grade equivalent is: D

This program computes a letter grade given a numeric score
Enter the numeric score: 92 
The letter grade equivalent is: A
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!!!
Hints for solving: I'd suggest approaching this problem in the following way:
  1. first, write the program so that the user enters a phrase, and your program constructs a new string consisting of all the characters in the original phrase where every alphabetic character is replaced with the character 'x' (all other non-alphabetic characters stay the same in the new string). For example, if the user enters the phrase "To be, or not to be." your program will produce a new string "xx xx, xx xxx xx xx.".

  2. once that works, next see if you can produce an encoded string where the alphabetic charcters are encoded by doing only the shift part of the cyclic cypher (don't do the cyclic, wrap-around, part of the encoding yet). For example, a shift value of 4 will encode 'a' as 'e', 'b' as 'f', ..., 'u' as 'y', 'v' as 'z', 'w' as whatever the character after 'z' is in the ascii encoding, 'x' as whatever the character is two beyond 'z' in the ascii encoding, ...

  3. once that works, add in code to do the cyclic part of the shift. For example, a shift value of 4 will encode 'a' as 'e', 'b' as 'f', ..., 'v' as 'z', 'w' as 'a', 'x' as 'b', ...


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.