20
4
Rearrange a given list such that all the odd numbers appear before all the even numbers. Besides for this requirement, the output list may be in any order.
The input will only contain integers, but they may be negative and there may be duplicates, and they may appear in any order.
Shortest solution wins.
Test cases
[1,2]
→ [1,2]
[2,1]
→ [1,2]
[1,0,0]
→ [1,0,0]
[0,0,-1]
→ [-1,0,0]
[3,4,3]
→ [3,3,4]
[-4,3,3]
→ [3,3,-4]
[2,2,2,3]
→ [3,2,2,2]
[3,2,2,2,1,2]
→ [1,3,2,2,2,2]
or [3,1,2,2,2,2]
[-2,-2,-2,-1,-2,-3]
→ [-1,-3,-2,-2,-2,-2,]
or [-3,-1,-2,-2,-2,-2,]
[]
→ []
Ty. Good question. Answer: odd numbers can come in any order. :) – display_name – 2018-07-18T05:37:27.003
this is somewhat similar – dylnan – 2018-07-18T05:40:26.407
Can there be any negative numbers in the input? – user202729 – 2018-07-18T06:14:44.433
Integers expected. – display_name – 2018-07-18T06:16:23.773
11Even though the challenge is quite simple, adding some test cases would be nice. E.g. at first glance I thought the block of odd and even numbers also needs to be sorted. – Laikoni – 2018-07-18T07:17:34.750
@Laikoni I agree. – display_name – 2018-07-18T07:23:41.847
for test cases I'd suggest
[1,2]
,[2,1]
,[1,0,0]
,[0,0,-1]
,[3,4,3]
,[-4,3,3]
,[2,2,2,3]
,[3,2,2,2,1,2]
(with both possible outputs) and[-2,-2,-2,-1,-2,-3]
(again giving both outputs). – Jonathan Allan – 2018-07-18T07:55:08.380You may also want to add some edge cases:
[]
,[3,1,3]
, and[2,8,8]
– Adám – 2018-07-18T08:11:15.783Nice first challenge! – Adám – 2018-07-18T08:11:54.280
@dylnan, given that the majority of the solutions to that challenge use this sorting method, I'd lean towards this being a dupe, but I won't VTC as my vote is a hammer. – Shaggy – 2018-07-18T08:35:52.650
Will there be repeated numbers in the array? – Asone Tuhid – 2018-07-18T09:38:14.930
1@AsoneTuhid Yes:), numbers can repeat. – display_name – 2018-07-18T09:47:47.843
@Willmore what about negatives? – Asone Tuhid – 2018-07-18T09:50:37.963
@AsoneTuhid Negatives allowed since integers include negatives. – display_name – 2018-07-18T10:07:12.887
11
@Willmore You never know with code golf, rules are important. Please use the Sandbox next time to clarify your question before you post it.
– Asone Tuhid – 2018-07-18T10:09:40.06712Please edit your question to include the clarifications you gave in the comments. – Laikoni – 2018-07-18T11:18:02.790