Untitled
unknown
python
2 years ago
1.1 kB
6
Indexable
You will be given an array of numbers. You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions.
Examples
[7, 1] => [1, 7]
[5, 8, 6, 3, 4] => [3, 8, 6, 5, 4]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] => [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
def sort_array(source_array):
"""
در این سوال باید اعداد فرد را مرتب کرده از کوچک به بزرگ در حالی که اعداد زوجمان تغیر نکنند ما در ابتدا اعداد فرد را
تفکیک و مرتب کردیم و در ادامه اعداد زوج را تفکیک اما مرتب نکردیم در اخر
با استفاده از شرط جایگاه اعداد فرد را در لیست اعداد زوج مشخص کردیم
"""
odd=[]
even=[]
index=0
for i in source_array:
if i%2==1:
odd.append(i)
odd.sort()
for b in source_array:
if b%2==0:
even.append(b)
else:
even.append(odd[index])
index+=1
return even
passEditor is loading...
Leave a Comment