Patrice Koehl
Department of Computer Science
Genome Center
Room 4319, Genome Center, GBSF
451 East Health Sciences Drive
University of California
Davis, CA 95616
Phone: (530) 754 5121
koehl@cs.ucdavis.edu




Introduction to Computers: Fall 2013

Lab8: Programming with Python (II)

This is your second programming assignment!

Obviously, you will program using Python...

Step 1: Getting ready

Please use IDLE as your interface to using Python. Create a file in which you will save your program. To do this using IDLE:

  • Step 1: In the menu File, choose "New Window". IDLE will then create a new window named "Untitled"
  • Step 2: Again in the menu File, choose "Save as..." and give a name to the file: make sure you add the extension .py;
    for example, you can choose the name: lab7.py.
    With the extension .py, IDLE will recognize the file as containing a Python program, and will color it properly.

Now you are ready to write your program!

Step 2: Generate proper header

It is always worth documenting your program as much as you can!
For example, it is important that the file contains the name of the programmer, as well as the date the file was created.

In your file, add the following lines at the top:

#
#   Author:          Your name
#   Student ID:      Your student ID
#   Date:            Today's date
#

where obviously, "Your name", "Your student ID" and "Today's date" refer to you and the date you are working on this program!

Step 3: Programming

You will now solve three small programming exercises; put your solutions to all problems in the file you are working on, one below the other.

Remember to test your program!

Problem 1: Splicing strings

Write a Python program that reads from standard input a long string S and outputs four new strings:

  • S2 only contains the last two letters of S
  • S3 only contains the first two letters of S
  • S4 only contains the 2nd, 4th, and 6th letter of S
  • S5 contains the first three AND last three letters of S
You will always assume that S contains more than 6 letters.

Problem 2: Encryption

Write a Python program that reads in a string S and an integer number N and encripts S based on N

The process of encryption is
  • Step 1: reverse the first N characters of S
  • Step 2: reverse the last N characters of S

Note that it is important that the two steps are performed in that order. We will assume that N is smaller than the length of the string S.

Examples of encryption:
  • The string "This is a test" with the key N=4 is encrypted to "sihT is a tset"
  • The string "Another example" with the key N=2 is ecnrypted to "nAother exampel"

Problem 3: Computing change

Write a Python program that reads in a change amount (assumed to be between 0 and 99 cents) and computes the minimal number of coins needed to tend this change.

For example, if the change amounts is 82 cents, the output of your program should look like:

Total change: 82 cents

3 quarter(s)
0 dime(s)
1 nickel(s)
2 cent(s)

Step 4: File submission

Once you are sure your program is working, submit your file on Smartsite.

Please submit your report as a single document (you can name this document as you want, but do keep the .py extension).

It is very important to turn in your assignment. If you do not turn in, you will not get your credit.
USE SMARTSITE to save your assignment. If you are still not sure how to do it, ask a TA for help.

Grading:


The maximum score you can get is 50. The breakdown is:


Overall presentation Correct heading (with name, student ID and date) 5 points
Exercise 2 Reading the string S
Correct string S2
Correct string S3
Correct string S4
Correct string S5
2 points
2 points
2 points
2 points
2 points
Exercise 2 Read the string and the key N
Generate correct encryption
5 points
10 points
Exercise 3 Read the change amount
Compute correct distribution of coins
5 points
15 points

You will get credit for a question only if it is properly executed...

Do not forget to logout from the lab computers!





  Page last modified 17 December 2015 http://www.cs.ucdavis.edu/~koehl/