Six natural numbers are input in order, a list is generated, and then output. Then, the rearranged list is printed according to the following rules.
[Example 1] 5 2 6 4 3 1
[Example 1] [5, 2, 6, 4, 3, 1] [5, 3, 1, 6, 4, 2]
[Example 2] 10 5 8 45 3 153
[Example 2] [10, 5, 8, 45, 3, 153] [153, 45, 5, 3, 10, 8] How do I solve it...?
python
mainList = [];
odd=[];
even=[];
parCnt = 6;
for i in range(parCnt):
val = int(input());
mainList.append(val);
print (mainList);
for i in mainList:
if i%2==0:
even.append(i);
else:
odd.append(i);
even.sort(); even.reverse();
odd.sort(); odd.reverse();
mainList = odd + even;
print (mainList);
Is this appropriate?
You can also write it in a bit of a psychedelic
In [1]: s = '5 2 6 4 3 1'
In [2]: L = [int(i)for i in s.split()]
In [3]: sorted(i for i in L if i & 1)[::-1] + sorted(i for i in L if not i & 1)[::-1]
Out[3]: [5, 3, 1, 6, 4, 2]
In [4]: sorted((i for i in L if i & 1), reverse=True) + sorted((i for i in L if not i & 1), reverse=True)
Out[4]: [5, 3, 1, 6, 4, 2]
© 2024 OneMinuteCode. All rights reserved.