Fix typos and add Python files
This commit is contained in:
parent
cddc649b7f
commit
776b96e52e
6 changed files with 215 additions and 3 deletions
|
|
@ -1,7 +1,7 @@
|
|||
# Exercises
|
||||
This lesson only had five problems, and thank goodness for that, because problem 4 really threw me for a loop. I've included all 5 here, because there are so few problems and because I genuinely found them all to be interesting problems.
|
||||
|
||||
Also I kept getting tripped up with how I interpreted the numbers from the files vs how Python interpreted them. Because they were coming from a file, those numbers were interpreted as strings, not numbers. You'd think that after my first encounter with this, I'd understand that, but I was literally still struggling with that on problem 5 lol.
|
||||
Also, I kept getting tripped up with how I interpreted the numbers from the files vs how Python interpreted them. Because they were coming from a file, those numbers were interpreted as strings, not numbers. You'd think that after my first encounter with this, I'd understand that, but I was literally still struggling with that on problem 5 lol.
|
||||
|
||||
Anyway, enough talk. Let's go through the exercises.
|
||||
|
||||
|
|
@ -395,7 +395,7 @@ Problem 5
|
|||
|
||||
> At the bottom of this page is a very long file called `mystery.txt` The lines of this file contain either the word UP or DOWN or a pair of numbers. UP and DOWN are instructions for a turtle to lift up or put down its tail. The pairs of numbers are some x,y coordinates. Write a program that reads the file `mystery.txt` and uses the turtle to draw the picture described by the commands and the set of points.
|
||||
|
||||
It's long so, I'll just link to the data in `mystery.txt` [here](Exercises/mystery.txt.md).
|
||||
It's long, so I'll just link to the data in `mystery.txt` [here](Exercises/mystery.txt.md).
|
||||
|
||||
Drawing the image was relatively straightforward. It was getting the world coordinates that was challenging, even though I literally just did the same thing in the previous problem.
|
||||
|
||||
|
|
@ -410,7 +410,7 @@ Anywho, here's my solution:
|
|||
# Chapter 11
|
||||
# Problem 5
|
||||
|
||||
import turtle
|
||||
import turtle
|
||||
|
||||
def getMinMax():
|
||||
#get world coordinates
|
||||
|
|
|
|||
12
thinkcspy/Chapter-11-Exercises/Python-files/Problem1.py
Normal file
12
thinkcspy/Chapter-11-Exercises/Python-files/Problem1.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 11
|
||||
# Problem 1
|
||||
|
||||
sdfile = open("studentdata.txt", "r")
|
||||
|
||||
for line in sdfile:
|
||||
student = line.split()
|
||||
if len(student[1:]) > 6:
|
||||
print(student[0])
|
||||
|
||||
sdfile.close()
|
||||
21
thinkcspy/Chapter-11-Exercises/Python-files/Problem2.py
Normal file
21
thinkcspy/Chapter-11-Exercises/Python-files/Problem2.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 11
|
||||
# Problem 2
|
||||
|
||||
sdfile = open("studentdata.txt", "r")
|
||||
|
||||
for line in sdfile:
|
||||
student = line.split()
|
||||
sum = 0
|
||||
avg = 0
|
||||
count = len(student[1:])
|
||||
|
||||
for grade in student:
|
||||
if grade != student[0]:
|
||||
sum += int(grade)
|
||||
|
||||
avg = sum/count
|
||||
|
||||
print(student[0], "-", avg)
|
||||
|
||||
sdfile.close()
|
||||
23
thinkcspy/Chapter-11-Exercises/Python-files/Problem3.py
Normal file
23
thinkcspy/Chapter-11-Exercises/Python-files/Problem3.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 11
|
||||
# Problem 3
|
||||
|
||||
sdfile = open("studentdata.txt", "r")
|
||||
|
||||
for line in sdfile:
|
||||
student = line.split()
|
||||
mingrade = student[1]
|
||||
maxgrade = student[1]
|
||||
|
||||
for grade in student:
|
||||
if grade != student[0]:
|
||||
if int(grade) < int(mingrade):
|
||||
mingrade = grade
|
||||
if int(grade) > int(maxgrade):
|
||||
maxgrade = grade
|
||||
|
||||
outputText = "{} - minimum score:{}; maximum score:{}".format(student[0], mingrade, maxgrade)
|
||||
|
||||
print(outputText)
|
||||
|
||||
sdfile.close()
|
||||
95
thinkcspy/Chapter-11-Exercises/Python-files/Problem4.py
Normal file
95
thinkcspy/Chapter-11-Exercises/Python-files/Problem4.py
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 11
|
||||
# Problem 4
|
||||
|
||||
import turtle
|
||||
|
||||
def slope():
|
||||
xlist = []
|
||||
ylist = []
|
||||
sumx = 0
|
||||
sumy = 0
|
||||
sumxy = 0
|
||||
sumxsq = 0
|
||||
count = 0 #n
|
||||
meanx = 0
|
||||
meany = 0
|
||||
slope = 0 #m
|
||||
|
||||
with open('labdata.txt') as lab:
|
||||
for line in lab:
|
||||
values = line.split()
|
||||
xlist += [int(values[0])]
|
||||
ylist += [int(values[1])]
|
||||
x = int(values[0])
|
||||
y = int(values[1])
|
||||
sumx += x
|
||||
sumy += y
|
||||
sumxy += x*y
|
||||
sumxsq += x**2
|
||||
count += 1
|
||||
|
||||
meanx = sumx / count
|
||||
meany = sumy / count
|
||||
|
||||
slope = (sumxy - count*meanx*meany) / (sumxsq - count*meanx**2)
|
||||
|
||||
plotRegression(xlist, ylist, meanx, meany, slope)
|
||||
|
||||
|
||||
|
||||
def plotRegression(xlist, ylist, meanx, meany, slope):
|
||||
#set world coordinates
|
||||
minx = xlist[0]
|
||||
miny = ylist[0]
|
||||
maxx = xlist[0]
|
||||
maxy = ylist[0]
|
||||
count = len(xlist)
|
||||
num = 0
|
||||
|
||||
while num < count:
|
||||
if xlist[num] < minx:
|
||||
minx = xlist[num]
|
||||
if xlist[num] > maxx:
|
||||
maxx = xlist[num]
|
||||
if ylist[num] < miny:
|
||||
miny = ylist[num]
|
||||
if ylist[num] > maxy:
|
||||
maxy = ylist[num]
|
||||
|
||||
num += 1
|
||||
|
||||
turtle.setworldcoordinates(minx-10, miny-10, maxx+10, maxy+10)
|
||||
|
||||
#plot points
|
||||
i = 0
|
||||
wn = turtle.Screen()
|
||||
wn.bgcolor("#222")
|
||||
ella = turtle.Turtle() #points from the labdata.txt
|
||||
ella.color("pink")
|
||||
maya = turtle.Turtle() #line of best fit (yplot)
|
||||
maya.color("lightblue")
|
||||
|
||||
for x in xlist:
|
||||
j = 0
|
||||
yplot = meany + slope*(x - meanx)
|
||||
|
||||
while not j:
|
||||
ella.up()
|
||||
ella.goto(x, ylist[i])
|
||||
ella.dot()
|
||||
|
||||
if i != 0:
|
||||
maya.goto(x, yplot)
|
||||
else:
|
||||
maya.up()
|
||||
maya.goto(x, yplot)
|
||||
maya.down()
|
||||
|
||||
j += 1
|
||||
|
||||
i += 1
|
||||
|
||||
wn.exitonclick()
|
||||
|
||||
slope()
|
||||
61
thinkcspy/Chapter-11-Exercises/Python-files/Problem5.py
Normal file
61
thinkcspy/Chapter-11-Exercises/Python-files/Problem5.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 11
|
||||
# Problem 5
|
||||
|
||||
import turtle
|
||||
|
||||
def getMinMax():
|
||||
#get world coordinates
|
||||
minx = 0
|
||||
miny = 0
|
||||
maxx = 0
|
||||
maxy = 0
|
||||
|
||||
with open('mystery.txt') as data:
|
||||
for line in data:
|
||||
values = line.split()
|
||||
if values[0] != "UP" and values[0] != "DOWN":
|
||||
minx = int(values[0])
|
||||
miny = int(values[1])
|
||||
maxx = int(values[0])
|
||||
maxy = int(values[1])
|
||||
break
|
||||
|
||||
for line in data:
|
||||
values = line.split()
|
||||
if values[0] != "UP" and values[0] != "DOWN":
|
||||
if int(values[0]) < minx:
|
||||
minx = int(values[0])
|
||||
if int(values[0]) > maxx:
|
||||
maxx = int(values[0])
|
||||
if int(values[1]) < miny:
|
||||
miny = int(values[1])
|
||||
if int(values[1]) > maxy:
|
||||
maxy = int(values[1])
|
||||
|
||||
draw(minx, miny, maxx, maxy)
|
||||
|
||||
|
||||
|
||||
def draw(minx, miny, maxx, maxy):
|
||||
turtle.setworldcoordinates(minx-10, miny-10, maxx+10, maxy+10)
|
||||
|
||||
wn = turtle.Screen()
|
||||
wn.bgcolor("#e0e0e0")
|
||||
ella = turtle.Turtle()
|
||||
ella.color("brown")
|
||||
|
||||
with open('mystery.txt') as data:
|
||||
for line in data:
|
||||
values = line.split()
|
||||
|
||||
if values[0] == "UP":
|
||||
ella.up()
|
||||
elif values[0] == "DOWN":
|
||||
ella.down()
|
||||
else:
|
||||
x = int(values[0])
|
||||
y = int(values[1])
|
||||
ella.goto(x,y)
|
||||
|
||||
getMinMax()
|
||||
Loading…
Reference in a new issue