Biorąc pod uwagę listę dodatnich liczb całkowitych, która zawiera co najmniej 3 różne wpisy, wypisz permutację tej listy, która nie jest posortowana w kolejności rosnącej lub malejącej.
Przykłady
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..
Dzięki @Arnauld i @NoOneIsHere za tytuł!
[2,[1,3]]
.Odpowiedzi:
JavaScript (ES6),
3934 bajtówPosortuj tablicę w porządku rosnącym, pop ostatni element i użyj go jako pierwszego elementu nowej tablicy. Następnie zniszcz pozostałe elementy oryginalnej tablicy do nowej tablicy (zarówno w JS, jak
sort
ipop
zmodyfikuj oryginalną tablicę).Sprawdź to
źródło
a.sort()
?sort
metoda JS sortuje leksykograficznie.Brachylog , 2 bajty
Wypróbuj online!
lub
Wypróbuj online!
Sortuje, a następnie obraca listę
źródło
Galaretka , 3 bajty
Wypróbuj online!
źródło
Ṣṙ-
działa również (po prostuṢṙ1
tylko trzy bajty? W UTF-8 jest to 7 bajtów.Ohm , 2 bajty
Wypróbuj online!
Sortuj i obracaj w prawo.
źródło
Japt , 3 bajty
Sprawdź to
Sortuje (
n
) tablicę i obraca ją (é
) o jeden element w prawo.źródło
Python 3 , 31 bajtów
Wypróbuj online!
-1 bajt dzięki xnor
źródło
min
na końcu oszczędza bajt.APL, 9 bajtów
Wypróbuj online!
W jaki sposób?
⍵[⍋⍵]
- posortuj listę1⌽
- obróć o 1źródło
TI-Basic (TI-84 Plus CE), 31 bytes
Prompts for input in the format
{1,2,3,4}
.TI-Basic is a tokenized language, all tokens used here are one-byte.
Explanation:
źródło
Pyth,
754 bytesTry it online!
-1 byte thanks to FryAmTheEggman
źródło
05AB1E, 2 bytes
Try it online!
źródło
05AB1E, 2 bytes
Try it online!
źródło
Retina, 21 bytes
Try it online! Sort and rotate as per usual. At least there's no unary conversion this time.
źródło
Java 8,
6837 bytes-31 bytes thanks to @Nevay (forgot Java 8 had a
List#sort(Comparator)
method..)Modifies the input-
ArrayList
, instead of returning a new one.Explanation:
Try it here.
źródło
l->{l.sort(null);java.util.Collections.rotate(l,1);}
to save 16 bytes.l->{l.sort(null);l.add(l.remove(0));}
to save 31 bytes (requires the usage of a not fixed sized list).add
andremove
must be implemented; nothing is said about fixed-sized list... Kevin Cruijssen, given that there are much better alternatives in the previous comments, I'll wait for an edit before +1ing.Haskell,
3637 bytesUse view patterns to match on the head of a sorted version of the input list, then append the first item of the list to the tail of the remaining list.View patterns aren't worth it. Sort the list, take the head off, append it to the end. In this case, it turns out that the naive solution typed out compactly is the best.
źródło
-XViewPatterns
. Counting those the standard wayf(a:b)=b++[a];f.sort
is shorter.Perl 6,
4319 bytesTry it
Try it
Note that
[1..*,0]
would result in((2,3),1)
, so.flat
is there to turn it into(2,3,1)
źródło
Mathematica, 18 bytes
Try it online!
źródło
RotateLeft@*Sort
Ly, 7 bytes
Try it online!
Ugh, ruining the sort is so expensive!
Explanation:
źródło
R,
333229 bytesTakes input from stdin. Sorts the list and then moves the first element to the end, ensuring that it is no longer sorted. Saved three bytes due to Giuseppe.
Another implementation, same byte count:
źródło
c(sort(x<-scan())[-1],min(x))
is 29 bytes using essentially the same idea as yours.Ohm, 2 bytes
Try it online!
I think this is dissimilar enough from totallyhuman's post to post a new answer; I hope you don't mind :P EDIT: DAMMIT YOU NINJA'D ME
źródło
Python, 31 bytes
Yet another Python solution.
Sadly, this one has the same length to HyperNeutrino's answer.
źródło
Gaia, 3 bytes
Try it online!
Same as other answers: sort
ȯ
and rotate left once1«
.źródło
Retina, 10 bytes
Try it online!
This leaves the list with the 2nd highest element first and the highest element last which is never correctly sorted
źródło
Ruby, 18 bytes
Submitted on mobile. Please don't kill me for problems.
źródło
Pyth, 5 bytes
Explanation
SQ
- sort input list.>SQ1
- rotate input list cyclicaly by 1źródło
Proton, 19 bytes
Try it online!
-2 bytes indirectly thanks to xnor
Not yet working on TIO; waiting for a pull.źródło
Python 3, 28 bytes
Try it online!
a.sort()
sortsa
in place and returnsNone
.None
can be used as a slicing index and is the same as omitting that index.źródło
Python 3, 31 bytes
Try it online! or Verify all test cases.
Inspired by Shaggy's JS answer.
źródło
RProgN 2, 2 bytes
Try it online!
źródło
PHP, 44 bytes
requires PHP 5.4 or later for short array syntax.
sort arguments, replace 0-th argument with removed last argument, print.
Run with
-nr
or try it online.The 0-th argument is the script file name,
"-"
if you call PHP with-r
."-"
is compared to the other arguments as a string, and sinceord("-")==45
, it is smaller than any number. The numbers themselves, although strings, are compared as numbers:"12" > "2"
.php -nr '<code>' 3 4 2 5 1
andsort($a=&$argv)
lead to$a=["-","1","2","3","4","5"]
→[array_pop($a)]+$a
is[0=>"5"]+[0=>"-",1=>"1",2=>"2",3=>"3",4=>"4"]
,which results in
[0=>"5",1=>"1",2=>"2",3=>"3",4=>"4"]
.źródło
+
operator does not append, it merges (without reordering the indexes; but that doesn´t matter here). The important point is that$a
points to$argv
and$argv[0]
contains the script´s file name, the arguments start at index 1. I extended the description. Thanks for the question.Julia, 23 bytes
Slightly shorter than, but equivalent to
f(x)=circshift(sort(x),1)
. I wish I could makeamethod based onselect
that was more, compact but I can notźródło