The problem
You’ll be given an array of numbers. You must kind the odd numbers in ascending order whereas leaving the even numbers at their authentic 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]
The answer in Python code
Possibility 1:
def sort_array(arr):
odds = sorted((x for x in arr if xpercent2 != 0), reverse=True)
return [x if x%2==0 else odds.pop() for x in arr]
Possibility 2:
def sort_array(source_array):
end result = sorted([l for l in source_array if l % 2 == 1])
for index, merchandise in enumerate(source_array):
if merchandise % 2 == 0:
end result.insert(index, merchandise)
return end result
Possibility 3:
def sort_array(source_array):
odd = sorted(checklist(filter(lambda x: x % 2, source_array)))
l, c = [], 0
for i in source_array:
if i in odd:
l.append(odd[c])
c += 1
else:
l.append(i)
return l
Check circumstances to validate our answer
take a look at.assert_equals(sort_array([5, 3, 2, 8, 1, 4]), [1, 3, 2, 8, 5, 4])
take a look at.assert_equals(sort_array([5, 3, 1, 8, 0]), [1, 3, 5, 8, 0])
take a look at.assert_equals(sort_array([]),[])