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/function

This commit is contained in:
Divlo 2022-05-01 19:07:49 +02:00
parent 4e7cad4a70
commit 530f433028
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
4 changed files with 22 additions and 2 deletions

View File

@ -6,7 +6,7 @@ next(sys.stdin)
for line in sys.stdin: for line in sys.stdin:
quantity_per_cake, quantity_available = list(map(int, line.split(' '))) quantity_per_cake, quantity_available = list(map(int, line.split(' ')))
cake_possible = quantity_available // quantity_per_cake 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 maximum_number_of_cake_possible = cake_possible
print(maximum_number_of_cake_possible) print(maximum_number_of_cake_possible)

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

View File

@ -53,7 +53,7 @@ operations_index = int(nested[last_operation_index])
current_value = None current_value = None
for index in range(len(operation_identifiers) - 1, -1, -1): for index in range(len(operation_identifiers) - 1, -1, -1):
current_identifier = operation_identifiers[index] current_identifier = operation_identifiers[index]
if current_value == None: if current_value is None:
current_value = assignments[current_identifier].get_by_index( current_value = assignments[current_identifier].get_by_index(
operations_index) operations_index)
else: else: