Add labs files for Chapters 7 and 8
This commit is contained in:
parent
de0699ead3
commit
af1eb1c281
6 changed files with 368 additions and 0 deletions
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
|
|
@ -0,0 +1,100 @@
|
|||
# Lab: Approximating the Value of Pi
|
||||
In this lab, our task was to simulate throwing darts at a 2x2 board, count how many landed in the circle out of the total amount and then approximate pi based on that.
|
||||
|
||||
This is the code they started me off with:
|
||||
|
||||
```text-x-python
|
||||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 7
|
||||
# Lab
|
||||
|
||||
import turtle
|
||||
import math
|
||||
import random
|
||||
|
||||
fred = turtle.Turtle()
|
||||
|
||||
wn = turtle.Screen()
|
||||
wn.setworldcoordinates(-1,-1,1,1)
|
||||
|
||||
fred.up()
|
||||
|
||||
numdarts = 10
|
||||
for i in range(numdarts):
|
||||
randx = random.random()
|
||||
randy = random.random()
|
||||
|
||||
x =
|
||||
y =
|
||||
|
||||
wn.exitonclick()
|
||||
```
|
||||
|
||||
My additions and changes to the above code:
|
||||
|
||||
```text-x-python
|
||||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 7
|
||||
# Lab
|
||||
|
||||
import turtle
|
||||
#import math
|
||||
import random
|
||||
|
||||
|
||||
fred = turtle.Turtle()
|
||||
clara = turtle.Turtle()
|
||||
|
||||
wn = turtle.Screen()
|
||||
wn.setworldcoordinates(-1,-1,1,1)
|
||||
wn.bgcolor("tan")
|
||||
wn.tracer(100)
|
||||
|
||||
clara.dot()
|
||||
print(clara.pos())
|
||||
clara.up()
|
||||
clara.goto(0,1)
|
||||
clara.down()
|
||||
clara.circle(-1)
|
||||
print(clara.pos())
|
||||
print(clara.distance(0.0,0.0))
|
||||
|
||||
fred.up()
|
||||
|
||||
numdarts = 100000
|
||||
insideCount = 0
|
||||
|
||||
for i in range(numdarts):
|
||||
randx = random.random()*2-1
|
||||
randy = random.random()*2-1
|
||||
|
||||
fred.goto(randx,randy)
|
||||
dist = fred.distance(0.0, 0.0)
|
||||
|
||||
if dist < 1:
|
||||
color = "red"
|
||||
insideCount += 1
|
||||
place = "in"
|
||||
else:
|
||||
color = "blue"
|
||||
place = "out"
|
||||
|
||||
fred.down()
|
||||
fred.dot(10,color)
|
||||
fred.up()
|
||||
|
||||
|
||||
mypi = 4 * (insideCount / numdarts)
|
||||
print(insideCount,"out of", numdarts, "darts made it into the circle.")
|
||||
print("The value of Pi based on the numbers number of darts that made it into the circle is", mypi, ".")
|
||||
|
||||
wn.exitonclick()
|
||||
```
|
||||
|
||||
Result:
|
||||
|
||||

|
||||
|
||||
Order really does matter.. My first solution to this lab was mostly fine, except that the dots were being colored wrong. Some dots inside the circle were blue and some outside the circle were red. It was supposed to be the other way around.
|
||||
|
||||
Initially I had fred go to his position after the if else block, but the problem was, the distance was being measured _before_ he moved. So distance was based on the previous loop's coordinates and coloring the darts based on those rather than the current loop's points. It took me a minute but I finally figured out that I needed to position fred _before_ measuring the distance between fred's current position and the center.
|
||||
56
thinkcspy/Chapter-7-Exercises/Python-files/Lab.py
Normal file
56
thinkcspy/Chapter-7-Exercises/Python-files/Lab.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 7
|
||||
# Lab
|
||||
|
||||
import turtle
|
||||
#import math
|
||||
import random
|
||||
|
||||
|
||||
fred = turtle.Turtle()
|
||||
clara = turtle.Turtle()
|
||||
|
||||
wn = turtle.Screen()
|
||||
wn.setworldcoordinates(-1,-1,1,1)
|
||||
wn.bgcolor("tan")
|
||||
wn.tracer(100)
|
||||
|
||||
clara.dot()
|
||||
print(clara.pos())
|
||||
clara.up()
|
||||
clara.goto(0,1)
|
||||
clara.down()
|
||||
clara.circle(-1)
|
||||
print(clara.pos())
|
||||
print(clara.distance(0.0,0.0))
|
||||
|
||||
fred.up()
|
||||
|
||||
numdarts = 100000
|
||||
insideCount = 0
|
||||
|
||||
for i in range(numdarts):
|
||||
randx = random.random()*2-1
|
||||
randy = random.random()*2-1
|
||||
|
||||
fred.goto(randx,randy)
|
||||
dist = fred.distance(0.0, 0.0)
|
||||
|
||||
if dist < 1:
|
||||
color = "red"
|
||||
insideCount += 1
|
||||
place = "in"
|
||||
else:
|
||||
color = "blue"
|
||||
place = "out"
|
||||
|
||||
fred.down()
|
||||
fred.dot(10,color)
|
||||
fred.up()
|
||||
|
||||
|
||||
mypi = 4 * (insideCount / numdarts)
|
||||
print(insideCount,"out of", numdarts, "darts made it into the circle.")
|
||||
print("The value of Pi based on the numbers number of darts that made it into the circle is", mypi, ".")
|
||||
|
||||
wn.exitonclick()
|
||||
|
|
@ -0,0 +1,125 @@
|
|||
# Lab: Experimenting With the 3n+1 Sequence
|
||||
Instructions
|
||||
------------
|
||||
|
||||
In this lab my job was to take the initial code, graph the iterations and find the max number of iterations from one of the 50 sequence starts.
|
||||
|
||||
Initial Code
|
||||
------------
|
||||
|
||||
```text-x-python
|
||||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 8
|
||||
# Lab
|
||||
|
||||
def seq3np1(n):
|
||||
""" Print the 3n+1 sequence from n, terminating when it reaches 1."""
|
||||
|
||||
while n != 1:
|
||||
print(n)
|
||||
if n % 2 == 0: # n is even
|
||||
n = n // 2
|
||||
else: # n is odd
|
||||
n = n * 3 + 1
|
||||
print(n) # the last print is 1
|
||||
```
|
||||
|
||||
My Code
|
||||
-------
|
||||
|
||||
```text-x-python
|
||||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 8
|
||||
# Lab
|
||||
|
||||
import turtle
|
||||
|
||||
border = 10
|
||||
|
||||
def writeData(tt, itr, height):
|
||||
tt.goto(0-border, 0-border)
|
||||
tt.up()
|
||||
|
||||
if height >= 100:
|
||||
tt.left(90)
|
||||
tt.forward(height+20)
|
||||
tt.right(90)
|
||||
tt.forward(itr*39)
|
||||
tt.write(str(height), move=False, align='center', font=('Times New Roman', 30, 'normal'))
|
||||
tt.forward(70)
|
||||
tt.write(str(itr), move=False, align='center', font=('Times New Roman', 30, 'normal'))
|
||||
else: # this is here so the last bar actually fills
|
||||
tt.left(90)
|
||||
tt.forward(height)
|
||||
tt.right(90)
|
||||
tt.forward(itr*40)
|
||||
tt.right(90)
|
||||
tt.forward(height)
|
||||
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
|
||||
|
||||
|
||||
def seq3np1(n):
|
||||
""" Print the 3n+1 sequence from n, terminating when it reaches 1."""
|
||||
count = 1
|
||||
|
||||
while n != 1:
|
||||
count += 1
|
||||
|
||||
#print(n)
|
||||
if n % 2 == 0: # n is even
|
||||
n = n // 2
|
||||
else: # n is odd
|
||||
n = n * 3 + 1
|
||||
#print(n) # the last print is 1
|
||||
#print(count)
|
||||
return count
|
||||
|
||||
|
||||
wn = turtle.Screen() # Set up the window and its attributes
|
||||
wn.setworldcoordinates(0-border, 0-border, 40*50+border, 150)
|
||||
wn.bgcolor("lightblue")
|
||||
wn.tracer(100)
|
||||
|
||||
tess = turtle.Turtle() # create tess and set some attributes
|
||||
tess.color("purple")
|
||||
tess.fillcolor("magenta")
|
||||
tess.pensize(3)
|
||||
|
||||
alex = turtle.Turtle()
|
||||
alex.color("purple")
|
||||
|
||||
maxSoFar = 0
|
||||
|
||||
for i in range(50):
|
||||
start = i + 1
|
||||
result = seq3np1(start)
|
||||
|
||||
#print("Number of iterations to get to '1' from", start, ":", result)
|
||||
|
||||
drawBar(tess, result)
|
||||
writeData(alex, start, result)
|
||||
|
||||
if result > maxSoFar:
|
||||
maxSoFar = result
|
||||
|
||||
print("The max number of iterations was", maxSoFar, "from sequence start", start)
|
||||
```
|
||||
|
||||
Result
|
||||
------
|
||||
|
||||

|
||||
|
||||
The numbers are not very readable, but it gets the job done.
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
87
thinkcspy/Chapter-8-Exercises/Python-files/Lab.py
Normal file
87
thinkcspy/Chapter-8-Exercises/Python-files/Lab.py
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 8
|
||||
# Lab
|
||||
|
||||
import turtle
|
||||
|
||||
border = 10
|
||||
|
||||
def writeData(tt, itr, height):
|
||||
tt.goto(0-border, 0-border)
|
||||
tt.up()
|
||||
|
||||
if height >= 100:
|
||||
tt.left(90)
|
||||
tt.forward(height+20)
|
||||
tt.right(90)
|
||||
tt.forward(itr*39)
|
||||
tt.write(str(height), move=False, align='center', font=('Times New Roman', 30, 'normal'))
|
||||
tt.forward(70)
|
||||
tt.write(str(itr), move=False, align='center', font=('Times New Roman', 30, 'normal'))
|
||||
else: # this is here so the last bar actually fills
|
||||
tt.left(90)
|
||||
tt.forward(height)
|
||||
tt.right(90)
|
||||
tt.forward(itr*40)
|
||||
tt.right(90)
|
||||
tt.forward(height)
|
||||
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
|
||||
|
||||
|
||||
def seq3np1(n):
|
||||
""" Print the 3n+1 sequence from n, terminating when it reaches 1."""
|
||||
count = 1
|
||||
|
||||
while n != 1:
|
||||
count += 1
|
||||
|
||||
#print(n)
|
||||
if n % 2 == 0: # n is even
|
||||
n = n // 2
|
||||
else: # n is odd
|
||||
n = n * 3 + 1
|
||||
#print(n) # the last print is 1
|
||||
#print(count)
|
||||
return count
|
||||
|
||||
|
||||
wn = turtle.Screen() # Set up the window and its attributes
|
||||
wn.setworldcoordinates(0-border, 0-border, 40*50+border, 150)
|
||||
wn.bgcolor("lightblue")
|
||||
wn.tracer(100)
|
||||
|
||||
tess = turtle.Turtle() # create tess and set some attributes
|
||||
tess.color("purple")
|
||||
tess.fillcolor("magenta")
|
||||
tess.pensize(3)
|
||||
|
||||
alex = turtle.Turtle()
|
||||
alex.color("purple")
|
||||
|
||||
maxSoFar = 0
|
||||
|
||||
for i in range(50):
|
||||
start = i + 1
|
||||
result = seq3np1(start)
|
||||
|
||||
#print("Number of iterations to get to '1' from", start, ":", result)
|
||||
|
||||
drawBar(tess, result)
|
||||
writeData(alex, start, result)
|
||||
|
||||
if result > maxSoFar:
|
||||
maxSoFar = result
|
||||
|
||||
print("The max number of iterations was", maxSoFar, "from sequence start", start)
|
||||
Loading…
Reference in a new issue