This is all the notes I have taken during class:

# how can i append some text at the beginning of a file?

if 1:

with open(r'.\filehandler\test33-Open.txt', 'r') as fh:

l = fh.readlines()

l.insert(0, '****')

l.insert(len(l)//2 , '++')

with open(r'.\filehandler\test33-Open.txt', 'w') as fh:

fh.writelines(l)

'''

QuickSort:

Best Case Scenario:

- Depth: log2(n) (Depth == (imagine the recursion fuction as a tree), the height of the tree)

- We have to compare n elements log2(n) times so O(n)=n*log2(n)

Worst Case Scenario:

- Depth: n

- O(n) = n**2

'''

'''

RGB System:

- Each image is a matrix, each pixel is a tuple with 3 components: (R,G,B)

- Additive System: each color is given by the sum of the light from the 2 pixels

'''

import pngmatrix

# Mario into Luigi

mario = pngmatrix.load_png8(r'.\img\Mario.png')

luigi = [[pixel if pixel!=(227,6,19) else (29, 227, 6) for pixel in x] for x in mario]

pngmatrix.save_png8(luigi, r'.\img\Luigi.png')

# Mirror Luigi

luigimir = luigi.copy()

l = [row[::-1] for row in luigimir]

pngmatrix.save_png8(l, r'.\img\LuigiMir.png')

# Mario(sx) & Luigi(dx)

patch=[mario[x]+l[x] for x in range(len(l))]

pngmatrix.save_png8(patch, r'.\img\patch.png')

# Luigi Transposed

tmario = [[mario[y][x] for y in range(len(mario[0]))] for x in range(len(mario))]

pngmatrix.save_png8(tmario, r'.\img\TMario.png')

# Mario rotated

rotmario = [[mario[y][x] for y in range(len(mario[0])-1, -1, -1)] for x in range(len(mario))]

pngmatrix.save_png8(rotmario, r'.\img\RotMario.png')

The part you are interested in is after the line: import pngmatrix

We have modified an image (list of lists of tuples with syntax (R,G,B)) of Super Mario as follows. From top to bottom:

- Changed a particular tuple with another (Mario's Red to Luigi's Green)
- Mirrored the image with respect to the vertical axis
- Concatenate two images
- Transpose an image (just like in linear algebra, rows become colums and vice versa)
- rotate an image by 90°

I really like using list comprehensions, as you might see, I hope it is understandable...

The files where shared by professor Mancini in "Unit 1, class exercises" \ "Mario", click here.