Even and Odd Buckets

Sometimes it is useful to split numbers into two buckets right away: even numbers in one and odd numbers in the other. Write a function group_by_parity that receives a list of integers nums. Return a dictionary with keys "even" and "odd", containing the matching numbers in their original order. Both keys must always be present, even if one of the lists is empty.

Примеры

Ввод
[1, 2, 3, 4]
Вывод
{"even": [2, 4], "odd": [1, 3]}
Ввод
[]
Вывод
{"even": [], "odd": []}
Ввод
[2, 4, 6]
Вывод
{"even": [2, 4, 6], "odd": []}

Нужно реализовать

def group_by_parity(nums: list[int]) -> dict:

Теги

arraydicthashmap
PostgreSQLv16

Här visas resultatet av din kod

Visible tests
1
Input
[1,2,3,4]
Expected{"odd":[1,3],"even":[2,4]}
2
Input
[]
Expected{"odd":[],"even":[]}
3
Input
[2,4,6]
Expected{"odd":[],"even":[2,4,6]}
4
Input
[1,3,5]
Expected{"odd":[1,3,5],"even":[]}
5
Input
[0]
Expected{"odd":[],"even":[0]}
6
Input
[-2,-1]
Expected{"odd":[-1],"even":[-2]}
Focus radio
Paused · SomaFM · Fluid