본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 45

반응형

Q>

You have a table with all available goods in the store. The data is represented as a list of dicts.

(상점에 사용 가능한 모든 상품이 들어있는 테이블이 있습니다. 데이터는 딕셔너리로 표시됩니다.)

Your mission here is to find the TOP most expensive goods.

(당신의 임무는 TOP에서 가장 비싼 물건을 찾는 것입니다.)

The amount we are looking for will be given as a first argument and the whole data as the second one.

(우리가 찾고있는 금액은 첫 번째 인수로 주어지며 전체 데이터는 두 번째 인수로 주어집니다.)

 

Input: int and list of dicts. Each dicts has two keys "name" and "price"

         (int 및 dicts 목록. 각 dicts에는 "name"과 "price"이라는 두 개의 키가 있습니다.)

 

Output: the same as the second Input argument.

           (두 번째 입력 인수와 같습니다.)

 

Example:

bigger_price(2, [
    {"name": "bread", "price": 100},
    {"name": "wine", "price": 138},
    {"name": "meat", "price": 15},
    {"name": "water", "price": 1}
]) == [
    {"name": "wine", "price": 138},
    {"name": "bread", "price": 100}
]

bigger_price(1, [
    {"name": "pen", "price": 5},
    {"name": "whiteboard", "price": 170}
]) == [{"name": "whiteboard", "price": 170}]

 

A>

def bigger_price(limit: int, data: list) -> list:
    """
        TOP most expensive goods
    """
    # your code here
    # sroted : 입력 받은 Data를 오름차순으로 정렬 메소드
    # price를 기준으로 정렬하는데 reverse 를 True므로 내림차순으로 정렬(비싼거로 정렬)
    # limit를 통해 상위 몇개를 가져올지 정한다.
    return sorted(data, key=lambda x: x['price'], reverse=True) [:limit]

if __name__ == '__main__':
    from pprint import pprint
    print('Example:')
    pprint(bigger_price(2, [
        {"name": "bread", "price": 100},
        {"name": "wine", "price": 138},
        {"name": "meat", "price": 15},
        {"name": "water", "price": 1}
    ]))

    # These "asserts" using for self-checking and not for auto-testing
    assert bigger_price(2, [
        {"name": "bread", "price": 100},
        {"name": "wine", "price": 138},
        {"name": "meat", "price": 15},
        {"name": "water", "price": 1}
    ]) == [
        {"name": "wine", "price": 138},
        {"name": "bread", "price": 100}
    ], "First"

    assert bigger_price(1, [
        {"name": "pen", "price": 5},
        {"name": "whiteboard", "price": 170}
    ]) == [{"name": "whiteboard", "price": 170}], "Second"

    print('Done! Looks like it is fine. Go and check it')

 

O>

Example:
[{'name': 'wine', 'price': 138}, {'name': 'bread', 'price': 100}]
Done! Looks like it is fine. Go and check it

Process finished with exit code 0

 

S>

https://py.checkio.org

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 47  (0) 2019.06.10
Python Learn the basics Quiz 46  (0) 2019.06.10
Python Learn the basics Quiz 44  (0) 2019.06.10
Python Learn the basics Quiz 43  (0) 2019.06.09
Python Learn the basics Quiz 42  (0) 2019.06.09