Answer to Question #289411 in Python for Abcd

Question #289411

Calculate Manhattan distances between these pairs of points (total 10*10 = 100


pairs inlcuding self pairs).This should give you a 10*10 2-D array where each value


corresponds to the distance between a pair

1
Expert's answer
2022-01-23T15:21:10-0500
'''
    Calculate Manhattan distances between these pairs of points (total 10*10 = 100
    pairs inlcuding self pairs).This should give you a 10*10 2-D array where each value
    corresponds to the distance between a pair
'''


def manhattan(a, b):
    return sum(abs(val1-val2) for val1, val2 in zip(a,b))


ROW=10
COL=10
Points=[]
Dist=[]
print("Points Array:")
for r in range(0,ROW):
    t=[]
    s = ""
    for c in range(0,COL):
        t.append(random.randint(0,100))
        s = s + "{:03d}".format(t[c])+" "
    print(s)
    Points.append(t)
    
Dist=[]
print("\nManhattan Distance Matrix:")
for r in range(0,ROW):
    t=Points[r]
    s = ""
    k=[]
    for c in range(0,ROW):
        k.append(manhattan(t, Points[c]))
        s = s + "{:03d}".format(k[c])+" "
    print(s)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS