1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-05-18 12:02:53 +02:00

feat(solutions): add find-closest-number/python/function

This commit is contained in:
Divlo
2022-05-01 19:07:49 +02:00
parent 4e7cad4a70
commit 530f433028
4 changed files with 22 additions and 2 deletions

View File

@ -0,0 +1,3 @@
# find-closest-number/python/function
Created by [@Divlo](https://github.com/Divlo) on 1 May 2022.

View File

@ -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))