Skip to main content

Exercises 11.10 Exercises

1.

The following sample file called studentdata.txt contains one line for each student in an imaginary class. The student’s name is the first thing on each line, followed by some exam scores. The number of scores might be different for each student.
Using the text file studentdata.txt write a program that prints out the names of students that have more than six quiz scores.
Solution.
f = open("studentdata.txt", "r")

for aline in f:
    items = aline.split()
    if len(items[1:]) > 6:
        print(items[0])

f.close()

2.

Using the text file studentdata.txt (shown in exercise 1) write a program that calculates the average grade for each student, and print out the student’s name along with their average grade.

3.

Using the text file studentdata.txt (shown in exercise 1) write a program that calculates the minimum and maximum score for each student. Print out their name as well.
Solution.
f = open("studentdata.txt", "r")

for aline in f:
    items = aline.split()
    for i in range(len(items[1:])):
        #converting scores from strings to integers
        items[i+1] = int(items[i+1]
    print(items[0], "max is", max(items[1:]), "min is", min(items[1:]))

f.close()

4.

Here is a file called labdata.txt that contains some sample data from a lab experiment.
Interpret the data file labdata.txt such that each line contains a an x,y coordinate pair. Write a function called plotRegression that reads the data from this file and uses a turtle to plot those points and a best fit line according to the following formulas:
y = \bar{y} + m(x - \bar{x})
m = \frac{\sum{x_iy_i - n\bar{x}\bar{y}}}{\sum{x_i^2}-n\bar{x}^2}
where \bar{x} is the mean of the x-values, \bar{y} is the mean of the y- values and n is the number of points. If you are not familiar with the mathematical \sum it is the sum operation. For example \sum{x_i} means to add up all the x values.
Your program should analyze the points and correctly scale the window using setworldcoordinates so that that each point can be plotted. Then you should draw the best fit line, in a different color, through the points.

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.
Solution.
import turtle

t = turtle.Turtle()
wn = turtle.Screen()
wn.setworldcoordinates(-300, -300, 300, 300)

f = open("mystery.txt", "r")

for aline in f:
    items = aline.split()
    if items[0] == "UP":
        t.up()
    else:
        if items[0] == "DOWN":
            t.down()
        else:
            # must be coords
            t.goto(int(items[0]), int(items[1]))

f.close()
wn.exitonclick()
Here is the mystery.txt file:
You have attempted of activities on this page.