CS21 Lab 3: Conditionals, if

Due before midnight, Saturday September 27

This lab assignment requires you to write more programs in python. As always, first run update21. This will create the cs21/labs/03 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/03 directory and begin working on the python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ update21
$ cd cs21/labs/03
$ pwd
/home/your_user_name/cs21/labs/03
We will only grade files submitted by handin21 in this labs directory, so make sure your programs are in this directory!

This is your third lab. By this point, you are starting to write non-trivial programs. Therefore, it is time to start getting into habits of good programming practice. In particular, we want to see this:


1. Run-length Encoding

One of the ways that computers use to compress strings is called "run-length encoding". It works by looking for "runs", or sequences of identical characters, and substituting the run by the character that it contains, followed by the number of times that it repeats. This method is very useful when compressing simple graphic images such as icons, line drawings, etc.

For example, consider the following string:

aaaaaaaaaabbbbbbccccc

This string has 21 characters. The character "a" appears 11 times, followed by the character "b" 6 times, then the character "c" 5 times. Using run-length encoding would give us this output string:

a10b6c5

Altogether, that takes up 7 characters, for a compression ratio of 0.33!

Write a program called runLengthEncoding.py that asks the user for a string, and then displays the run-length encoded version of the string and the compression ratio.

Here is a sample run of such a program:

$ python runLengthEncoding.py

string: aaaaaaaaaabbbbbbccccc
The compressed string is a10b6c5
The old string had 21 characters
The compressed string has 7 characters
Compression ratio is 0.33

Hints: For this program you need:


2. Factorization

The factorization or factoring of an integer is done by decomposing it into a product of other integers, or factors. For example, the number 12 factors into 1x12, 2x6 and 3x4. Prime numbers have no factors other than 1 and themselves.

Write a program factorization.py that will ask the user for a number, and decompose it into its factors. If the number turns out to be a prime number, your program should inform the user.

Here is a sample run of such a program:

$ python factorization.py

Give me a number: 12
1 x 12 = 12
2 x 6 = 12
3 x 4 = 12
$ python factorization.py

Give me a number: 13
1 x 13 = 13
13 is a prime number

Note that the program does not repeat reversed factorizations -- for example, when the number given is 12, the program gives 2x6, 3x4, but does not go on to give 4x3 and 6x2. In other words, when we look for factors, we only need to go all the way up to the square root of that number. Anything beyond that would be repeating ourselves (think about it and see if you can figure out why.)

The sqrt() function in the math library gives us the square root of a number. Recall to import a library in python, the import statement goes at the top of the program and you only need to import once per program.

from math import *

3. Mini Quiz

Your third assignment is to write a program, miniquiz.py that will run a mini quiz. An example run of the program might look like the following:

$ python miniquiz.py

This is a quiz about the game of Ultimate.

1. In ultimate a frisbee is called a disc (y/n)? y
You are correct!
2. The game of ultimate is self officiated (y/n)? n
Sorry that is the wrong answer.
3. Ultimate is where people throw discs to dogs (y/n)? y
Sorry that is the wrong answer.
4. In ultimate you can run with the disc (y/n)? y
Sorry that is the wrong answer.
5. In ultimate you can throw a scoober or a hammer (y/n)? n
Sorry that is the wrong answer.

You got 1 out of 5 correct.
Go buy a disc and give ultimate a try!

Obviously, depending on the topic you choose, the output to your program may look very different. Therefore, we are only going to give you the requirements for your program this time, and your output may vary accordingly:

NOTE: This is not required. For an extra challenge, make your question list a bank of questions. Then let the user decide how many questions he/she wishes to tackle, and randomly pick questions out of your list. You do not need to worry about repeating questions.)



Submit

Run handin21 every time you make a significant update to your programs. We will only grade the most recent submission submitted prior to the deadline.

Remember: for this lab, programs must appear in your cs21/labs/03 directory. If you create your programs in a different directory, use the unix mv or cp commands to move or copy them into the cs21/labs/03 directory. For example:

cp  myprog.py ~/cs21/labs/03/myprog.py