CS21 Lab3: Boolean and Strings

Due 11:59pm Tuesday night, September 20

Run update21, if you haven't already, to create 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):

$ update21
$ 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

After grading your final exam results, I need to convert the number scores into letter grades. Let's assume I use the following scale to compute grades:

A: 93-100
B: 81-92
C: 72-80 
D: 59-71
F: below 59

Write a program in a file named grades.py that takes as input a numeric score and prints out the 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: 96 
The letter grade equivalent is: A

$ python grades.py

This program computes a letter grade given a numeric score
Enter the numeric score: 72 
The letter grade equivalent is: C

$ python grades.py

This program computes a letter grade given a numeric score
Enter the numeric score: 50 
The letter grade equivalent is: F
2. Leap Year

A typical calendar year contains 365 days. Earth, however, takes slighly longer than 365 days to complete an orbit around the sun. To account for this difference in a calendar year and an astronomical year, we periodically add a day to a year on February 29. Years with this added day are called leap years.

Write a program called leapYear.py that takes as input a year and outputs whether that year is a leap year or not. A leap year typically occurs every 4 years (2004 and 108 for example, but not 1991). The exception is that years divisible by 100 must also be divisible by 400 to be leap years. That is, a year divisilble by 100 is not a leap year unless it is also divisible by 400. For example, 1700, 1800, and 1900 are not leap years but 2000 is a leap year.

Hint:

A sample run of your program should look similar to the following:
$ python leapYear.py

This program calculates if a provided year is a leap year.
Enter a year: 2011
2011 is NOT a leap year

$ python leapYear.py

This program calculates if a provided year is a leap year.
Enter a year: 1908
1908 IS a leap year

$ python leapYear.py

This program calculates if a provided year is a leap year.
Enter a year: 2100
2100 is NOT a leap year


3. Primes
A prime number is a natural number whose only divisors are 1 and itself. That is, dividing a prime number N by any integer between 2 and N-1 will not produce an integer. Prime numbers are very important in mathematics and computer science. In fact, the study of prime numbers plays an important part in cryptography. Write a program, prime.py, that takes a number as input from the user and outputs whether the number is prime. A sample run of this program will look like:
$ python prime.py

This program calculates if a number is prime.
Enter a number: 5
5 is a prime number

$ python prime.py

This program calculates if a number is prime.
Enter a number: 10
10 is not a prime number


4. Pattern of Chars
Write a program that prints the two patterns of characters shown below: in inverse pyramid and a diamond. Your program should take input from the user specifying the character to place in the pattern as well as the size of the pattern to print. Here is what a few runs of your program might look like:
$ python pattern.py

This program prints out two different patterns of chars.

Enter a character: $

Enter a value for the size of the pyramid of chars: 10

 $ $ $ $ $ $ $ $ $ $
  $ $ $ $ $ $ $ $ $
   $ $ $ $ $ $ $ $
    $ $ $ $ $ $ $
     $ $ $ $ $ $
      $ $ $ $ $
       $ $ $ $
        $ $ $
         $ $
          $

Enter a value for the size of the diamond:  4 

    $
   $ $
  $ $ $
 $ $ $ $
  $ $ $
   $ $
    $


$ python pattern.py

This program prints out two different patterns of chars.

Enter a character: *

Enter a value for the size of the pyramid of chars: 6

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

Enter a value for the size of the diamond:  9 


         *
        * *
       * * *
      * * * *
     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *
      * * * *
       * * *
        * *
         *
Note: there is one space between the characters in these patterns

Extra Challenge Problem #1
Note: Extra Challenge Problems are not a part of the graded Lab 3 assignment, and are not required. Please do not attempt these problems until you have completed Lab 3. Feel free to hand them in, but again, they are not graded. Primes are really cool so try out this problem if you want to understand them better. Do not try this until you have completed the regular problems in Lab 3. Write a program named findPrimes.py that searches all numbers from 1 to 1000 and prints only the prime numbers. The initial output of your program should look like this:
$ python findPrimes.py 
This program prints out all primes from 1 to 1000.

Prime numbers:
1
2
3
5
and so on.
Extra Challenge Problem #2
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.