# 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')
We have modified an image (list of lists of tuples with syntax (R,G,B)) of Super Mario as follows. From top to bottom:
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.