23
\$\begingroup\$

Given a list of positive integers that contains at least 3 distinct entries, output a permutation of that list that isn't sorted in ascending or descending order.

Examples

1,2,3 -> 2,1,3 or 3,1,2 or 1,3,2 or 2,3,1
1,2,3,3 -> 2,1,3,3 or 3,1,2,3 or 1,3,2,3 etc..

Thanks @Arnauld and @NoOneIsHere for the title!

\$\endgroup\$
  • \$\begingroup\$ Will the input always be sorted? \$\endgroup\$ – xnor Aug 13 '17 at 20:31
  • \$\begingroup\$ Must the sort be "reliable" in that given a given set of entries, it always produces the same permutation as output? Or must it only be "reliable" in that the output is not sorted? \$\endgroup\$ – Wildcard Aug 15 '17 at 3:16
  • \$\begingroup\$ It must just satisfy the specs. \$\endgroup\$ – flawr Aug 15 '17 at 14:58
  • \$\begingroup\$ Would a nested array be allowed as output? e.g., [2,[1,3]]. \$\endgroup\$ – Shaggy Aug 19 '17 at 9:51
  • \$\begingroup\$ No, it should be one single array/list. \$\endgroup\$ – flawr Aug 19 '17 at 11:47

36 Answers 36

1
2
1
\$\begingroup\$

Common Lisp, 52 bytes

(let((x(sort(read)'<)))(rotatef(nth 0 x)(nth 1 x))x)

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C# (Mono), 76 67 bytes

using System.Linq;a=>a.OrderBy(n=>n).Skip(1).Concat(new[]{a.Min()})

Saved 9 bytes thanks to @Patrick Stephansen.

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Save a few bytes: a=>a.OrderBy(n=>n).Skip(1).Concat(new[]{a.Min()}) \$\endgroup\$ – Patrick Stephansen Aug 15 '17 at 10:42
1
\$\begingroup\$

Clojure, 36 bytes

#(let[s %](cons(last s)(butlast s)))

Stole the solution to sort it and push the last element to the start of the list.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

TeX - 207 183 bytes

\def\a#1 #2 #3 #4 {\ifnum#2#1#3\def\e{#3 #4 #2 }\else\def\e{#2 #4 #3 }\fi\e}\def\b#1 #2 #3 {\a< #1 #2 \a> #3 {} }\def\c#1 #2 {\ifnum#1=#2\let\b\f\def\d#1 {#1 \c}\else\let\d\b\fi\d#1 #2 }\def\f#1 #2 {#2 #1 }

This is the best I can do with a language that doesn't really have data structures, or many builtins.

The approach is to take the first three numbers we find, and do a broken sort as follows: Sort the first two by greater, and then take the second two by lesser. This should always result in our list being unsorted. It's convenient to ignore anything but the first 3 unique inputs.

We also have a check for two of the same number in a row. In that case, we scan ahead until we find a different number, and then switch the two, ensuring that our list is out of order.

I'm gonna work on golfing this down, but it's the best approach I can think of.

EDIT: Considerable work has been put in to this, to make it more golfy and also actually work. So here it is:

\let~\def~\a#1 #2 {\if^#2~\e{\box9 #1 }\else\ifnum#1\h#2~\e{\hbox{#1 }\a#2 }\else~\e{\hbox{#2 }\a#1 }\ifnum#1=#2~\h{=}\setbox9=\lastbox\fi\fi\fi~\g{>}\e}~\g{<}~\h{\g}\everypar{\a}\x^

I changed the the method from inserting things into the input to one where we recurse, replaced \expandafter with \everypar, and added that when it finds an equal number, it grabs the last number typeset and saves it until the second to last slot, ensuring that 1 2 2 (the case that secretly broke the previous answer) will be incorrectly sorted as 2 1 2.

This is just a function, to properly use it you need to: have input stored on the variable \x (or you could type in a list of numbers, with a ^ as a delimeter).

If you'd really like to try this, then do this

\read1to\x    

before the lines of this function.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ruby, 20 bytes

->(x){x.sort.rotate}

I can't figure out how to use lambdas on TIO, but I'll add a link when I do

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 33 bytes

DEF B A
SORT A
PUSH A,SHIFT(A)END

Modifies the array in place. This uses the simple sort+rotate algorithm, though there may be a shorter solution involving RINGCOPY or something (the problem being that COPY and RINGCOPY don't work well when copying between overlapping parts of the same array)

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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