1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

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

This commit is contained in:
Divlo 2022-05-01 19:11:11 +02:00
parent 530f433028
commit 173c32d6f2
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
2 changed files with 24 additions and 0 deletions

View File

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

View File

@ -0,0 +1,21 @@
import sys
def find_closest_number(integers: list[int], value: int) -> int:
"""
From list of integers, get number closest to a given value
"""
current = integers[0]
for number in integers:
if abs(number - value) < abs(current - value):
current = number
return current
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))