Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort only the odd numbers
#1
my question here
Define a function that takes an array of numbers and sorts only the odd ones in ascending order, leaving the even ones in their places. No need to move the number 0 should it occur. Here is an example:

sort_odds([1,5,3,2,7,7,3,2,7]) -> [1, 3, 3, 2, 5, 7, 7, 2, 7]

How can I add the even # back to their original positions?

[
python]my code here[/python]
def sort_odds(arr):
return sorted([i for i in arr if i%2!= 0])
Reply


Messages In This Thread
Sort only the odd numbers - by tomhuang - Jul-20-2017, 05:10 PM
RE: Sort only the odd numbers - by Bass - Jul-20-2017, 05:15 PM
RE: Sort only the odd numbers - by nilamo - Jul-20-2017, 06:00 PM
RE: Sort only the odd numbers - by ichabod801 - Jul-20-2017, 06:02 PM
RE: Sort only the odd numbers - by ichabod801 - Jul-20-2017, 06:07 PM
RE: Sort only the odd numbers - by buran - Jul-20-2017, 08:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,393 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,847 May-09-2019, 12:19 PM
Last Post: Pleiades
  Need Help: Sort of Binary numbers based on 1's present abinashj 5 5,179 Jul-25-2017, 12:23 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020