KKP4

Untitled

Mar 23rd, 2022
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. import itertools as it
  2. import math
  3. from decimal import Decimal
  4.  
  5. import pandas as pd
  6.  
  7.  
  8. def calculate_distance(x1: int, y1: int, x2: int, y2: int) -> Decimal:
  9.  
  10.     return Decimal(math.sqrt((x2 - x1)**2 + (y2 - y1)**2))
  11.  
  12.  
  13. def main(num_results: int, distance: Decimal):
  14.     df = pd.read_csv('data.csv')
  15.     list = df.values.tolist()[:500]
  16.  
  17.     output = []
  18.  
  19.     for a, b in it.combinations(list, 2):
  20.         result = calculate_distance(y1=a[0], x1=a[1], y2=b[0], x2=b[1])
  21.         if result >= distance:
  22.             max_score = a[2] + b[2]
  23.             if len(output) < num_results:
  24.                 pair = (a, b, max_score, result)
  25.                 output.append(pair)
  26.             else:
  27.                 ''' find the pair with the current lowest score'''
  28.                 output = sorted(output, key=lambda x: x[2])
  29.                 # print(f"Previous minmax score {output[0][2]}")
  30.                 # print(f"New minmax score {max_score}")
  31.                 ''' if the current's pair max score is greater than the lowest
  32.                        previous, replace'''
  33.                 if output[0][2] < max_score:
  34.                     print("replacing")
  35.                     pair = (a, b, max_score, result)
  36.                     output[0] = pair
  37.     return output
  38.  
  39.  
  40. results = main(5, Decimal(12))
  41. print(results)
Advertisement
Add Comment
Please, Sign In to add comment