Fix typos and add Python files
This commit is contained in:
parent
cddc649b7f
commit
776b96e52e
6 changed files with 215 additions and 3 deletions
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