Spring 2010 EECS 110 Homework III Homework III

  • Slides: 14
Download presentation
Spring 2010 EECS 110: Homework III

Spring 2010 EECS 110: Homework III

Homework III Evolving Lists & Lights On! Caesar Cipher Looks Good!

Homework III Evolving Lists & Lights On! Caesar Cipher Looks Good!

Problem 2: Caesar Cipher Overview encipher(S, n) Rotates the string S by n characters

Problem 2: Caesar Cipher Overview encipher(S, n) Rotates the string S by n characters Example: encipher('Ab. C', 2) returns 'Cd. E' encipher('x. Yz', 4) returns 'b. Cd' decipher(S) Retrieve the original string from an enciphered version (with unknown rotation)

Problem 2: Caesar Cipher encipher – Overview Recursion Rotate character individually Rules: Lower-case →

Problem 2: Caesar Cipher encipher – Overview Recursion Rotate character individually Rules: Lower-case → lower-case Upper-case → upper-case Otherwise → unchanged Character checking: 'a' <= c <= 'z' 'A' <= c <= 'Z' Character rotation: using ASCII values

Problem 2: Caesar Cipher encipher – Character Rotation ASCII values A B C …

Problem 2: Caesar Cipher encipher – Character Rotation ASCII values A B C … X Y Z 65 66 67 … 88 89 90 a b c … x y z 97 98 99 … 120 121 122 Useful functions: ord('a') returns 97 chr(66) returns 'B' Example: rotate 'b' by 22 characters: chr(ord('b') + 22) returns 'x' Note: need to take care of wrapping around

Problem 2: Caesar Cipher decipher – Strategy Unknown number of rotation → Test 26

Problem 2: Caesar Cipher decipher – Strategy Unknown number of rotation → Test 26 cases and pick out the best Techniques: English letter appearance probability (provided!) Scrabble scores (roughly a reverse of the above) Any other heuristics (i. e. rules of thumb) Should work well on large strings

Problem 2: Caesar Cipher decipher – Example Flow Create a list of all possibilities

Problem 2: Caesar Cipher decipher – Example Flow Create a list of all possibilities (there are 26) #['Dmc', 'End', 'Foe', . . . ] For each possibility, calculate the sum of the probability of the letters #[0. 0766, 0. 1943, 0. 1802, . . . ] Return the one with the largest sum (using recursion? using for loop? )

Problem 3: Looks Good Overview See class web-site for set-up instruction Goals: Write basic

Problem 3: Looks Good Overview See class web-site for set-up instruction Goals: Write basic image editing tools At least 3, one from each of the following: ▪ Group 1: negative, gray scale ▪ Group 2: vertical flip, horizontal flip, vertical mirror, horizontal mirror ▪ Group 3: scale, blur, random grid

Problem 3: Looks Good Image An image is a 2 dimensional list (i. e.

Problem 3: Looks Good Image An image is a 2 dimensional list (i. e. list of lists) of pixels Example: pixels = [[pixel, pixel], [pixel, pixel]] Note: An image is a list of rows A row is a list of pixels len(pixels) returns height len(pixels[0]) returns width

Problem 3: Looks Good Pixel Color A pixel is a tuple of 3 colors

Problem 3: Looks Good Pixel Color A pixel is a tuple of 3 colors red, green, blue Color value is from 0 to 255 Example: (255, 0, 0) is red (100, 100) is gray (112, 48, 160) is purple List uses [ ], tuple uses ( ) No difference (for now)

Problem 3: Looks Good Put them together pixels = [[(255, 0, 0), (0, 255,

Problem 3: Looks Good Put them together pixels = [[(255, 0, 0), (0, 255, 0)], [(0, 0, 255), (100, 100)], [ (0, 0, 0), (112, 48, 160)]]

Problem 3: Looks Good Example Code: Brighten The beginning from modify. Base import *

Problem 3: Looks Good Example Code: Brighten The beginning from modify. Base import * label = "Brighten" # change this ordinal = 1 Similarity in roles to problem 1: modify(pic) is similar to evolve(L) set. New. Pixel is similar to set. New. Element In general, modifying set. New. Pixel is enough, but feel free to do anything else.

Problem 3: Looks Good Functions to implement Group 1 (individual pixel): Negative: new color

Problem 3: Looks Good Functions to implement Group 1 (individual pixel): Negative: new color value = 255 – original value Grayscale (values of red, green & blue are equal) Group 2 (change pixel position): Flip/Mirror horizontally/vertically Group 3 (using multiple pixels): Scale Blur: take an average of surrounding pixels Random Grid: Use random. shuffle(L)

Homework III Have fun + Good luck

Homework III Have fun + Good luck