diff --git a/thinkcspy/Chapter-6-Exercises/Exercises.md b/thinkcspy/Chapter-6-Exercises/Exercises.md index 3287ee9..bd18261 100644 --- a/thinkcspy/Chapter-6-Exercises/Exercises.md +++ b/thinkcspy/Chapter-6-Exercises/Exercises.md @@ -1,4 +1,4 @@ -# Exercises +# Exercises Problem 1 --------- @@ -434,7 +434,7 @@ Problem 14 > Write a function called `mySqrt` that will approximate the square root of a number, call it n, by using Newton’s algorithm. Newton’s approach is an iterative guessing algorithm where the initial guess is n/2 and each subsequent guess is computed using the formula: newguess = (1/2) \* (oldguess + (n/oldguess)). -I'm adding this one here, because it took me a minute to figure out the most efficient way to compute. this. At first I had a a for loop, but that was taking way too many steps for the program to get to the right answer. Then I did a for loop with a break and that was better, but that was still taking the program too many steps.  +I'm adding this one here, because it took me a minute to figure out the most efficient way to compute. this. At first I had a for loop, but that was taking way too many steps for the program to get to the right answer. Then I did a for loop with a break and that was better, but that was still taking the program too many steps.  Finally, I realized that I could just create this program with a while loop and that drastically reduced the amount of steps the program had to go through. It still seems to be going through a few too any checks, but I think it's okay for my current skill level. Plus, it passed all the built in checks! @@ -584,7 +584,7 @@ And I did this: Technically, I did _solve_ the problem but was tired and didn't feel like dealing with the little line that appeared as a result of combing `write` and `fill` within the same function. But then we got this problem in the exercises lol and they specifically mentioned the “trickiness” of being able to write the data and fill the bars at the same time. Presumably because of that little extra line.  -So this time I actually did deal with the little line. Created a second turtle, `alex`, and a separate function `writeData`, for the `alex` to write with. Also changed the colors, because what even is that red blue green color combo?  +So this time I actually did deal with the little line. Created a second turtle, `alex`, and a separate function `writeData`, for `alex` to write with. Also changed the colors, because what even is that red blue green color combo?  Anyway, here is the code: diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem1.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem1.py new file mode 100644 index 0000000..c90836b --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem1.py @@ -0,0 +1,33 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 1 + +import turtle + +def drawSquare(t, sz): + """Get turtle t to draw a square of sz side""" + + for i in range(4): + t.forward(sz) + t.left(90) + +wn = turtle.Screen() +wn.bgcolor("lightgreen") + +alex = turtle.Turtle() +alex.color("pink") +alex.pensize(2) + + +def drawManySquares(tt, num): + for i in range(num): + drawSquare(tt, 20) + tt.up() + tt.forward(30) + tt.down() + + +drawManySquares(alex, 5) + + +wn.exitonclick() \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem10.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem10.py new file mode 100644 index 0000000..c4fd365 --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem10.py @@ -0,0 +1,34 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 10 + +import turtle + +def drawStar(t): + for i in range(5): + t.left(144) + t.forward(-100) + +def drawFiveStars(tt): + tt.up() + tt.goto(-180,50) + tt.down() + + for i in range(5): + drawStar(tt) + tt.up() + tt.forward(350) + tt.right(144) + tt.down() + +wn = turtle.Screen() +wn.bgcolor("tan") + +stella = turtle.Turtle() +stella.color("purple") +stella.pensize(2) +stella.speed(10) + +drawFiveStars(stella) + +wn.exitonclick() \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem11.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem11.py new file mode 100644 index 0000000..24ba979 --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem11.py @@ -0,0 +1,39 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 11 + +import turtle + +def drawStar(t, numSides, sideLength): + angle = 180 - 180/numSides + + t.up() + t.goto(sideLength/2,0) + t.down() + + for i in range(numSides): + t.left(angle) + t.forward(sideLength) + +def checkSides(tt, sides, length): + if sides >= 3 and sides % 2 != 0: + print("What a beautiful star!") + drawStar(tt, sides, length) + else: + print("Oops! Your star must have an ODD number of at least 3 sides. Let's try that again!") + + +wn = turtle.Screen() +wn.bgcolor("tan") + +stella = turtle.Turtle() +stella.color("purple") +stella.pensize(2) +stella.speed(0) + +a = int(input("Help Stella draw a star! How many points does the star have?")) +b = int(input("How long is each side?")) + +checkSides(stella, a, b) + +wn.exitonclick() \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem12.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem12.py new file mode 100644 index 0000000..f592e3e --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem12.py @@ -0,0 +1,28 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 12 + +import turtle + +def drawSprite(t, numSides, legLength): + angle = 360/numSides + + for i in range(numSides): + t.forward(legLength) + t.up() + t.forward(-legLength) + t.right(angle) + t.down() + + +wn = turtle.Screen() + +wn.bgcolor("tan") + +avery = turtle.Turtle() +avery.color("brown") + +drawSprite(avery, 15, 120) + + +wn.exitonclick() \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem13.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem13.py new file mode 100644 index 0000000..b94abc0 --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem13.py @@ -0,0 +1,14 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 13 + +def sumTo(n): + # your code here + runningTotal = 0 + + for counter in range(n): + runningTotal = runningTotal + (n+1)/2 + + return runningTotal + +print(sumTo(10)) \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem14.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem14.py new file mode 100644 index 0000000..23b51ea --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem14.py @@ -0,0 +1,14 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 14 + +def mySqrt(n): + # your code here + oldGuess = n/2 + + while oldGuess**2 != n: + oldGuess = (1/2)*(oldGuess+(n/oldGuess)) + + return oldGuess + +print(mySqrt(10000)) \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem15.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem15.py new file mode 100644 index 0000000..4702c82 --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem15.py @@ -0,0 +1,18 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 15 + +def myPi(iters): + # Calculate an approximation of PI using the Leibniz + # approximation with iters number of iterations + + # your code here + apprPi = 0 + + for i in range(iters): + term = (-1)**i/(2*i+1) + apprPi += term + + return apprPi * 4 + +print(myPi(1000000)) \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem16.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem16.py new file mode 100644 index 0000000..491a82f --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem16.py @@ -0,0 +1,20 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 16 + +import math + +def myPi(iters): + # Calculate an approximation of PI using the Madhava + # approximation with iters number of iterations + + #your code here + apprPi = 0 + + for i in range(iters): + term = (-1)**i/((2*i+1)*3**i) + apprPi += term + + return apprPi * math.sqrt(12) + +print(myPi(10)) \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem17.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem17.py new file mode 100644 index 0000000..efdd8b9 --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem17.py @@ -0,0 +1,32 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 17 + +import turtle + +def drawSprite(t, numSides, legLength): + angle = 360/numSides + + for i in range(numSides): + t.forward(legLength) + t.up() + t.forward(-legLength) + t.right(angle) + t.down() + +def fancySquare(tt, sz): + """Get turtle t to draw a square of sz side""" + + for i in range(4): + drawSprite(tt, int(sz/10), int(sz/20)) + tt.forward(sz) + tt.left(90) + +wn = turtle.Screen() +wn.bgcolor("lightblue") + +alex = turtle.Turtle() +alex.color("maroon") +alex.pensize(2) + +fancySquare(alex, 100) \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem18.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem18.py new file mode 100644 index 0000000..64278d4 --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem18.py @@ -0,0 +1,56 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 18 + +import turtle + +def writeData(tt, height): + tt.up() + tt.left(90) + tt.forward(height+10) + tt.right(90) + tt.forward(10) + tt.write(str(height)) + tt.forward(30) + tt.right(90) + tt.forward(height+10) + tt.left(90) + + +def drawBar(t, height): + """ Get turtle t to draw one bar, of height. """ + t.begin_fill() # start filling this shape + t.left(90) + t.forward(height) + t.right(90) + t.forward(40) + t.right(90) + t.forward(height) + t.left(90) + t.end_fill() # stop filling this shape + + + +xs = [48, 117, 200, 240, 160, 260, 220] # here is the data +maxheight = max(xs)+10 +numbars = len(xs) +border = 10 + +wn = turtle.Screen() # Set up the window and its attributes +wn.setworldcoordinates(0-border, 0-border, 40*numbars+border, maxheight+border) +wn.bgcolor("lightblue") + +tess = turtle.Turtle() # create tess and set some attributes +tess.color("purple") +tess.fillcolor("magenta") +tess.pensize(3) + +alex = turtle.Turtle() +alex.color("magenta") + + +for a in xs: + drawBar(tess, a) + writeData(alex, a) + +wn.exitonclick() diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem2.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem2.py new file mode 100644 index 0000000..ca33de8 --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem2.py @@ -0,0 +1,36 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 2 + +import turtle + +def drawSquare(t, sz): + """Get turtle t to draw a square of sz side""" + + for i in range(4): + t.forward(sz) + t.left(90) + +wn = turtle.Screen() +wn.bgcolor("lightgreen") + +alex = turtle.Turtle() +alex.color("pink") +alex.pensize(2) + + +def drawStackedSquares(tt, num): + for i in range(num): + drawSquare(tt, 20*(i+1)) + tt.up() + tt.backward(10) + tt.left(90) + tt.backward(10) + tt.right(90) + tt.down() + + +drawStackedSquares(alex, 5) + + +wn.exitonclick() \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem3.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem3.py new file mode 100644 index 0000000..b6e50ed --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem3.py @@ -0,0 +1,25 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 3 + +import turtle + +def drawPolygon(t, sideLength, numSides): + """Make turtle t draw a polygon.""" + angle = 360/numSides + + for i in range(numSides): + t.forward(sideLength) + t.left(angle) + + +wn = turtle.Screen() +wn.bgcolor("lightgreen") + +tess = turtle.Turtle() +tess.color("pink") +tess.pensize(2) + +drawPolygon(tess,50,8) + +wn.exitonclick() \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem4.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem4.py new file mode 100644 index 0000000..933d985 --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem4.py @@ -0,0 +1,32 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 4 + +import turtle + +def drawSquare(t, sz): + """Get turtle t to draw a square of sz side""" + + for i in range(4): + t.forward(sz) + t.left(90) + +wn = turtle.Screen() +wn.bgcolor("lightgreen") + +alex = turtle.Turtle() +alex.color("blue") +alex.pensize(2) +alex.speed(10) + + +def drawCircleSquares(tt, num): + for i in range(num): + drawSquare(tt, 80) + tt.right(15) + + +drawCircleSquares(alex, 25) + + +wn.exitonclick() \ No newline at end of file diff --git a/thinkcspy/Chapter-6-Exercises/Python-files/problem5.py b/thinkcspy/Chapter-6-Exercises/Python-files/problem5.py new file mode 100644 index 0000000..1c28829 --- /dev/null +++ b/thinkcspy/Chapter-6-Exercises/Python-files/problem5.py @@ -0,0 +1,38 @@ +# Runestone.Academy thinkcspy course +# Chapter 6 +# Problem 5 + +import turtle + +def drawSquareSpiral(t, sz, num, angle): + """Get turtle t to draw a square of sz side""" + + for i in range(num): + t.forward(sz*i) + t.left(angle) + +wn = turtle.Screen() +wn.setworldcoordinates(-300,-300,300,300) +wn.bgcolor("lightgreen") + + +alex = turtle.Turtle() +alex.color("blue") +alex.speed(0) +alex.up() +alex.goto(-150,0) +alex.down() + +tess = turtle.Turtle() +tess.color("purple") +tess.speed(0) +tess.up() +tess.goto(150,0) +tess.down() + +drawSquareSpiral(alex,2,100,90) + +drawSquareSpiral(tess,2,100,91) + + +wn.exitonclick() \ No newline at end of file