Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools as it
- import math
- from decimal import Decimal
- import pandas as pd
- def calculate_distance(x1: int, y1: int, x2: int, y2: int) -> Decimal:
- return Decimal(math.sqrt((x2 - x1)**2 + (y2 - y1)**2))
- def main(num_results: int, distance: Decimal):
- df = pd.read_csv('data.csv')
- list = df.values.tolist()[:500]
- output = []
- for a, b in it.combinations(list, 2):
- result = calculate_distance(y1=a[0], x1=a[1], y2=b[0], x2=b[1])
- if result >= distance:
- max_score = a[2] + b[2]
- if len(output) < num_results:
- pair = (a, b, max_score, result)
- output.append(pair)
- else:
- ''' find the pair with the current lowest score'''
- output = sorted(output, key=lambda x: x[2])
- # print(f"Previous minmax score {output[0][2]}")
- # print(f"New minmax score {max_score}")
- ''' if the current's pair max score is greater than the lowest
- previous, replace'''
- if output[0][2] < max_score:
- print("replacing")
- pair = (a, b, max_score, result)
- output[0] = pair
- return output
- results = main(5, Decimal(12))
- print(results)
Advertisement
Add Comment
Please, Sign In to add comment