Presentation is loading. Please wait.

Presentation is loading. Please wait.

HW 3: Problems 2&3. HW 3 Prob 2:Encipher encipher( S, n ) takes as input a string S and a non-negative integer n between 0 and 25. This function returns.

Similar presentations


Presentation on theme: "HW 3: Problems 2&3. HW 3 Prob 2:Encipher encipher( S, n ) takes as input a string S and a non-negative integer n between 0 and 25. This function returns."— Presentation transcript:

1 HW 3: Problems 2&3

2 HW 3 Prob 2:Encipher encipher( S, n ) takes as input a string S and a non-negative integer n between 0 and 25. This function returns a new string in which the letters in S have been rotated by n characters. For this problem, you should assume that.... upper-case letters are "rotated" to upper-case letters lower-case letters are "rotated" to lower-case letters all non-alphabetic characters are left unchanged. Examples: Shift ‘y’ by 3  ‘b’ Shift ‘Y’ by 3  ‘B’

3 abcdefghijklmnopqrstuvwxyz 97122 65 ABCDEFGHIJKLMNOPQRSTUVWXYZ 90 ASCII VALUES 99101103105107109111113115117119 6769717375777981838785 HW 3 Prob 2:Encipher chr(66) is 'B' ord('a') is 97 What is chr( ord('i')+13 )'v' What is chr( ord('P')+13 ) ']'

4 HW 3 Prob 2:Encipher Wala! If c was ‘x’  def adv13( c ): """ rotates c by 13 chars, "wrapping" as needed NON-LETTERS DO NOT CHANGE! """ if 'a' <= c <= 'z': #neword = 120 + 13 neword = ord(c) + 13 if neword <= ord('z'): return chr(neword) # no wrapping else: #chr(97+ 133 – 122 -1)  chr(107)  ‘k’ return chr(ord('a')+neword-ord('z')-1) elif 'A' <= c <= 'Z': # same as above, only use 'A' and 'Z' else: return c

5 HW 3 Prob 2:Decipher decipher decipher should return the original English string from an encoding, to the best of its ability. Note: some strings have more than one English "deciphering." What's more, it is very difficult to handle short strings correctly. Thus, your decipher does not have to be perfect. However, you could use letter frequencies -- a function is provided online. - Scrabble scores have also been suggested!?! - You might want also to use some additional "heuristics" (rules of thumb) of your own design.

6 HW 3 Prob 2:Decipher def decipher(S): """ go through all rotations and see which makes most sense "" encoded word is ‘Nkuc’ #create all 26 possibilities [‘Olvd’,’Pmwe’,….] (Hint: use your encoding function!) …. #create list of lists of prob for each letter [[#,#,#,#],[#,#,#,#]….[…]] (could use scrabble score for this…) …. #sum the letter probabilities [ #, #, …., #] …. #find the maximum probability  MAX FROM PREVIOUS LIST ….. #search for index where there is the maximum  use a for loop? …. #return most likely decoding…. ….

7 HW 2 Prob 3: Looks Good! Click here to for set up instructions… http://cs.northwestern.edu/~akuzma/classes/EECS110- s09/hw/hw3/hw3pr3.htm

8 HW 2 Prob 3: Looks Good! pixels: [ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] Width: len(pixels[0]) Height: len(pixels) 2x2 pixel image

9 [ L[j][0] for j in range(2) ] L [[42, 43], [44, 45]] [ [L[0][i]] for i in range(2) ] [ [ L[j][i]+1 for i in range(2) ] for j in range(2) ] HW 2 Prob 3: Looks Good! Comprehending List Comprehensions [42, 44] [42, 43] [[43,44][45,46]]

10 HW 2 Prob 3: Looks Good! [ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] Tuples use ( ); lists use [ ] But otherwise, they are the same… (for now, almost) >>> t = (1, 2, 3) >>> t[1] 2 >>> t[1:] (2, 3) >>> (x, y, z) = t >>> x 1 >>> y 2

11 HW 2 Prob 3: Looks Good! def modify(pic): """ modify modifies an image to make it brighter """ pixels = getPixels(pic) #returns a list of lists, example for 2x2… # [ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] if len(pixels) == 0: return newPixels = [ [setNewPixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] setPixels(pic, newPixels) def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW imanges (row, col) (r,g,b) value input pixels: a 2D list containing RGB information in the pixels in a picture input row: the row of the pixel in question input col: the column of the pixel in question """ rval= min(pixels[row][col][0]+30, 255) gval = min(pixels[row][col][1]+30, 255) bval = min(pixels[row][col][2]+30, 255) return (rval, gval, bval)

12 HW 2 Prob 3: Looks Good! Your task is to implement (at least) 3 functions, one from each group (groups will be reviewed shortly) For up to +15 points of extra credit (depending on the scope of your additions) you can implement more of them or make up your own creative image manipulation function.

13 Group 1: negative.py: Modifies an image to create its negative. That is, all color values should be 255 minus their original value grayscale.py: Modifies an image to make it grayscale (i.e. black and white). HW 2 Prob 3: Looks Good! There is no one "correct" conversion from RGB to grayscale, since it depends on the sensitivity response curve of your detector to light as a function of wavelength. A common one in use is: Y = 0.3*R + 0.59*G + 0.11*B

14 HW 2 Prob 3: Looks Good! Group 2: flipVert.py: Flip the image on its horizontal axis Hint: flip the rows around flipHoriz.py: Flip the image on its vertical axis Hint: flip the columns around mirrorVert.py: Mirror the photo across its horizontal axis (i.e., so that the top part is mirrored upside down on the bottom of the image Hint:first, must find the midpoint of the columns mirrorHoriz.py: Same as above, but across the vertical axis Hint:first, must find the midpoint of the rows

15 HW 2 Prob 3: Looks Good! Group3: scale.py: Scale the image to half its original size (either horizontally, vertically, or both) blur.py: Blur the image by combining neighboring pixels in some way (up to you) one method would be to take the average of the pixels surrounding the pixel you are looking at

16 HW 2 Prob 3: Looks Good! randomGrid.py: Divide the image into an NxN grid (where the N is up to you) and randomly sort the pieces of the grid across the image. Useful function: random.shuffle Ex. Shuffle [0,1,2]  [1,2,0] [[0,1,2],[3,4,5],[6,7,8]]  [[3,4,5],[0,1,2],[6,7,8]] Feel free to add any more functions you can think of. Be creative! The professor will show off the more interesting images in class.


Download ppt "HW 3: Problems 2&3. HW 3 Prob 2:Encipher encipher( S, n ) takes as input a string S and a non-negative integer n between 0 and 25. This function returns."

Similar presentations


Ads by Google