0

I tried coding these lines:

copy the first {ceiling of} n/2 points of P to array Pleft
copy the same {ceiling of} n/2 points from Q to array Qleft
copy the remaining {floor of} n/2 points of P to array Pright
copy the same {floor of} n/2 points from Q to array Qright 
m = P[ ({ceiling of} n/2) - 1].x

And I got:

mid = len(P) / 2

Pl = P[:mid]
Ql = Q[:mid]
Pr = P[mid:]
Qr = Q[mid:]
m = P[:mid - 1].x

But I do not know how to code this line:

copy all the points of array Q for which |x - m| < d into new array S[0..num - 1]

Please help.

2
  • You need math.abs() and list comprehensions (or a simple for-loop) for this. Commented Nov 14, 2017 at 2:23
  • 1
    By the way: You overwrite mid without using it. It can only hold one value. Commented Nov 14, 2017 at 2:25

1 Answer 1

1

EDIT: Oops! Forgot that m is a list!

This is what list comprehensions are for. Because m is a list, you'll also need to make use of python's zip function. The definition of the new array is:

S = [x for x, y in zip(Q, m) if abs(x - y) < d]

You'll have to supply d. Also, for completeness:

num = len(S)
Sign up to request clarification or add additional context in comments.

2 Comments

But there's this error for this code: "TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'"
I can't tell what the structure of Q or m from the code you posted, so I assumed they were simple lists of numeric types. By the text of the error, it appears to be a list of tuples. You will either have to post how Q and P are constructed in your question, or modify the code I posted to put the tuple indices on x and y in the call abs(x - y).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.