Tantangan Anda hari ini adalah menulis program atau fungsi yang mengambil daftar l
dan memberikan posisil
mana setiap elemen berurutan l
muncul.
Dengan kata lain, output indeks dari nilai terkecil, diikuti oleh indeks dari nilai terkecil kedua, dll.
Anda dapat mengasumsikan bahwa array input hanya akan berisi bilangan bulat positif, dan akan mengandung setidaknya satu elemen.
Kasus uji:
Input | Output (1-indexed)
[7, 4, 5] | [2, 3, 1]
[1, 2, 3] | [1, 2, 3]
[2, 6, 1, 9, 1, 2, 3] | [3, 5, 1, 6, 7, 2, 4]
[4] | [1]
Ketika dua atau lebih elemen dengan nilai yang sama muncul, indeks mereka akan muncul bersebelahan dari yang terkecil hingga yang terbesar.
Ini adalah kode-golf , byte paling sedikit menang!
Jawaban:
Jelly , 1 byte
Cobalah online!
sumber
Dyalog APL, 1 byte
Dyalog APL memiliki built-in
operatorfungsi bawaan (terima kasih Zacharý untuk menyelesaikan ini) untuk melakukan ini.Contoh
Di sini saya mengindeks ke dalam daftar dengan indeks yang diurutkan untuk mengembalikan daftar dalam urutan menaik.
sumber
⍋
dianggap sebagai fungsi, sementara hal-hal seperti¨⍨⍣.∘/\⌿⍀⌸⍤
adalah operator.Haskell ,
4342 byte1
-indeks:Cobalah online!
-1
byte terima kasih kepada @ ØrjanJohansen!sumber
map snd.sort.(`zip`[1..])
.Python 2 , 56 byte
Solusi ini diindeks 0. Ini menyalahgunakan fakta yang
sorted()
membuat salinan daftar asli.Cobalah online!
sumber
Javascript (ES6), 39 byte
-2 byte terima kasih kepada @powelles
Ini hanya berfungsi di browser
Array.prototype.sort
yang stabil.Versi 1-diindeks (47 byte):
Cuplikan kode contoh:
sumber
[...a.keys()]
bukannyaa.map((_,i)=>i)
akan menghemat beberapa byte.Python 2 , 48 byte
Cobalah online!
sumber
__<blahblah>__
sintaksis). Saya akan melakukan beberapa Jelly, saya tidak ingin kehilangan pelatihan saya :)Perl 6 ,
2721 byteMenguji
Menguji
Terinspirasi oleh jawaban Python
Diperluas:
sumber
Bash + coreutils, 20
Cobalah online .
sumber
Cepat 4 , 82 byte
Test Suite.
Penjelasan
Di Swift,
l.sorted()
buat salinan diurutkan dari Array asli. Kami loop melalui unsur-unsur diurutkan dalam daftar dan setelah mencetak indeks setiap item dalam Array asli denganlet a=l.index(of:k)!;print(a)
, dan kemudian, untuk menjaga indeks yang benar dalam Array, kami menetapkanl[a]
untuk0
, karena tidak mempengaruhi output normal kita.Perhatikan bahwa ini adalah 0-diindeks, karena ini adalah port dari solusi Python saya. Jika Anda ingin diindeks 1, ganti
print(a)
denganprint(a+1)
atau Coba online! .sumber
R, 5 bytes
There is a builtin function for this.
sumber
order
is already a function, so you don't have to handle input usingscan()
. This would be 5 bytes.rank()
would save a byterank
answer by @JarkoDubbeldam, but I do not see it anymore.Ruby, 40 bytes
Try it online!
sumber
MATL, 2 bytes
Try it online!
Input and output are implicit.
sumber
J, 2 bytes
Try it online!
Zero-based indexing.
sumber
Octave, 17 bytes
Try it online!
Octave is like MATLAB but with inline assignment, making things possible that gives the folks at Mathworks HQ headaches. It doesn't matter what you call
y
, but you can't do without that dummy variable, as far as I know.sumber
MY, 3 bytes
MY also has a builtin for this!
Try it online!
How?
Evaluated input, grade up, then output with a newline.
Indexed however you set the index, with
⌶
/0x48
. (Can even be some weird integer like-1
or2
, the default is1
).sumber
Java 8, 128 + 19 = 147 bytes
Based on Mr. Xcoder's solution. 0-based. Lambda takes input as an
Integer[]
and returnsInteger[]
. Byte count includes lambda expression and required import.Try It Online
Ungolfed lambda
Notes
I use
Integer[]
instead ofint[]
to allow use ofArrays.asList
, which has no primitive versions.Integer
is preferred toLong
because values are used as array indices and would require casting.This ended up being shorter than my best procedural-style
List
solution because of the cost of class and method names.This also beat a solution I tried that streamed the inputs, mapped to (value, index) pairs, sorted on values, and mapped to indices, mostly because of the baggage needed to collect the stream.
Acknowledgments
sumber
j
:l->{Integer o[]=l.clone(),s[]=l.clone(),i=0;for(Arrays.sort(s);i<l.length;l[o[i]=Arrays.asList(l).indexOf(s[i++])]=0);return o;}
(19+128 bytes).Common Lisp, 82 bytes
Try it online!
sumber
Clojure, 39 bytes
sumber
{map *.key,(sort *.value,(0..* Z=> @_))}
CJam, 12 bytes
Try it online!
sumber
MATLAB / Octave, 29 bytes
Try it online!
sumber
@(X)([~,y]=sort(X))
, and while I was looking of a way to gety
from this, I realizedy
was actually the return value from the assignment, and closer inspection revealed that brackets weren't even needed. MATLAB likes everything explicit; Octave is happy when it's unambiguous.JavaScript (ES6), 69 bytes
0-indexed. Works for lists containing up to 65,536 elements.
Test cases
Show code snippet
sumber
n=>a.indexOf(n)
to justa.indexOf
?Array#map
passes 3 arguments to the callback function, andArray#indexOf
expects 2, so it will give undesirable results.Python 3, 52 bytes
0-indexed. Based on Bruce Forte's Haskell answer here and G B's Ruby answer here.
Try it online!
sumber
Husk,
107 bytesThis is a direct port of my Haskell answer, also
1
-indexed:Try it online!
Ungolfed/Explained
sumber
Java (OpenJDK 8), 72 bytes
Try it online!
Takes a
List<Integer>
, returns aStream<Integer>
containing the results.We get a Stream based off the initial list, sort it, then map each number to it's index in the list. In order to accommodate duplicate elements, we set the original element in the list to
0
.sumber
SmileBASIC, 67 bytes
Very simple, all it does is generate a list of numbers from 1 to (length of array) and sort this by the same order as the input.
sumber
Python 3 with Numpy,
3826 bytes12 bytes saved thanks to Jo King (no need to give the function a name)
Output is 0-based.
Try it online!
sumber
numpy.argsort
without the lambda partnumpy.argsort;import numpy
I get an error (numpy
has not been imported yet), and withimport numpy;numpy.argsort
I need to movef=
to the code part. Do you know that the standard procedure is in these cases? Move thef=
and not count it?f=numpy.argsort
in the footer05AB1E, 4 bytes
Try it online!
sumber
Pari/GP, 16 bytes
Try it online!
sumber
PHP, 54 bytes
Try it online!
This is zero-indexed. Simply sorts the array and returns the keys.
sumber
<?php
tag is unnecessary for a function. 48 bytes.Tcl, 21 bytes
(0-indexed)
Try it online!
sumber