본문 바로가기

Python_Matter/[Check_IO]Elementary

Nearest Value

반응형

Quiz>

Find the nearest value to the given one.

 

You are given a list of values as set form and a value for which you need to find the nearest one.

 

For example, we have the following set of numbers: 4, 7, 10, 11, 12, 17, 

and we need to find the nearest value to the number 9. 

If we sort this set in the ascending order, then to the left of number 9 will be number 7 and to the right 

- will be number 10. But 10 is closer than 7, which means that the correct answer is 10.

 

A few clarifications:

If 2 numbers are at the same distance, you need to choose the smallest one;

The set of numbers is always non-empty, i.e. the size is >=1;

The given value can be in this set, which means that it’s the answer;

The set can contain both positive and negative numbers, but they are always integers;

The set isn’t sorted and consists of unique numbers.

 

Input:

Two arguments. A list of values in the set form. The sought value is an int.

 

Output:

Int.

 

Example:

nearest_value({4, 7, 10, 11, 12, 17}, 9) == 10
nearest_value({4, 7, 10, 11, 12, 17}, 8) == 7

 

def nearest_value(values: set, one: int) -> int:
    # your code here
    return None


if __name__ == '__main__':
    print("Example:")
    print(nearest_value({4, 7, 10, 11, 12, 17}, 9))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert nearest_value({4, 7, 10, 11, 12, 17}, 9) == 10
    assert nearest_value({4, 7, 10, 11, 12, 17}, 8) == 7
    assert nearest_value({4, 8, 10, 11, 12, 17}, 9) == 8
    assert nearest_value({4, 9, 10, 11, 12, 17}, 9) == 9
    assert nearest_value({4, 7, 10, 11, 12, 17}, 0) == 4
    assert nearest_value({4, 7, 10, 11, 12, 17}, 100) == 17
    assert nearest_value({5, 10, 8, 12, 89, 100}, 7) == 8
    assert nearest_value({-1, 2, 3}, 0) == -1
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Solve>

1. set에서 임의의 숫자를 하나 가져와서 변수에 담아둔다.

def nearest_value(values: set, one: int):
    guess = values.pop()

 

2. set에서 입력된 값을 하나씩 가져와서 절대 값을 통해서 각 값을 비교한다.

   그중에 가장 가까운 값을 guess 변수에 저장한다.

def nearest_value(values: set, one: int):
    for val in values:
        if abs(one - val) < abs(one - guess):
            guess = val

 

3. for문이 종료되면 guess 변수의 값을 반환한다.

def nearest_value(values: set, one: int):
    return guess

 

Code>

def nearest_value(values: set, one: int):
    guess = values.pop()

    for val in values:
        if abs(one - val) < abs(one - guess):
            guess = val

    return guess

 

Example>

if __name__ == '__main__':
    print("Example:")
    print(nearest_value({4, 7, 10, 11, 12, 17}, 9))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert nearest_value({4, 7, 10, 11, 12, 17}, 9) == 10
    assert nearest_value({4, 7, 10, 11, 12, 17}, 8) == 7
    assert nearest_value({4, 8, 10, 11, 12, 17}, 9) == 8
    assert nearest_value({4, 9, 10, 11, 12, 17}, 9) == 9
    assert nearest_value({4, 7, 10, 11, 12, 17}, 0) == 4
    assert nearest_value({4, 7, 10, 11, 12, 17}, 100) == 17
    assert nearest_value({5, 10, 8, 12, 89, 100}, 7) == 8
    assert nearest_value({-1, 2, 3}, 0) == -1
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Result>

Example:

10

Coding complete? Click 'Check' to earn cool rewards!

반응형

'Python_Matter > [Check_IO]Elementary' 카테고리의 다른 글

Elementary Map  (0) 2020.04.12
Between Markers  (0) 2020.04.11
Beginning Zeros  (0) 2020.04.11
Split Pairs  (0) 2020.04.11
Max Digit  (0) 2020.04.11