25
\$\begingroup\$

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,]

[][]

\$\endgroup\$
16
  • \$\begingroup\$ Ty. Good question. Answer: odd numbers can come in any order. :) \$\endgroup\$ Commented Jul 18, 2018 at 5:37
  • 11
    \$\begingroup\$ Even 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. \$\endgroup\$
    – Laikoni
    Commented Jul 18, 2018 at 7:17
  • 1
    \$\begingroup\$ @AsoneTuhid Yes:), numbers can repeat. \$\endgroup\$ Commented Jul 18, 2018 at 9:47
  • 11
    \$\begingroup\$ @Willmore You never know with code golf, rules are important. Please use the Sandbox next time to clarify your question before you post it. \$\endgroup\$ Commented Jul 18, 2018 at 10:09
  • 13
    \$\begingroup\$ Please edit your question to include the clarifications you gave in the comments. \$\endgroup\$
    – Laikoni
    Commented Jul 18, 2018 at 11:18

65 Answers 65

1 2
3
1
\$\begingroup\$

sed 4.2.2, 32 bytes

:
s/(<(!!)*>)(<(!!)*!>)/\3\1/g
t

Try it online!

Input is formatted as a exclamation marks surrounded by <>. Output is the same.

Ex: <><!><!!><!!!><!!!!> => <!><!!!><><!!><!!!!>

\$\endgroup\$
1
\$\begingroup\$

x86-16 machine code, 25 bytes

00000000: e316 8bd7 03d1 4afc aca8 0174 03aa eb06  ......J....t....
00000010: 87d7 fdaa 87d7 e2ef c3                   .........

Listing

E3 16       JCXZ EXIT           ; handle empty input list
8B D7       MOV  DX, DI         ; DI = output list head 
03 D1       ADD  DX, CX         ; DX = output list tail (1 past)
4A          DEC  DX             ; DX = output list tail
        INLOOP: 
FC          CLD                 ; string direction forward 
AC          LODSB               ; get next char 
A8 01       TEST AL, 1          ; is even? NZ if odd, ZF if even 
74 03       JZ   EVEN 
        ODD: 
AA          STOSB               ; write to list head 
EB 06       JMP  DONE 
        EVEN: 
87 D7       XCHG DX, DI         ; DI = list tail 
FD          STD                 ; string direction reverse 
AA          STOSB               ; write to list tail 
87 D7       XCHG DX, DI         ; DI = list head 
        DONE:
E2 EF       LOOP INLOOP         ; continue looping input list
        EXIT:
C3          RET                 ; return to caller

Callable function, input list at DS:SI, length in CX. Output list buffer at ES:DI.

If input number is odd, is added to the beginning of output list. If even, is added to the end of the output list.

Test cases:

[1, 2][1, 2]

enter image description here

[0, 0, -1][-1, 0, 0]

enter image description here

[-1, -2, 3, -4, -5][-1, 3, -5, -4, -4]

enter image description here

[3, 2, 2, 2, 1, 2][3, 1, 2, 2, 2, 2]

enter image description here

\$\endgroup\$
0
\$\begingroup\$

Kotlin, 33 bytes

{l:List<Int>->l.sortedBy{1-it%2}}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ is it correct with negative even? 0,0,-1, -2,-2,-2,-1,-2,-3 \$\endgroup\$
    – mazzy
    Commented Jul 20, 2018 at 14:56
  • \$\begingroup\$ This answer does not work for negative numbers. Suggestion (38 bytes, you may be able to improve on that): tio.run/##TY7bCoJAFEXf/YqDEMzEUdLuoka9Bb71BQNeODSOMp4CEb/… \$\endgroup\$ Commented Nov 8, 2018 at 10:03
0
\$\begingroup\$

Swift, 22 bytes

Two different answers with the same byte count!

In-place

{$0.partition{$0&1<1}}

Argument type is any MutableCollection whose element type conforms to BinaryInteger. Technically this returns the index where it changes from odd to even numbers, but this can be made to return Void with a warning: SwiftFiddle link

C brain keeps telling me to do ~$0&1 but Swift doesn't do int-bool conversion

New array

{$0.sorted{$0&1>$1&1}}

Argument type is any Sequence whose element type conforms to BinaryInteger. Returns an array with the same element type. SwiftFiddle link

\$\endgroup\$
0
\$\begingroup\$

Arturo, 22 bytes

$=>[arrange&=>[^0-1&]]

Try it!

$=>[             ; a function where input is assigned to &
    arrange&=>[  ; sort input by
        ^0-1&    ; -1 to the (current element) power
    ]            ; end arrange
]                ; end function
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.