Numbers Increase While Letters Decrease
Python 2, 53 52 51 bytes
-2 bytes thanks to g.rocket
-1 byte thanks to Jonathan Frech
-1 byte thanks to RootTwo
def F(x):n=sorted(x);print[n.pop(-(e>x))for e in x]
Try it online!
The sorted
list will have the numbers first and then the chars like [3, 5, 6, 'a', 'b', 'x']
, then use e>x
to filter what is number and what is char, in python any number is less than a list (input) and a list is less than a string.
APL (Dyalog), 27 26 bytes
Expects characters to be uppercase
(⍋⊃¨⊂)@(~e)(⍒⊃¨⊂)@(e←∊∘⎕A)
Try it online!
This is just two applications of the form f@g
, apply the function f
on the items indicated by g
.
For the first application we use:
f
: ⍒⊃¨⊂
the descending grades (⍒
) each pick (⊃¨
) from the entire argument (⊂
).
g
: (e←∊∘⎕A)
members (∊
) of (∘
) the Alphabet (⎕A
), and store (←
) this function as e
.
For the second application we use:
f
: ⍋⊃¨⊂
the ascending grades (⍋
) each pick (⊃¨
) from the entire argument (⊂
).
g
: (~e)
not (~
) members of the alphabet (e
; the function we stored before)
JavaScript (ES6), 71 51 47 bytes
Saved 20 bytes by just using sort()
, as suggested by @JustinMariner
Saved 4 more bytes thanks to @CraigAyre
Using a similar approach as Rod's Python answer:
a=>[...a].map(n=>a.sort()[1/n?'shift':'pop']())
Test cases
let f =
a=>[...a].map(n=>a.sort()[1/n?'shift':'pop']())
console.log(JSON.stringify(f(['a', 2, 'b', 1, 'c', 3]))) // -> ['c', 1', 'b', 2, 'a', 3]
console.log(JSON.stringify(f([5, 'a', 'x', 3, 6, 'b']))) // -> [3, 'x', 'b', 5, 6, 'a']
console.log(JSON.stringify(f([3, 2, 1]))) // -> [ 1, 2, 3 ]
console.log(JSON.stringify(f(['a', 'b', 'c']))) // -> [ 'c', 'b', 'a' ]
console.log(JSON.stringify(f([]))) // -> []
console.log(JSON.stringify(f([2, 3, 2, 1]))) // -> [1, 2, 2, 3]