From 5c38f5ef86869c06bde9e6eb2708d58eba463f66 Mon Sep 17 00:00:00 2001 From: lolabird Date: Fri, 29 Sep 2023 12:39:26 -0600 Subject: [PATCH] Add Chapter 8 python files --- .../Python-files/Problem10.py | 31 +++++++ .../Python-files/Problem2.py | 20 ++++ .../Python-files/Problem3.py | 24 +++++ .../Python-files/Problem6.py | 91 +++++++++++++++++++ .../Python-files/Problem8.py | 28 ++++++ .../Python-files/Problem9.py | 34 +++++++ 6 files changed, 228 insertions(+) create mode 100644 thinkcspy/Chapter-8-Exercises/Python-files/Problem10.py create mode 100644 thinkcspy/Chapter-8-Exercises/Python-files/Problem2.py create mode 100644 thinkcspy/Chapter-8-Exercises/Python-files/Problem3.py create mode 100644 thinkcspy/Chapter-8-Exercises/Python-files/Problem6.py create mode 100644 thinkcspy/Chapter-8-Exercises/Python-files/Problem8.py create mode 100644 thinkcspy/Chapter-8-Exercises/Python-files/Problem9.py diff --git a/thinkcspy/Chapter-8-Exercises/Python-files/Problem10.py b/thinkcspy/Chapter-8-Exercises/Python-files/Problem10.py new file mode 100644 index 0000000..485eba9 --- /dev/null +++ b/thinkcspy/Chapter-8-Exercises/Python-files/Problem10.py @@ -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() \ No newline at end of file diff --git a/thinkcspy/Chapter-8-Exercises/Python-files/Problem2.py b/thinkcspy/Chapter-8-Exercises/Python-files/Problem2.py new file mode 100644 index 0000000..5950fab --- /dev/null +++ b/thinkcspy/Chapter-8-Exercises/Python-files/Problem2.py @@ -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) \ No newline at end of file diff --git a/thinkcspy/Chapter-8-Exercises/Python-files/Problem3.py b/thinkcspy/Chapter-8-Exercises/Python-files/Problem3.py new file mode 100644 index 0000000..959123c --- /dev/null +++ b/thinkcspy/Chapter-8-Exercises/Python-files/Problem3.py @@ -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)) \ No newline at end of file diff --git a/thinkcspy/Chapter-8-Exercises/Python-files/Problem6.py b/thinkcspy/Chapter-8-Exercises/Python-files/Problem6.py new file mode 100644 index 0000000..f1265cd --- /dev/null +++ b/thinkcspy/Chapter-8-Exercises/Python-files/Problem6.py @@ -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() \ No newline at end of file diff --git a/thinkcspy/Chapter-8-Exercises/Python-files/Problem8.py b/thinkcspy/Chapter-8-Exercises/Python-files/Problem8.py new file mode 100644 index 0000000..f1cbea3 --- /dev/null +++ b/thinkcspy/Chapter-8-Exercises/Python-files/Problem8.py @@ -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() \ No newline at end of file diff --git a/thinkcspy/Chapter-8-Exercises/Python-files/Problem9.py b/thinkcspy/Chapter-8-Exercises/Python-files/Problem9.py new file mode 100644 index 0000000..69b6a20 --- /dev/null +++ b/thinkcspy/Chapter-8-Exercises/Python-files/Problem9.py @@ -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() \ No newline at end of file