Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3 - Include docstrings. 2. (a) write 100 random points in 2-space to a fi

ID: 3739231 • Letter: P

Question

Python 3 - Include docstrings.

2. (a) write 100 random points in 2-space to a file named 'cloud.pt'; each random point should lie in the square bounded by (-300,-300) and (300,300); all point coordinates should be floats (b) read the data in this file into a list of points; each point should be stored in a float 2-tuple; so the list of points is a list of 2-tuples: each element of the list is a 2-tuple; for example, the beginning of the list may be (100.1, -3.4), (-1.2, 2.3), .. (c) draw these points (called a point cloud) using turtle graphics (d) (advanced) count the number of points inside the circle of radius 300 at the origin

Explanation / Answer

import turtle
t = turtle.Turtle()
import math

def is_inside_circle(x, y):
d = math.sqrt(x*x + y*y)
return not d > 300

points = []
points_inside_circle = 0

file = open('cloud.pt')
for line in file.readlines():
x,y = line.split(' ')
points.append((float(x),float(y)))

for x,y in points:
if is_inside_circle(x, y):
points_inside_circle += 1
t.goto(x, y)
t.dot(5, "Blue")

print 'Total Points: ', len(points)
print 'Points inside circle: ', points_inside_circle