Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Есть не сортированный список операций:
- operations = [
- {"id": 1, "timestamp": 2, "amount": 1},
- {"id": 2, "timestamp": 4, "amount": 8},
- {"id": 1, "timestamp": 3, "amount": 2}
- ]
- # В этом списке операции дублируются по id
- # если так случилось, то правильной операцией считается та,
- # у которой timestamp более поздний
- # Задача: модифицировать функцию filter, так чтобы она
- # возвращала только правильные операции
- # как только все тесты пройдут - задача решена
- def filter(operations: list) -> list:
- sortedOperations = sorted(operations, key=lambda d: int(d['timestamp']), reverse=True)
- currentID = -1
- res = []
- for _, el in enumerate(sortedOperations):
- if int(el['id']) == currentID:
- continue
- res.append(el)
- currentID = int(el['id'])
- return res
- # Do not touch this
- test_data = [
- {"in": [], "out": []},
- {"in": [{"id":1, "timestamp": 2}], "out": [{"id":1, "timestamp": 2}]},
- {"in": [{"id":1, "timestamp": 2}, {"id":1, "timestamp": 3}], "out": [{"id":1, "timestamp": 3}]},
- {"in": [{"id":1, "timestamp": 5}, {"id":1, "timestamp": 4}], "out": [{"id":1, "timestamp": 5}]}
- ]
- def test():
- for i, t in enumerate(test_data):
- try:
- assert filter(t["in"]) == t["out"]
- print(f"Test#{i} [ok]")
- except Exception as e:
- print(f"Test#{i} [error]")
- test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement