# Question

In this exercise, we compute the distance between two points where the first is 2D and the second is 3D. How do we compute their difference when the dimensions are different?

If we have points `p1` and `p2` represented as arrays and the dimension, that is, the size of the array, of `p1` is less than the dimension of `p2`. Then we make sense of the distance between these points by adding enough zeros to the end of the array for `p1` until the sizes of the arrays are equal. Explicitly, if `p1 = [1,0]` and `p2 = [1,1,1,1]`, then `Distance(p1,p2)` is the same as `Distance([1,0,0,0], p2)`.

5 Likes

Explicitly, if `p1 = [1,0]` and `p2 = [1,1,,1,1]` , then `Distance(p1,p2)` is the same as `Distance([1,0,0,0], p2)` .

What does the empty space do at index 2 of the p2 list `p2 = [1,1,,1,1]` ?

I feel like this would throw a syntax error.

2 Likes

Well spotted! You are right

I edited the FAQ. Thank you very much.

1 Like

On point 2 with the code:

Blockquote
def euclidean_distance(pt1, pt2):
distance = 0
â# We will be adding code here nextâ
for i in range(len(pt1)):
distance += (pt1[i] - pt2[i]) ** 2

It is giving me error:

Blockquote
Traceback (most recent call last):
File âscript.pyâ, line 10, in
for i in range(len(pt1)):
NameError: name âpt1â is not defined

Which is probably right pt1 and pt2 havenât been defined yet.

Anyone else having similar issues?

the for loop should be part of the function (body). There `pt1` is defined, its one of the function parameters.

1 Like

I know this question is two months old and youâve probably resolved the issue yourself, but just in case someone else comes along and sees this question, what I think is going on here is that you have indeed defined pt1 as a parameter of the function, but you are then using pt1 as a variable outside the function in your line âfor i in range(len(pt1)):â because itâs not indented (plus the indent problems for the for loop and the âdistance = 0â)

I think if your code is otherwise fine if itâs just indented correctly.

Happy coding.

1 Like

def euclidean_distance(pt1, pt2):
distance = sum([(pt1[i] - pt2[i]) ** 2 for i in range(len(pt1))]) ** 0.5
return distance

1 Like

Like @jimbeason pointed, the identation thing is very important, absolutely. Although, it seems that the step 2 will only works if you pass the the step 3 code in that ; it is, when you return the square root of distance.
The text passage primarily discusses the computation of the distance between two points of different dimensions, providing an explanation and a code snippet for the Euclidean distance calculation. It also includes a discussion about a syntax error in an example and a coding issue related to indentation.
multiple_questions

the passage contains multiple questions:
1. "what does the empty space do at index 2 of the p2 list `p2 = [1,1,,1,1]`?"
2. a question about an error in the code snippet: "it is giving me error: ... which is probably right pt1 and pt2 havenât been defined yet."

however, the passage does not provide answers to these specific questions, though it does acknowledge the syntax error and provide a solution for the indentation issue.
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# Question

In this exercise, we compute the distance between two points where the first is 2D and the second is 3D. How do we compute their difference when the dimensions are different?

If we have points `p1` and `p2` represented as arrays and the dimension, that is, the size of the array, of `p1` is less than the dimension of `p2`. Then we make sense of the distance between these points by adding enough zeros to the end of the array for `p1` until the sizes of the arrays are equal. Explicitly, if `p1 = [1,0]` and `p2 = [1,1,1,1]`, then `Distance(p1,p2)` is the same as `Distance([1,0,0,0], p2)`.

5 Likes

Explicitly, if `p1 = [1,0]` and `p2 = [1,1,,1,1]` , then `Distance(p1,p2)` is the same as `Distance([1,0,0,0], p2)` .

What does the empty space do at index 2 of the p2 list `p2 = [1,1,,1,1]` ?

I feel like this would throw a syntax error.

2 Likes

Well spotted! You are right

I edited the FAQ. Thank you very much.

1 Like

On point 2 with the code:

Blockquote
def euclidean_distance(pt1, pt2):
distance = 0
â# We will be adding code here nextâ
for i in range(len(pt1)):
distance += (pt1[i] - pt2[i]) ** 2

It is giving me error:

Blockquote
Traceback (most recent call last):
File âscript.pyâ, line 10, in
for i in range(len(pt1)):
NameError: name âpt1â is not defined

Which is probably right pt1 and pt2 havenât been defined yet.

Anyone else having similar issues?

the for loop should be part of the function (body). There `pt1` is defined, its one of the function parameters.

1 Like

I know this question is two months old and youâve probably resolved the issue yourself, but just in case someone else comes along and sees this question, what I think is going on here is that you have indeed defined pt1 as a parameter of the function, but you are then using pt1 as a variable outside the function in your line âfor i in range(len(pt1)):â because itâs not indented (plus the indent problems for the for loop and the âdistance = 0â)

I think if your code is otherwise fine if itâs just indented correctly.

Happy coding.

1 Like

def euclidean_distance(pt1, pt2):
distance = sum([(pt1[i] - pt2[i]) ** 2 for i in range(len(pt1))]) ** 0.5
return distance

1 Like

Like @jimbeason pointed, the identation thing is very important, absolutely. Although, it seems that the step 2 will only works if you pass the the step 3 code in that ; it is, when you return the square root of distance.
user
The text passage primarily discusses the computation of the distance between two points of different dimensions, providing an explanation and a code snippet for the Euclidean distance calculation. It also includes a discussion about a syntax error in an example and a coding issue related to indentation.

TYPE: MULTIPLE_QUESTIONS

The passage contains multiple questions:
1. "What does the empty space do at index 2 of the p2 list `p2 = [1,1,,1,1]`?"
2. A question about an error in the code snippet: "It is giving me error: ... Which is probably right pt1 and pt2 havenât been defined yet."

However, the passage does not provide answers to these specific questions, though it does acknowledge the syntax error and provide a solution for the indentation issue.