Type: Group Assignment

Due: by 10:00 PM on Tuesday, April 16, 2019

Prologue

This is a group assignment. Remember that you should always be working with your partner. You may NOT divide up the work and complete the parts independently. All work you submit must be fully authored by both you and your partner. I also suggest that you work on one computer and take turns using the keyboard. Here are some suggested policies.

  1. Set a timer for 10-15 minutes and switch who is using the keyboard. Even if you’re not comfortable using a keyboard, it is important that you get experience doing so in this course.
  2. If you are currently the driver (i.e. the one using the keyboard), practice talking out loud what you are thinking as you are typing.
  3. If you are currently the navigator (i.e. the one not using the keyboard), pay close attention to what the driver is doing, offer feedback, and stay involved throughout that time. Do NOT simply sit idly and let the driver work alone.
  4. Ask each other questions. If you’re confused about something your partner is doing, let them know and ask them if they could explain or clarify their idea. Talking to each other and checking each other’s work is the best way to pair program!

I recommend that you create a folder named assignment3 in your StuWork directory. If you have the courses drive mounted already, you can do this by executing the commands.

$ cd /Volumes/COURSES/cs111-03-s19/StuWork/USERNAME
$ mkdir assignment3
$ cd assignment3

Note that USERNAME should be replaced with your actual username.

After executing the third command above, you will be inside the assignment3 directory and ready to work.

Problem 0 (Code of Conduct)

History has shown that it is useful to have a “Code of Conduct” for pair programming that is tailored to our class. Your task for this problem is to help develop key elements to be included in this code of conduct in order to make the pair programming experience as effective as possible. In particular, try to answer the following questions:

  1. Are there any general principles of behavior that should be included in the code of conduct?
  2. What are the responsibilities and role of the driver (i.e. the one currently using the keyboard)?
  3. What are the responsibilities and role of the navigator (i.e. the one currently not using the keyboard)?
  4. Are there any other guidelines that each should follow to make the partnership as educational and effective as possible?

Document your ideas in a text file named suggestions.txt and place it in the Hand-in/USERNAME/code_of_conduct directory.

Problem 1

Write a program called nearest_multiple.py that asks the user to input a number (here I will call it num) and a strictly positive integer (here I will call it mult) and prints as output the closest integer to num that is a multiple of mult. Below are some example inputs and corresponding outputs.

Input1 : (num) Input 2: (mult) Output
17 3 18
725.3 100 700
-123.7 25 -125

Problem 2

Sometimes programmers write programs that are difficult to read. Let’s take a look at the following program.

# mystery.py
# Titus Klinge
# 2019-01-16
#
# Reads some input from the user and computes the output
the_good, the_bad, the_ugly = input(), input(), input()
dollars = (int(the_good) + int(the_ugly) + 2*int(the_bad)) - min(int(the_good),int(the_bad),int(the_ugly)) - int(the_bad) - max(int(the_good),int(the_bad),int(the_ugly))
print(dollars)

a. Create a new file mystery_solved.py and copy the code above into it.

b. Add yourselves as authors to the program in the comments at the top.

c. Determine what the program is doing and give it a better name and description in the comments at the top of the file. Please keep the name of the file mystery_solved.py—just change the comments at the top.

d. The variable names the_good, the_bad, and the_ugly aren’t very helpful. Give them names that are more descriptive of what they are. Consider renaming the dollars variable, too.

e. Change the program so that it explains what the program is doing to the user and asks them to type in inputs of the appropriate type.

f. Notice that the int() function is being called an excessive number of times. Modify the code so that it only calls int() at most three times.

g. Think again about what the program is doing. Are there any unnecessary steps in the computation of the output? If so, remove them.

Problem 3

Write a program describe_number.py that asks the user for a number and then prints a description of that number. The description of the number should adhere to the following rules.

  • If it is less than -1,000,000 include very small in the output
  • If it is more than 1,000,000 include very large in the output
  • If it is negative, include negative in the output
  • If it is positive, include positive in the output
  • If it is even, include even (zero is considered even)
  • If it is odd, include odd in the output
  • If it is of type int, include number that is an int in the output
  • If it is of float type, include number that is a float in the output

For example, here is a table of some inputs and their corresponding outputs.

Input Output
56 positive even number that is an int
-101.0 negative odd number that is a float
23.7 positive number that is a float
1000001 very large positive odd number that is an int

This problem can get rather complicated if you “hard code” every possible case into a long series of conditionals. Instead, try to identify which tasks are independent of other tasks. For example, the positive / negative aspect of the number is independent of the int / float aspect of the number. Thus, you can determine these separately and combine your answer at the end.

HINT: Read input from the user using eval(input()) on this problem. If they type in a float, then it will be given a “float” type and if they type in an int, it will be given an “int” type. Then you can check if a variable val is of type int, you can use the following Boolean expression:

type(val) == int

Problem 4

A Caesar cipher is a simple method of encrypting a message which makes it difficult to read unless you have a secret number called a shift. To encrypt a message, what you do is replace every character in your message with one that is shift number of characters ahead in the alphabet. For example, if shift=3, then all characters in the alphabet would be shifted by three and those on the end will “wrap around” to the beginning.

Alphabet A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Alphabet Shifted by 3 D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

Using a shift of 3, a message like “hello” would be encrypted to “khoor” since the character “h” gets shifted to “k” (since “k” is three characters ahead of “h”) and so on.

Part A

In this part, you will write a simple program shift_letter.py that asks the user to input exactly one lowercase alphabetic character (I call it letter below) as well as an integer shift, and then prints the “shifted” form of the character to the output. Below are some example input executions.

Input 1: letter Input 2: shift Output
a 3 d
z 1 a
b -1 a
h 5 m

You will likely find the ord() and chr() functions helpful in this problem.

Part B

In this part, you will write a program shift_phrase.py that asks the user to input a string of alphabetic characters (that may also include spaces) as well as an integer shift, and then prints the “shifted” form of the phrase to the output (spaces are not shifted). You will likely find the code you wrote in Part A to be helpful here.

Below are some example input executions.

Input 1: phrase Input 2: shift Output
hello world 3 khoor zruog
khoor zruog -3 hello world
the quick brown fox 13 gur dhvpx oebja sbk
gur dhvpx oebja sbk 13 the quick brown fox

Remember that strings in Python are sequences which means that you can use a definite loop to iterate over the individual characters. For example, the code

for c in "hello":
    print(c)

will print the following lines of to the output since the loop will execute once for exactly each character in the string.

h
e
l
l
o

Part C

Testing your programs to make sure they are correct is important. The goal of testing is to cover all of the odd cases that might cause your program to fail. In this part, you will write a list of tests that if your program passes, will give you confidence that it is perfectly correct.

To start, create a text file named shift_tests.txt that includes the following starter text.

phrase                               shift                  expected output
------                               -----                  ---------------
hello                                3                      khoor

Each line corresponds to a test case that maps the input the user types in to the correct output they should see. Add more tests to the text file until you are confident that there are enough to be sure that there are no errors in your code.

Please include your names at the top of the file and a brief explanation why you believe the tests you included give you confidence in the correctness of your program.

How to Turn in Your Code

a. Make sure that you and your partner are clearly identified at the top of every file you have created for this assignment.

b. All of your files need to be placed into the Hand-in/USERNAME/assignment3 directory. You can do this visually in Finder by copying all of your files in your StuWork/USERNAME/assignment3 directory into your Hand-in/USERNAME/assignment3 directory. (Note that you only need to do this once—it is not necessary for every member of your group to submit separately.)

c. Send all of your files to your partner so they have access to them, too!

Important Grading Criteria

We will evaluate your work with the following criteria in mind.

  1. Correctness. Does your code do what is asked? (e.g. named correctly, input and outputs correct values, etc.)
  2. Formatting. Is your code formatted so that it is easy to understand?
  3. Elegance. Is your code concise and elegant? Does it use subroutines when appropriate rather than copying and pasting certain parts?