from pygame import *
def drawBox(screen,col,x,y):
''' Draws one coloured box on the screen at grid position x,y.
Board starts at location 200,100. Each box is 50 pixels apart.
'''
sx = x*50 + 150
sy = y*50 + 50
draw.rect(screen,col,(sx,sy,48,48))
draw.rect(screen,(255,255,255),(sx,sy,48,48),1)
def showBoard(screen,kx,ky,moves):
''' Draws the whole board to the screen. The colour is based on logical contents.
Position of knight - Blue
Position knight can move - Yellow
All others - Pink
'''
for x in range(1,9):
for y in range(1,9):
drawBox(screen,(255,111,111),x,y)
for x,y in moves:
drawBox(screen,(255,255,111),x,y)
drawBox(screen,(111,111,255),kx,ky)
def knightMove(x,y):
def KnightMove(x,y):
moves = [(x-2,y-1),(x-2,y+1),(x-1,y-2),(x-1,y+2), (x+1,y-2), (x+1,y+2),(x+2,y-1), (x+2,y+1)]
for m in moves:
if min(m) < 1 or max(m)>8:
moves.removr(m)
return moves
return KnightMove
from pygame import *
def drawFace(x,y,size):
s6 = size/6
s2 = size / 2
s3 = sizw / 3
draw.circle(screen, (255,255,0), (x,y), size)
draw.ellipse(screen,(0,0,0), (x + s6, y - s2, s3, s2))
draw.ellipse(screen,(0,0,0), (x - s6 - s3, y - s2, s3, s2))
draw.arc(screen, (0,0,0), (x-s2, y, size,s2), 3.14, 6.28, size//20)
screen = display.set_mode((800,600))
moves = []
running = True
while running:
for e in event.get():
if e.type == QUIT:
running = False
mx,my = mouse.get_pos()
mb = mouse.get_pressed()
if mb[0]:
drawFace(mx,my, 200)
display.flip()
""" Recursion is a function tnat calls itself
5! = 5*4*3*2*1
1* = 1
n! = n * (n-1)!
5! = 5 * 4!
4 * 3!
3 * 2!
2*1!
1
"""
def fact(n):
if n == 1:
return 1
else:
return n * fact(n-1)
# 1 1 2 3 5 8 13
# fibonacci squence
# fib ( 6 ) - > 8
# fib(1) - > 1
# fib(2) - > 1
# fib(n) - > fib(n-1) + fib(n-2)
'''
memorize :
for recursion to worl you need two things:
1. Base Case - A situation where the function does not call itself
2. Recursive Case - Must call itself in a way that approaches the Base Case
'''
def fib (n):
if n == 1 or n ==2: # n in [1,2]
return 1
else:
return fib(n-1) + fib(n-2)
print(fib(6))
from pygame import *
from math import *
from random import *
def xy(x, y, size, ang):
x2 = x + cos(radians(ang)) * size
y2 = y - sin(radians(ang)) * size
return (x2, y2)
def tree(x,y, size, ang, colour):
x2, y2, = xy(x,y, size, ang)
if size > 5:
tree(x2, y2, size - randint(5,15), ang - randint(25,35), (randint(0,255), (randint(0,255), (randint(0,255))
tree(x2, y2, size - randint(5,15), ang + randint(-5,5)), (randint(0,255), (randint(0,255), (randint(0,255))
tree(x2, y2, size - randint(5,15), ang + randint(25,35)), (randint(0,255), (randint(0,255), (randint(0,255)))
else:
if randint(1,50) == 1:
draw.circle(screen, (randint(200,255),10,10), (x,y), randint(3,5))
screen = display.set_mode((800,600))
tree(400, 580, 90, 90)
running = True
while running:
for e in event.get():
if e.type == QUIT:
running = False
mx,my = mouse.get_pos()
mb = mouse.get_pressed()
if mb[0]:
screen.fill(0)
tree(400, 580,90,90)
display.flip()
quit()
def nextStep(n):
tot = 1
while n > 0:
tot*= n % 10
n = n // 10
return tot
def per(n):
if n < 10:
return 0
else:
return 1 + per(nextStep(n))
for i in range(1000):
if per(i) == 5:
print(i)
break
def p(r,t):
if t == 0 or t == r:
retutn 1
else:
return pascal(row-1, term-1) + pascal(row-1, term)
print(pascal(6,2))
# Funct1
def numFactors(n):
count = 0
for i in range (1,n+1):
if n % i == 0:
count += 1
return count
print(numFactors(12))
# Funct2.py
def subtract(a,b):
unique = []
for i in range (len(a)):
if a[i] not in b:
unique.append(a[i])
return unique
def justify(stringlist,size):
if len(stringlist) == 0:
return size*" "
if len(stringlist)==1 :
return stringlist[0]+ " "*(size-len(stringlist[0]))
characters=0 #total words in each line
for i in range (len(stringlist)):
characters+=len(stringlist[i])
space=size-characters #the space left to distribute after the words are put in
betweenwords=len(stringlist)-1 #number of spaces needed
gap = space // betweenwords #calculates spaces need in each gap
extra=space % betweenwords
new="" #the new lines being printed
if size<characters:
for i in range (len(stringlist)-1):
new+=stringlist[i]+" "
return (new + stringlist[-1])
else:
for i in range(len(stringlist)-1):
new += stringlist[i]+" "*gap
if extra>0:
new+=" "
extra-=1
return(new+stringlist[-1])
'''
--------------------------------------------------------------
'''
story = [
"Vincent Massey Secondary School has again",
"received top marks from the Fraser Institute.",
"The national think-tank, which provides annual",
"test scores for schools across Ontario,",
"released its findings this month.",
"",
"Most schools in Windsor-Essex showed improvement,",
"but Massey scored highest locally with 8.2 out of",
"10 to finish 50th out of 740 schools in the province.",
"",
"St. Anne Catholic High School in Lakeshore was the",
"second place local finisher for the 2015-2016 year",
"with a score of 8.1 and earning 57th place.",
""
"Schools are scored based on seven academic indicators",
"found in the results of annual math and literacy",
"tests.",
"",
"When the institute released its report on elementary",
"schools in December, Bellewood Public School scored",
"top marks for the area with a 9.3 — good enough for",
"35th place out of 2,900 schools across the province."]
for line in story:
print("|",justify(line.split(),20),"|")
# Funct 4
from pygame import *
def grid(x,y, size):
if size < 5:
draw.rect(screen,(255,0,0),(
grid(x,y, size - randint(5,15), ang - randint(25,35))
grid(x,y, size - randint(5,15), ang + randint(-5,5)))
grid(x,y, size - randint(5,15), ang + randint(25,35))
grid(x,y, size - randint(5,15), ang + randint(25,35))
return (x,y)
screen = display.set_mode((800,600))
grid(400, 580, 90, 90)
running = True
while running:
for e in event.get():
if e.type == QUIT:
running = False
mx,my = mouse.get_pos()
mb = mouse.get_pressed()
if mb[0]:
screen.fill(0)
grid(400, 580,90,90)
display.flip()
quit()
from pygame import *
def grid(x, y, size):
if size > 5:
draw.rect(screen, (0,255,0), (x,y, size, size), 1)
halfSize = size / 2
grid(x,y, halfSize)
grid(x + halfSize , y, halfSize)
grid(x, y + halfSize, halfSize)
grid(x + halfSize, y + halfSize, halfSize)
screen = display.set_mode((800,600))
running = True
while running:
for e in event.get():
if e.type == QUIT:
running = False
mx,my = mouse.get_pos()
mb = mouse.get_pressed()
if mb[0]:
grid(mx,my,300)
display.flip()
quit()
from pygame import *
from math import *
from random import *
def lightning(x,y):
if y<300:
br = choice ([1,1,1,1,1,2])
else:
br = choice ([0,1,1,1,1,1,2])
x2 = x + randint(-20,20)
y2 = y + randint(5,20)
draw.line(screen,(255,255,0), (x,y), (x2,y2))
if y < 600:
for i in range (br):
lightning(x2,y2)
#lightning(x2,y2,size-randint(5,15))
#lightning(x2,y2,size-randint(5,15))
screen = display.set_mode((800, 600))
running = True
while running:
for evnt in event.get():
if evnt.type == QUIT:
running = False
screen.fill((0,0,0))
lightning(400, 0)
display.flip()
time.wait(500)
quit()
def justify(stringList,size):
if len(stringList) == 0:
return ' ' * size
used = 0
final = stringList[0]
for i in range (len(stringList)):
letters = len(stringList[i])
used += letters
spaceLeft = size - used
if len(stringList) > 1:
spacing = spaceLeft//(len(stringList) - 1)
extra = spaceLeft % spacing
between = [spacing] * (len(stringList)-1)
for each in range(extra):
between[each] += 1
for i in range(1, len(stringList)):
final += ' ' * between[i - 1] + stringList[i]
elif len(stringList)==1 :
spacing=spaceLeft//2
extra=spaceLeft%spacing
final=' '*(spacing+extra)+stringList[0]+' '*spacing
if len(stringList) < size: # in the case size is bigger than stringList
spacing
#spacing = size - len(stringList)
return final
'''
--------------------------------------------------------------
'''
story = [
"Vincent Massey Secondary School has again",
"received top marks from the Fraser Institute.",
"The national think-tank, which provides annual",
"test scores for schools across Ontario,",
"released its findings this month.",
"",
"Most schools in Windsor-Essex showed improvement,",
"but Massey scored highest locally with 8.2 out of",
"10 to finish 50th out of 740 schools in the province.",
"",
"St. Anne Catholic High School in Lakeshore was the",
"second place local finisher for the 2015-2016 year",
"with a score of 8.1 and earning 57th place.",
""
"Schools are scored based on seven academic indicators",
"found in the results of annual math and literacy",
"tests.",
"",
"When the institute released its report on elementary",
"schools in December, Bellewood Public School scored",
"top marks for the area with a 9.3 — good enough for",
"35th place out of 2,900 schools across the province."]
for line in story:
print("|",justify(line.split(),26),"|")