Question: Rewrite the distance function from Section 5.2 so that it takes two Po
ID: 3619798 • Letter: Q
Question
Question: Rewrite the distance function from Section 5.2 sothat it takes two Points as arguments instead of four numbers.
--
Reading for this question is below:
---
Instances as arguments:
You can pass an instance as an argument in the usual way. For example:
def printPoint(p):
print ’(’ + str(p.x) + ’, ’ + str(p.y) + ’)’
printPoint takes a point as an argument and displays it in the standard format.
If you call printPoint(blank), the output is (3.0, 4.0). Question: Rewrite the distance function from Section 5.2 so
that it takes two Points as arguments instead of four numbers.
--
Reading for this question is below:
---
Instances as arguments:
You can pass an instance as an argument in the usual way. For example:
def printPoint(p):
print ’(’ + str(p.x) + ’, ’ + str(p.y) + ’)’
printPoint takes a point as an argument and displays it in the standard format.
If you call printPoint(blank), the output is (3.0, 4.0).
Explanation / Answer
class point:x = 0;
y = 0;
def printPoint(p):
print '(' + str(p.x) + ',' + str(p.y) + ')';
from math import sqrt;
def distance(p1,p2):
result = sqrt(((p1.x-p2.x)*(p1.x-p2.x))+((p1.x-p2.x)*(p1.x-p2.x)));
print result;
p1 = point();
p2 = point();
p1.y = 12;
p2.x = 5;
p2.y = 7;
distance(p1,p2);
raw_input();
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.