1 / 16

HW 3: Problems 2&3

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 a new string in which the letters in S have been rotated by n characters. For this problem, you should assume that....

sheena
Download Presentation

HW 3: Problems 2&3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. ord('a') is 97 chr(66) is 'B' What ischr( ord('i')+13 )'v' abcdefghijklmnopqrstuvwxyz ASCII VALUES 97 99 101 103 105 107 109 111 113 115 117 119 122 What ischr( ord('P')+13 ) ']' ABCDEFGHIJKLMNOPQRSTUVWXYZ 65 67 69 71 73 75 77 79 81 83 85 87 90 HW 3 Prob 2:Encipher

  4. HW 3 Prob 2:Encipher 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’ returnchr(ord('a')+neword-ord('z')-1) elif 'A' <= c <= 'Z': # same as above, only use 'A' and 'Z' else: return c Wala! If c was ‘x’ 

  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 • defdecipher(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. HW 2 Prob 3: Looks Good! Comprehending List Comprehensions [ L[j][0] for j inrange(2) ] L [[42, 43], [44, 45]] [42, 44] [ [L[0][i]] for i inrange(2) ] [42, 43] [ [ L[j][i]+1 for i inrange(2) ] for j inrange(2) ] [[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! defmodify(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) defsetNewPixel( 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. HW 2 Prob 3: Looks Good! • 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). 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.

More Related