EXAM 02 STUDY GUIDE

This study guide will help give you an idea of what to expect for the exam. It is still a work in progress, and may have some changes (you will be notified via email as those changes occur). All book, quiz, notes, slides, assignments, lecture, and code example content can be inspiration for exam questions.

FORMAT:

Moodle based Exam:

Total: 26 points

TOPICS:

This exam builds off of previous content we have covered in this class. The topics listed here are specific to the new content we have covered. You will still need to know Python data structures, if statements, conditionals, loops, etc.

SAMPLE CODING QUESTIONS

The exam coding assignment(s) can come from the examples below, previous exercises, book exercises, or code demos from our GitHub repository and class.

Question #1

Create two Python source code files in the same directory named main.py and image_editor.py. Write a function in image_editor.py named greyscale. This function takes one parameter, which is a path to an image on your computer, and returns a greyscale version of that image. To create a greyscale image, you need to determine the average luminence of a pixel (brightness) and then set that value for each of the r, g, b channels of that pixel. From main.py you will call your greyscale function from image_editor.py.

Question #2

Create a class called Pixel. This class has the following attributes:

These attributes should be assigned values when you create an object instance of the Pixel class. The pixel class must have two methods:

Test your class by creating a new file pixel_test.py that creates two pixel objects, prints their info, and the displays their color distances from one another.

Question #3

Create a Python file called blackjack.py and write a function that correctly computes the value of a hand in blackjack. Assume that the function takes one parameter which is a list of cards representing the “hand” of cards. Each card is represented as 2 through 10, J, Q, K, A with out any consideration for suits. Remember that the cards 2-10 have their respective numeric values, while J, Q, K are valued as 10, and the A is either a 1 or an 11 depending on whether that value would make the player go over the maximum hand value of 21.

Create a second Python file named card_test.py which opens a input text file that contains possible blackjack hand values. Each line of the file should represent one hand with each card separated by a space. YOU MAY NOT ASSUME HOW MANY LINES ARE IN THE FILE. There could be zero, one, or one thousand. You will read in these blackjack hands one at a time and call your function from blackjack.py to calculate the value of the hand. The value of each hand should be written out to an output file with one result per line. The result should be that each line in the output file has a hand value that matches the respective hand of cards in the input file.

Question #4

Create a function that uses Turtle to draw nearly any shape where the sides are all the same length. Your function should three parameters:

Place this function in a file called tortoise.py and call this function using another Python file named hare.py.

Question #5

Create a python file named caesar.py and write a function called cipher. This function will take two parameters:

Your cipher function will implement the caesar cipher to shift the letters in the string by the “shift” value provided. For example:

Should preserve capital letters and spaces:

Your function will return this encrypted string as the result.

Create a second python file named brutus.py which opens an input text file containing normal (unencrypted) text. You will call your ciper function and give it all of the text in the file as well as the shift value you wish to use for the encryption. You will then take the encrypted string you received from your ciper function and append that text to the end your normal text file (put one newline of space in between the normal and encrypted text for easy reading).