From 776b96e52ecf398bded88f24c58a47415c273d62 Mon Sep 17 00:00:00 2001 From: lolabird Date: Fri, 27 Oct 2023 15:59:23 -0600 Subject: [PATCH] Fix typos and add Python files --- thinkcspy/Chapter-11-Exercises/Exercises.md | 6 +- .../Python-files/Problem1.py | 12 +++ .../Python-files/Problem2.py | 21 ++++ .../Python-files/Problem3.py | 23 +++++ .../Python-files/Problem4.py | 95 +++++++++++++++++++ .../Python-files/Problem5.py | 61 ++++++++++++ 6 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 thinkcspy/Chapter-11-Exercises/Python-files/Problem1.py create mode 100644 thinkcspy/Chapter-11-Exercises/Python-files/Problem2.py create mode 100644 thinkcspy/Chapter-11-Exercises/Python-files/Problem3.py create mode 100644 thinkcspy/Chapter-11-Exercises/Python-files/Problem4.py create mode 100644 thinkcspy/Chapter-11-Exercises/Python-files/Problem5.py diff --git a/thinkcspy/Chapter-11-Exercises/Exercises.md b/thinkcspy/Chapter-11-Exercises/Exercises.md index 34a52ed..8df8365 100644 --- a/thinkcspy/Chapter-11-Exercises/Exercises.md +++ b/thinkcspy/Chapter-11-Exercises/Exercises.md @@ -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 diff --git a/thinkcspy/Chapter-11-Exercises/Python-files/Problem1.py b/thinkcspy/Chapter-11-Exercises/Python-files/Problem1.py new file mode 100644 index 0000000..280425d --- /dev/null +++ b/thinkcspy/Chapter-11-Exercises/Python-files/Problem1.py @@ -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() \ No newline at end of file diff --git a/thinkcspy/Chapter-11-Exercises/Python-files/Problem2.py b/thinkcspy/Chapter-11-Exercises/Python-files/Problem2.py new file mode 100644 index 0000000..fe251cf --- /dev/null +++ b/thinkcspy/Chapter-11-Exercises/Python-files/Problem2.py @@ -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() \ No newline at end of file diff --git a/thinkcspy/Chapter-11-Exercises/Python-files/Problem3.py b/thinkcspy/Chapter-11-Exercises/Python-files/Problem3.py new file mode 100644 index 0000000..00ea00f --- /dev/null +++ b/thinkcspy/Chapter-11-Exercises/Python-files/Problem3.py @@ -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() \ No newline at end of file diff --git a/thinkcspy/Chapter-11-Exercises/Python-files/Problem4.py b/thinkcspy/Chapter-11-Exercises/Python-files/Problem4.py new file mode 100644 index 0000000..1f93acd --- /dev/null +++ b/thinkcspy/Chapter-11-Exercises/Python-files/Problem4.py @@ -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() \ No newline at end of file diff --git a/thinkcspy/Chapter-11-Exercises/Python-files/Problem5.py b/thinkcspy/Chapter-11-Exercises/Python-files/Problem5.py new file mode 100644 index 0000000..dd25491 --- /dev/null +++ b/thinkcspy/Chapter-11-Exercises/Python-files/Problem5.py @@ -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() \ No newline at end of file