Heart Killer
Heart Killer

Reputation: 9

Python: Converting Pseudo-code to code

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.

Upvotes: 0

Views: 1186

Answers (1)

PaSTE
PaSTE

Reputation: 4548

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)

Upvotes: 1

Related Questions