From a630f6f7fb530163d30e83d571be8c6c20d21c39 Mon Sep 17 00:00:00 2001 From: Divlo Date: Mon, 28 Jun 2021 17:36:42 +0200 Subject: [PATCH] feat(solutions): add `defibrillators/python/function` --- .../solutions/python/function/README.md | 3 + .../solutions/python/function/solution.py | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 challenges/defibrillators/solutions/python/function/README.md create mode 100644 challenges/defibrillators/solutions/python/function/solution.py diff --git a/challenges/defibrillators/solutions/python/function/README.md b/challenges/defibrillators/solutions/python/function/README.md new file mode 100644 index 0000000..388a230 --- /dev/null +++ b/challenges/defibrillators/solutions/python/function/README.md @@ -0,0 +1,3 @@ +# defibrillators/python/function + +Created by [@Divlo](https://github.com/Divlo) on 28 June 2021. diff --git a/challenges/defibrillators/solutions/python/function/solution.py b/challenges/defibrillators/solutions/python/function/solution.py new file mode 100644 index 0000000..78a4d96 --- /dev/null +++ b/challenges/defibrillators/solutions/python/function/solution.py @@ -0,0 +1,56 @@ +from typing import List +import sys +import math + +input_values: List[str] = [] +for value in sys.stdin: + input_values.append(value.rstrip('\n')) + + +def convert_string_to_float(string: str) -> float: + return float(string.replace(',', '.')) + + +class Position: + def __init__(self, longitude: float, latitude: float) -> None: + self.longitude = self.convert_degrees_to_radien(longitude) + self.latitude = self.convert_degrees_to_radien(latitude) + + @staticmethod + def calculation_distance(pointA: 'Position', pointB: 'Position') -> float: + x = (pointB.longitude - pointA.longitude) * \ + math.cos((pointA.latitude + pointB.latitude) / 2) + y = pointB.latitude - pointA.latitude + return math.sqrt(math.pow(x, 2) + math.pow(y, 2)) * 6371 + + def convert_degrees_to_radien(self, degrees: float) -> float: + return degrees * (math.pi / 180) + + +class Defibrillator: + def __init__(self, strings: List[str], user_position: Position) -> None: + self.id = strings[0] + self.name = strings[1] + self.address = strings[2] + self.position = Position(convert_string_to_float( + strings[len(strings) - 2]), convert_string_to_float(strings[len(strings) - 1])) + self.distance = Position.calculation_distance( + self.position, user_position) + + +longitude = convert_string_to_float(input_values[0]) +latitude = convert_string_to_float(input_values[1]) +user_position = Position(longitude, latitude) +defibrillators: List[Defibrillator] = [] + +for index in range(3, len(input_values), 1): + line = input_values[index].split(';') + defibrillator = Defibrillator(line, user_position) + defibrillators.append(defibrillator) + +defibrillator_result = defibrillators[0] +for index in range(1, len(defibrillators), 1): + if defibrillator_result.distance > defibrillators[index].distance: + defibrillator_result = defibrillators[index] + +print(defibrillator_result.name)