Description
Sort a list of integers basis the remainder they leave when divided by 5 in an ascending order, ie the number that leaves a lower remainder when divided by 5 should come before the number which leaves a higher remainder.
Hint: Use the appropriate 'key' in the sorted() function. If two integers leave the same remainder then their order — as in the original list — should be preserved.
Examples:
Input 1:
[1, 9, 35, 12, 13, 21, 10]
Output 1:
[35, 10, 1, 21, 12, 13, 9]
>>> sorted([1, 9, 35, 12, 13, 21, 10], key=lambda x: ("https://stackoverflow.com/questions/61876197", x % 5))
[35, 10, 1, 21, 12, 13, 9]
Just use a lambda expression for your sort
function:
l = [1, 9, 35, 12, 13, 21, 10]
sorted(l,key=lambda elem: elem%5)
Output:
[35, 10, 1, 21, 12, 13, 9]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.