Python_Matter/[Check_IO]Home

# Bigger Price

Quiz>

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.

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"

Output:

the same as the second Input argument.

Example:

``````bigger_price(2, [

{"name": "wine", "price": 138},

{"name": "meat", "price": 15},

{"name": "water", "price": 1}

]) == [

{"name": "wine", "price": 138},

]

bigger_price(1, [

{"name": "pen", "price": 5},

{"name": "whiteboard", "price": 170}

]) == [{"name": "whiteboard", "price": 170}]``````

``````def bigger_price(limit: int, data: list) -> list:
"""
TOP most expensive goods
"""
return None

if __name__ == '__main__':
from pprint import pprint
print('Example:')
pprint(bigger_price(2, [
{"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": "wine", "price": 138},
{"name": "meat", "price": 15},
{"name": "water", "price": 1}
]) == [
{"name": "wine", "price": 138},
], "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')``````

Solve>

1. 모듈 임포트

``from operator import itemgetter``

2. 특정한 데이터를 기준으로 정렬해야되므로 key 매겨변수에 price를 주고 reverse를 True를 줘서 내림차순으로

정렬한다. 또한 정렬한 값을 슬라이싱해서 반환한다.

itemgetter 모듈은 딕셔너리에 입력된 key 값과 value 값들을 정렬한다.

``````def bigger_price(limit: int, data: list):
return sorted(data, key=itemgetter("price"), reverse=True)[:limit]``````

Code>

``````def bigger_price(limit: int, data: list):
return sorted(data, key=itemgetter("price"), reverse=True)[:limit]``````

Example>

``````if __name__ == '__main__':
from pprint import pprint
print('Example:')
pprint(bigger_price(2, [
{"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": "wine", "price": 138},
{"name": "meat", "price": 15},
{"name": "water", "price": 1}
]) == [
{"name": "wine", "price": 138},
], "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')``````

Result>

Example:

[{'name': 'wine', 'price': 138}, {'name': 'bread', 'price': 100}]

Done! Looks like it is fine. Go and check it

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

 Sun Angle  (0) 2020.04.15 2020.04.15 2020.04.14 2020.04.14 2020.04.14 2020.04.14