From 530f433028055e02b76ac2cba07e988f1d09f3b7 Mon Sep 17 00:00:00 2001 From: Divlo Date: Sun, 1 May 2022 19:07:49 +0200 Subject: [PATCH] feat(solutions): add `find-closest-number/python/function` --- .../solutions/python/function/solution.py | 2 +- .../solutions/python/function/README.md | 3 +++ .../solutions/python/function/solution.py | 17 +++++++++++++++++ .../solutions/python/function/solution.py | 2 +- 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 challenges/find-closest-number/solutions/python/function/README.md create mode 100644 challenges/find-closest-number/solutions/python/function/solution.py diff --git a/challenges/cakes-swerc-2020-2021/solutions/python/function/solution.py b/challenges/cakes-swerc-2020-2021/solutions/python/function/solution.py index 049ccca..38ebd76 100644 --- a/challenges/cakes-swerc-2020-2021/solutions/python/function/solution.py +++ b/challenges/cakes-swerc-2020-2021/solutions/python/function/solution.py @@ -6,7 +6,7 @@ next(sys.stdin) for line in sys.stdin: quantity_per_cake, quantity_available = list(map(int, line.split(' '))) cake_possible = quantity_available // quantity_per_cake - if maximum_number_of_cake_possible == None or cake_possible < maximum_number_of_cake_possible: + if maximum_number_of_cake_possible is None or cake_possible < maximum_number_of_cake_possible: maximum_number_of_cake_possible = cake_possible print(maximum_number_of_cake_possible) diff --git a/challenges/find-closest-number/solutions/python/function/README.md b/challenges/find-closest-number/solutions/python/function/README.md new file mode 100644 index 0000000..8d6fa85 --- /dev/null +++ b/challenges/find-closest-number/solutions/python/function/README.md @@ -0,0 +1,3 @@ +# find-closest-number/python/function + +Created by [@Divlo](https://github.com/Divlo) on 1 May 2022. diff --git a/challenges/find-closest-number/solutions/python/function/solution.py b/challenges/find-closest-number/solutions/python/function/solution.py new file mode 100644 index 0000000..63a67de --- /dev/null +++ b/challenges/find-closest-number/solutions/python/function/solution.py @@ -0,0 +1,17 @@ +import sys + + +def find_closest_number(integers: list[int], value: int) -> int: + """ + From list of integers, get number closest to a given value + """ + return min(integers, key=lambda x: abs(x - value)) + + +numbers: list[int] = [] +for value in sys.stdin: + numbers.append(int(value.rstrip('\n'))) + +given_number = numbers[0] +numbers = numbers[2:] +print(find_closest_number(numbers, given_number)) diff --git a/challenges/offset-arrays/solutions/python/function/solution.py b/challenges/offset-arrays/solutions/python/function/solution.py index fa9f6d9..623a007 100644 --- a/challenges/offset-arrays/solutions/python/function/solution.py +++ b/challenges/offset-arrays/solutions/python/function/solution.py @@ -53,7 +53,7 @@ operations_index = int(nested[last_operation_index]) current_value = None for index in range(len(operation_identifiers) - 1, -1, -1): current_identifier = operation_identifiers[index] - if current_value == None: + if current_value is None: current_value = assignments[current_identifier].get_by_index( operations_index) else: