Remove .md and image files and move python files into main chapter folders

This commit is contained in:
lolabird 2023-11-19 20:53:50 -07:00
commit 67806dab6a
159 changed files with 0 additions and 4562 deletions

View file

@ -1,248 +0,0 @@
# Exercises
Problem 4
---------
> Modify the turtle bar chart program from the previous chapter so that the bar for any value of 200 or more is filled with red, values between \[100 and 200) are filled yellow, and bars representing values less than 100 are filled green.
Back at it with the turtle bar! See the last problem in chapter 6 for context. 
This one was more involved than I expected. Plus there is some unexpected behavior that I haven't quite figured out. When I tried to assign color to `alex`, it would write every number except the one to the furthest right. I have no idea why, at least not yet. My Solution was just to not include `alex` in the `getColor` function. To be honest, it looks better that why, but it's still bothering me that the last number gets left out like that.
```text-x-python
# Runestone.Academy thinkcspy course
# Chapter 7
# Problem 4
import turtle
def getColor(tf, tw, height):
if height >= 200:
tf.fillcolor("red")
elif height >= 100:
tf.fillcolor("yellow")
else:
tf.fillcolor("green")
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")
wn.tracer(100)
tess = turtle.Turtle() # create tess and set some attributes
tess.color("black")
tess.pensize(3)
alex = turtle.Turtle()
for a in xs:
getColor(tess, alex, a)
drawBar(tess, a)
writeData(alex, a)
wn.exitonclick()
```
Result:
![](Exercises/image.png)
Problem 5
---------
> In the turtle bar chart program, what do you expect to happen if one or more of the data values in the list is negative? Go back and try it out. Change the program so that when it prints the text value for the negative bars, it puts the text above the base of the bar (on the 0 axis)
No new functions this time, but I did have to adjust the `writeData` function, the world coordinates and the data, itself, of course. 
```text-x-python
# Runestone.Academy thinkcspy course
# Chapter 7
# Problem 5
import turtle
def getColor(tf, tw, height):
if height >= 200:
tf.fillcolor("red")
elif height >= 100:
tf.fillcolor("yellow")
else:
tf.fillcolor("green")
def writeData(tt, height):
if height < 0:
writeHeight = 0
else:
writeHeight = height
tt.up()
tt.left(90)
tt.forward(writeHeight+10)
tt.right(90)
tt.forward(10)
tt.write(str(height))
tt.forward(30)
tt.right(90)
tt.forward(writeHeight+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
minheight = min(xs)-10
numbars = len(xs)
border = 10
wn = turtle.Screen() # Set up the window and its attributes
wn.setworldcoordinates(0-border, 0+minheight, 40*numbars+border, maxheight+border)
wn.bgcolor("lightblue")
wn.tracer(100)
tess = turtle.Turtle() # create tess and set some attributes
tess.color("black")
tess.pensize(3)
alex = turtle.Turtle()
for a in xs:
getColor(tess, alex, a)
drawBar(tess, a)
writeData(alex, a)
wn.exitonclick()
```
Result:
![](Exercises/1_image.png)
Problem 13
----------
> Implement the calculator for the date of Easter.
>
> The following algorithm computes the date for Easter Sunday for any year between 1900 to 2099.
>
> Ask the user to enter a year. Compute the following:
>
> 1. a = year % 19
> 2. b = year % 4
> 3. c = year % 7
> 4. d = (19 \* a + 24) % 30
> 5. e = (2 \* b + 4 \* c + 6 \* d + 5) % 7
> 6. dateofeaster = 22 + d + e
>
> Special note: The algorithm can give a date in April. Also, if the year is one of four special years (1954, 1981, 2049, or 2076) then subtract 7 from the date.
>
> Your program should print an error message if the user provides a date that is out of range.
We had to figure out the leap year in problem 12:
> 3 criteria must be taken into account to identify leap years:
>
> The year is evenly divisible by 4;
>
> If the year can be evenly divided by 100, it is NOT a leap year, unless;
>
> The year is also evenly divisible by 400. Then it is a leap year.
>
> Write a function that takes a year as a parameter and returns `True` if the year is a leap year, `False` otherwise.
```text-x-python
# Runestone.Academy thinkcspy course
# Chapter 7
# Problem 13
def isLeap(year):
# your code here
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
def easter(year):
a = year % 19
b = year % 4
c = year % 7
d = (19 * a + 24) % 30
e = (2 * b + 4 * c + 6 * d + 5) % 7
if year >= 1900 and year <= 2099:
if isLeap(year):
dateofeaster = 22 + d + e - 7
else:
dateofeaster = 22 + d + e
else:
dateofeaster = 0
return dateofeaster
def convertDate(year):
date = easter(year)
if date == 0:
print("Error: Date is out of range.")
elif date <= 30:
print("In", year, "Easter falls on March", date - 1, ".")
else:
print("In", year, "Easter falls on April", date - 31, ".")
userYear = int(input("Enter a year between 1900 and 2099."))
convertDate(userYear)
```
Result in range:
![](Exercises/2_image.png)
Result out of range:
![](Exercises/3_image.png)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

View file

@ -1,100 +0,0 @@
# 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:
![](Lab%20Approximating%20the%20Value%20of%20Pi/image.png)
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.