Add Chapter 8 python files
This commit is contained in:
parent
2424fa6028
commit
5c38f5ef86
6 changed files with 228 additions and 0 deletions
31
thinkcspy/Chapter-8-Exercises/Python-files/Problem10.py
Normal file
31
thinkcspy/Chapter-8-Exercises/Python-files/Problem10.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
# Runestone.Academy thinkcspy course
|
||||||
|
# Chapter 8
|
||||||
|
# Problem 10
|
||||||
|
|
||||||
|
import image
|
||||||
|
|
||||||
|
|
||||||
|
img = image.Image("luther.jpg")
|
||||||
|
win = image.ImageWin(img.getWidth(), img.getHeight())
|
||||||
|
img.draw(win)
|
||||||
|
img.setDelay(0) # setDelay(0) turns off animation
|
||||||
|
|
||||||
|
|
||||||
|
for row in range(img.getHeight()):
|
||||||
|
for col in range(img.getWidth()):
|
||||||
|
p = img.getPixel(col, row)
|
||||||
|
|
||||||
|
R = p.getRed()
|
||||||
|
G = p.getGreen()
|
||||||
|
B = p.getBlue()
|
||||||
|
|
||||||
|
newred = (R * 0.393 + G * 0.769 + B * 0.189)
|
||||||
|
newgreen = (R * 0.349 + G * 0.686 + B * 0.168)
|
||||||
|
newblue = (R * 0.272 + G * 0.534 + B * 0.131)
|
||||||
|
|
||||||
|
newpixel = image.Pixel(newred, newgreen, newblue)
|
||||||
|
|
||||||
|
img.setPixel(col, row, newpixel)
|
||||||
|
|
||||||
|
img.draw(win)
|
||||||
|
win.exitonclick()
|
||||||
20
thinkcspy/Chapter-8-Exercises/Python-files/Problem2.py
Normal file
20
thinkcspy/Chapter-8-Exercises/Python-files/Problem2.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# Runestone.Academy thinkcspy course
|
||||||
|
# Chapter 8
|
||||||
|
# Problem 2
|
||||||
|
|
||||||
|
def print_triangular_numbers(n):
|
||||||
|
# your code here
|
||||||
|
i = 1
|
||||||
|
|
||||||
|
while i <= n:
|
||||||
|
trinum = 0
|
||||||
|
bound = i
|
||||||
|
|
||||||
|
while bound > 0:
|
||||||
|
trinum += bound
|
||||||
|
bound -= 1
|
||||||
|
|
||||||
|
print(i, "\t", trinum)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
print_triangular_numbers(5)
|
||||||
24
thinkcspy/Chapter-8-Exercises/Python-files/Problem3.py
Normal file
24
thinkcspy/Chapter-8-Exercises/Python-files/Problem3.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
# Runestone.Academy thinkcspy course
|
||||||
|
# Chapter 8
|
||||||
|
# Problem 3
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
def is_prime(n):
|
||||||
|
# your code here
|
||||||
|
|
||||||
|
x = 3
|
||||||
|
|
||||||
|
if abs(n) > 2:
|
||||||
|
while x < math.sqrt(n):
|
||||||
|
if n % 2 == 0:
|
||||||
|
return False
|
||||||
|
elif n % x == 0:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
x += 2
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
print(is_prime(4813))
|
||||||
91
thinkcspy/Chapter-8-Exercises/Python-files/Problem6.py
Normal file
91
thinkcspy/Chapter-8-Exercises/Python-files/Problem6.py
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
# Runestone.Academy thinkcspy course
|
||||||
|
# Chapter 8
|
||||||
|
# Problem 6
|
||||||
|
|
||||||
|
import random
|
||||||
|
import turtle
|
||||||
|
import math
|
||||||
|
|
||||||
|
def isInScreen(w, t, lBound, rBound, bBound, tBound):
|
||||||
|
turtleX = t.xcor()
|
||||||
|
turtleY = t.ycor()
|
||||||
|
|
||||||
|
stillIn = True
|
||||||
|
if turtleX > rBound or turtleX < lBound:
|
||||||
|
stillIn = False
|
||||||
|
if turtleY > tBound or turtleY < bBound:
|
||||||
|
stillIn = False
|
||||||
|
|
||||||
|
return stillIn
|
||||||
|
|
||||||
|
|
||||||
|
def randGoto(t, num1, num2):
|
||||||
|
return random.randrange(num1, num2)
|
||||||
|
|
||||||
|
|
||||||
|
def randPos(t, lBound, rBound, bBound, tBound):
|
||||||
|
xpos = randGoto(t, lBound, rBound)
|
||||||
|
ypos = randGoto(t, bBound, tBound)
|
||||||
|
t.up()
|
||||||
|
t.goto(xpos, ypos)
|
||||||
|
t.down()
|
||||||
|
|
||||||
|
|
||||||
|
def spaceBetween(num1, num2):
|
||||||
|
if math.sqrt(num1**2 + num2**2) < 5:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def randMove(wn, t1, t2, lBound, rBound, bBound, tBound):
|
||||||
|
randPos(t1, lBound, rBound, bBound, tBound)
|
||||||
|
randPos(t2, lBound, rBound, bBound, tBound)
|
||||||
|
|
||||||
|
while isInScreen(wn, t1, lBound, rBound, bBound, tBound) and isInScreen(wn, t2, lBound, rBound, bBound, tBound):
|
||||||
|
t1x = t1.xcor()
|
||||||
|
t1y = t1.ycor()
|
||||||
|
t2x = t2.xcor()
|
||||||
|
t2y = t2.ycor()
|
||||||
|
coin = random.randrange(0, 2)
|
||||||
|
|
||||||
|
if spaceBetween(t1x, lBound) or spaceBetween(t1x, rBound) or spaceBetween(t1y, bBound) or spaceBetween(t1y, tBound):
|
||||||
|
t1.right(170)
|
||||||
|
if spaceBetween(t2x, lBound) or spaceBetween(t2x, rBound) or spaceBetween(t2y, bBound) or spaceBetween(t2y, tBound):
|
||||||
|
t2.right(170)
|
||||||
|
if spaceBetween(t1x, t2x) or spaceBetween(t1y, t2y):
|
||||||
|
t1.left(170)
|
||||||
|
t2.right(170)
|
||||||
|
|
||||||
|
if coin == 0:
|
||||||
|
t1.left(random.randrange(1,180))
|
||||||
|
t2.left(random.randrange(1,180))
|
||||||
|
else:
|
||||||
|
t1.right(random.randrange(1,180))
|
||||||
|
t2.right(random.randrange(1,180))
|
||||||
|
|
||||||
|
t1.forward(50)
|
||||||
|
t2.forward(50)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
wn = turtle.Screen()
|
||||||
|
wn.bgcolor("lightblue")
|
||||||
|
|
||||||
|
x1 = -wn.window_width() / 2
|
||||||
|
x2 = wn.window_width() / 2
|
||||||
|
y1 = -wn.window_height() / 2
|
||||||
|
y2 = wn.window_height() / 2
|
||||||
|
|
||||||
|
ella = turtle.Turtle()
|
||||||
|
ella.color("pink")
|
||||||
|
ella.shape('turtle')
|
||||||
|
|
||||||
|
micah = turtle.Turtle()
|
||||||
|
micah.color("green")
|
||||||
|
micah.shape('turtle')
|
||||||
|
|
||||||
|
randMove(wn, ella, micah, x1, x2, y1, y2)
|
||||||
|
|
||||||
|
|
||||||
|
wn.exitonclick()
|
||||||
28
thinkcspy/Chapter-8-Exercises/Python-files/Problem8.py
Normal file
28
thinkcspy/Chapter-8-Exercises/Python-files/Problem8.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
# Runestone.Academy thinkcspy course
|
||||||
|
# Chapter 8
|
||||||
|
# Problem 8
|
||||||
|
|
||||||
|
import image
|
||||||
|
|
||||||
|
def greyScale (red, blue, green):
|
||||||
|
return (red + blue + green)/3
|
||||||
|
|
||||||
|
img = image.Image("luther.jpg")
|
||||||
|
win = image.ImageWin(img.getWidth(), img.getHeight())
|
||||||
|
img.draw(win)
|
||||||
|
img.setDelay(0) # setDelay(0) turns off animation
|
||||||
|
|
||||||
|
for row in range(img.getHeight()):
|
||||||
|
for col in range(img.getWidth()):
|
||||||
|
p = img.getPixel(col, row)
|
||||||
|
|
||||||
|
pixelColor = greyScale(p.getRed(), p.getGreen(), p.getBlue())
|
||||||
|
|
||||||
|
newblue = newgreen = newred = pixelColor
|
||||||
|
|
||||||
|
newpixel = image.Pixel(newred, newgreen, newblue)
|
||||||
|
|
||||||
|
img.setPixel(col, row, newpixel)
|
||||||
|
|
||||||
|
img.draw(win)
|
||||||
|
win.exitonclick()
|
||||||
34
thinkcspy/Chapter-8-Exercises/Python-files/Problem9.py
Normal file
34
thinkcspy/Chapter-8-Exercises/Python-files/Problem9.py
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
# Runestone.Academy thinkcspy course
|
||||||
|
# Chapter 8
|
||||||
|
# Problem 9
|
||||||
|
|
||||||
|
import image
|
||||||
|
|
||||||
|
def greyScale (red, blue, green):
|
||||||
|
return (red + blue + green)/3
|
||||||
|
|
||||||
|
def blackWhite (color):
|
||||||
|
if color >= 255 / 2:
|
||||||
|
return 255
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
img = image.Image("luther.jpg")
|
||||||
|
win = image.ImageWin(img.getWidth(), img.getHeight())
|
||||||
|
img.draw(win)
|
||||||
|
img.setDelay(0) # setDelay(0) turns off animation
|
||||||
|
|
||||||
|
for row in range(img.getHeight()):
|
||||||
|
for col in range(img.getWidth()):
|
||||||
|
p = img.getPixel(col, row)
|
||||||
|
|
||||||
|
pixelColor = greyScale(p.getRed(), p.getGreen(), p.getBlue())
|
||||||
|
|
||||||
|
newblue = newgreen = newred = blackWhite(pixelColor)
|
||||||
|
|
||||||
|
newpixel = image.Pixel(newred, newgreen, newblue)
|
||||||
|
|
||||||
|
img.setPixel(col, row, newpixel)
|
||||||
|
|
||||||
|
img.draw(win)
|
||||||
|
win.exitonclick()
|
||||||
Loading…
Reference in a new issue