1

I am trying to write code to solve this python exercise: I must use the 'math' library, sqrt and possibly pow functions.

"The distance between two points x and y is the square root of the sum of squared differences along each dimension of x and y.

"Create a function that takes two vectors and outputs the distance between them.

x = (0,0) y = (1,1)"

So far I've tried this - which certainly hasn't worked.

x = (0,0)
y = (1,1)
(c1, c2) = x
(c3, c4) = y
math.sqrt(sum((c1,**2)(c2,**2)(c3,**2)(c4,**2)))
File "<ipython-input-14-ac0f3dc1fdeb>", line 1
    math.sqrt(sum((c1,**2)(c2,**2)(c3,**2)(c4,**2)))
                       ^
SyntaxError: invalid syntax
if c1 < c3:
    difference1 = c3-c1
    print(difference1)

1

... not even sure if that's the kind of calculation I should be working with.

def distance(x, y):

ummm... I expect the function starts by unpacking the tuples! But not sure how to write the rest of it, or cleanly.

I'm a beginner programmer & not a mathematician so I may be wrong in more than one sense... This exercise is from this HarvardX course: 'Using Python for Research'.

It's OK to search for solutions via StackOverflow for learning on this course... not cheating to ask for pointers.

Many thanks for any ideas! I will keep searching around.

gogaz
  • 2,323
  • 2
  • 23
  • 31
avwinter
  • 83
  • 2
  • 10
  • Btw - I initially tried this without the commas after c1, c2, etc - but that didn't work either. Aware that's really messy formatting. – avwinter Aug 31 '18 at 16:28
  • You're sort of on the right track. I'm not really sure what you're going for with `(c1,**2)(c2,**2)(c3,**2)(c4,**2)`, so it's hard to phrase this as a hint. Try this: `sum([(c1-c2)**2,(c3-c4)**2])` – mypetlion Aug 31 '18 at 16:41
  • `math.sqrt((x[0]-y[0])**2 + (x[1]-y[1])**2)` should solve your issue – Onyambu Aug 31 '18 at 16:43
  • @Onyambu That's not the equation. – mypetlion Aug 31 '18 at 16:45
  • @mypetlion what do you mean its not the equation? – Onyambu Aug 31 '18 at 16:46
  • @Onyambu It's not `(x[0] - y[0])**2 + (x[1]-y[1])**2`, it's `(x[0] - x[1])**2 + (y[0] - y[1])**2` – mypetlion Aug 31 '18 at 16:49
  • 1
    @mypetlion its usually the first value of the first point subtract the first value of the second point. So here the first point is called x, so the first value of this point will be x[0]. then the first value of the second point is y[0]. I understand what you mean. but that only occurs when we have (x1,y1), (x2,y2)... but here we have (x1,x2), (y1,y2). I hope you understand – Onyambu Aug 31 '18 at 16:52
  • @Onyambu Oh gotcha. Yes, I was reading OP's code wrong. – mypetlion Aug 31 '18 at 16:54
  • I'd recommend using a square root function that returns a complex number. – duffymo Sep 04 '18 at 14:40

3 Answers3

1
import math
def distance (x,y):
    value= math.sqrt ((x[0]-y[0])**2 + (x[1] - y[1])**2)
    print (value)
distance((0,0), (1,1))
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel May 02 '20 at 10:05
0

Thanks so much for those ideas! I figured it out. So happy.

for (a,b) in x,y:
    dis = math.sqrt((y[0] - x[0])**2 + (y[1] - x[1])**2)
    print(dis)
avwinter
  • 83
  • 2
  • 10
  • what is the purpose of for-loop yet even `a` and `b` have not been used???? – Onyambu Aug 31 '18 at 18:13
  • import math def distance(x, y): for (a,b) in x,y: distance = math.sqrt((y[0] - x[0])**2 + (y[1] - x[1])**2) print(distance) distance((0,0),(1,1)) – avwinter Aug 31 '18 at 19:57
  • without 'for (a, b)' the function does not know it is dealing with a tuple – avwinter Aug 31 '18 at 19:58
  • I was expected to use 'for (a,b)' in this exercise but how would you do it using indexing? I'm sure your method's good. – avwinter Sep 05 '18 at 16:35
0
import math
def distance(x1,x2,y1,y2):

    x=(x1,x2)
    y=(y1,y2)
    dis = math.sqrt((x[1]-x[0])**2 + (y[1] - y[0])**2)
    return dis

print(dis(0,0,1,1))

this works very well to answer your quest

David Buck
  • 3,752
  • 35
  • 31
  • 35