Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2015.

Similar presentations


Presentation on theme: "Spring 2015."— Presentation transcript:

1 Spring 2015

2 I know Matrix

3 Evolving Lists & Lights On!
Caesar Cipher Looks Good!

4 runGenerations2d evolve2d setNewElement2d allOnes2d randBL2d

5 >>> def get_one_spot():
...     return ‘This is one spot’ ...  >>> def get_a_list(n): ...     return [get_one_spot() for y in range(n)] >>> def get_lists_of_lists(n): ...     return [def get_a_list(n) for y in range(n)]

6 Rotates the string S by n characters Example:
encipher(S, n) Rotates the string S by n characters Example: encipher('AbC', 2) returns 'CdE' encipher('xYz', 4) returns 'bCd' decipher(S) Retrieve the original string from an enciphered version (with unknown rotation)

7 Rotate character individually
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

8 Note: need to take care of wrapping around
ASCII values 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 A B C X Y Z 65 66 67 88 89 90 a b c x y z 97 98 99 120 121 122

9 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

10 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, , , ...] Return the one with the largest sum (using recursion? using for loop?)

11 Get all possible strings (26)
Calculate String probability Locate maximum index Get original String back (encipher)

12 s = 'vnz yrneavat clguba’
>>> possible_strings = [0]*3 >>>  >>> possible_strings[0] = encipher('vnz yrneavat clguba’, 0) >>> possible_strings[1] = encipher('vnz yrneavat clguba', 2) >>> possible_strings[2] = encipher('vnz yrneavat clguba', 3) >>> possible_strings ['woa zsofbwbu dmhvcb', 'xpb atpgcxcv eniwdc', 'yqc buqhdydw fojxed']

13 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

14 An image is a 2 dimensional list (i.e. list of lists) of pixels
Example: pixels = [[pixel, pixel], [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

15 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,100) is gray (112, 48, 160) is purple List uses [ ], tuple uses ( ) No difference (for now)

16 pixels = [[(255,0,0), (0,255,0)], [(0,0,255), (100,100,100)], [ (0,0,0), (112,48,160)]]

17 The beginning from modifyBase import
The beginning from modifyBase import * label = "Brighten" # change this ordinal = 1 Similarity in roles to problem 1: modify(pic) is similar to evolve(L) setNewPixel is similar to setNewElement In general, modifying setNewPixel is enough, but feel free to do anything else.

18 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)

19 def setNewPixel( pixels… ):
def modify(pic): def setNewPixel( pixels… ): pixels = getPixels(pic) All fancy spot (i,j) allocations modifiers

20 >>> pixels = [[1,2,3,4] , [5,6,7,8] , [9,10,11,12] , [13,14,15,16]]
>>>  >>> pixels [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] >>> def crossFlip(pixels): ...     new_pixels = [[get_pixels(pixels , i , j) for j in range(4)] for i in range(4)] ...     return new_pixels >>> def get_pixels(pixels , i , j): ...     if i+j>3: ...             return pixels[3-j][3-i] … if i+j<3:

21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4

22 Have fun + Good luck


Download ppt "Spring 2015."

Similar presentations


Ads by Google