Title: HW 3: Problems 2
1HW 3 Problems 23
2HW 3 Prob 2Encipher
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
3HW 3 Prob 2Encipher
4HW 3 Prob 2Encipher
def adv13( c ) """ rotates c by 13 chars,
"wrapping" as needed NON-LETTERS DO NOT
CHANGE! """ if 'a' lt c lt 'z'
neword 120 13 neword ord(c) 13
if neword lt 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' lt c lt 'Z' same as above, only
use 'A' and 'Z' else return c
Wala! If c was x ?
5HW 3 Prob 2Decipher
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.
6HW 3 Prob 2Decipher
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. .
7HW 2 Prob 3 Looks Good!
Click here to for set up instructions
http//cs.northwestern.edu/akuzma/classes/EECS110
-s09/hw/hw3/hw3pr3.htm
8HW 2 Prob 3 Looks Good!
pixels (3, 10, 100), (3, 11, 110), (3, 10,
200), (10, 110, 290)
- Width len(pixels0)
- Height len(pixels)
2x2 pixel image
9HW 2 Prob 3 Looks Good!
Comprehending List Comprehensions
Lj0 for j in range(2)
L
42, 43, 44, 45
42, 44
L0i for i in range(2)
42, 43
Lji1 for i in range(2) for j in
range(2)
43,4445,46
10HW 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)
gtgtgt t (1, 2, 3) gtgtgt t1 2 gtgtgt t1 (2, 3) gtgtgt
(x, y, z) t gtgtgt x 1 gtgtgt y 2
11HW 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(pixels0)) 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(pixelsrowcol030, 255) gval
min(pixelsrowcol130, 255) bval
min(pixelsrowcol230, 255) return
(rval, gval, bval)
12HW 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.
13HW 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.3R 0.59G 0.11B
14HW 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 - Hintfirst, must find the midpoint of the columns
- mirrorHoriz.py Same as above, but across the
vertical axis - Hintfirst, must find the midpoint of the rows
15HW 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
16HW 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.