20
\$\begingroup\$

Your challenge today is to write a program or function which takes a list l and gives the positions in l at which each successive element of l sorted appears.

In other words, output the index of the smallest value, followed by the index of the second smallest value, etc.

You can assume that the input array will contain only positive integers, and will contain at least one element.

Test cases:

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]

When two or more elements with the same value appear, their indices should appear next to each other from smallest to largest.

This is , fewest bytes wins!

\$\endgroup\$
9
  • 18
    \$\begingroup\$ -1 for a trivial challenge that can be solved with built-ins in common golfing languages, and for accepting an answer in less than 24 hours. This was neither a fair challenge, nor an interesting one. \$\endgroup\$ Commented Jul 30, 2017 at 11:44
  • 3
    \$\begingroup\$ Well, I get why he accepted an answer within 24 hours, it's impossible to beat. \$\endgroup\$
    – Adalynn
    Commented Jul 30, 2017 at 14:28
  • 3
    \$\begingroup\$ @CodyGray I thought downvoting when I saw the 1-2 bytes answer, but actually, I don't think it's a bad challenge for more standard programming languages. Of course, it's not a hard challenge, but still, there is definitely some golfing possibilities. Of course, it's unpleasant to see 1-byte built-ins, but I don't think that it's fair to blame the challenge for that. \$\endgroup\$
    – Dada
    Commented Jul 30, 2017 at 16:07
  • 1
    \$\begingroup\$ Using a 1 character builtin is hardly practice. Easy doesn't necessarily mean solvable using only builtins. \$\endgroup\$
    – JAD
    Commented Jul 31, 2017 at 7:36
  • 2
    \$\begingroup\$ The best solution in such cases is to forget about te accept feature, which isn't really relevant anyway here. \$\endgroup\$
    – Mr. Xcoder
    Commented Jul 31, 2017 at 9:01

38 Answers 38

1
2
2
\$\begingroup\$

Desmos, 27 bytes

Yields a 1-indexed result.

f(l)=sort([1...l.length],l)

Try it on Desmos!

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

05AB1E, 4 bytes

ā<Σè

Try it online!

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

Pari/GP, 16 bytes

a->vecsort(a,,1)

Try it online!

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

PHP, 54 bytes

<?php function i($a){asort($a);return array_keys($a);}

Try it online!

This is zero-indexed. Simply sorts the array and returns the keys.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The <?php tag is unnecessary for a function. 48 bytes. \$\endgroup\$
    – Titus
    Commented Aug 21, 2017 at 17:33
1
\$\begingroup\$

Tcl, 21 bytes

(0-indexed)

puts [lsort -indi $L]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ The test cases only have 1 digit numbers; my solution only works well on 1 digit numbers. \$\endgroup\$
    – sergiol
    Commented Oct 21, 2017 at 17:29
1
\$\begingroup\$

Ruby, 34 bytes

->a{(0...a.size).sort_by{|i|a[i]}}

0-indexed, 1-indexing requires one more net byte:

->a{(1..a.size).sort_by{|i|a[i-1]}}

Also, a slightly golfed version of G B's answer is 37 bytes:

->a{a.each_with_index.sort.map &:pop}
\$\endgroup\$
1
\$\begingroup\$

Kotlin,  44  34 bytes

not today, crossed out 44

{l->(1..l.size).sortedBy{l[it-1]}}

Try it online!

1-indexed because that's what the examples give. A 0-indexed version looks like this for 30 bytes:

{l->l.indices.sortedBy{l[it]}}
\$\endgroup\$
1
\$\begingroup\$

Japt, 6 5 bytes

0-indexed

ð ñ@v

Try it


Alternative, 6 bytes

í ñÎmÌ

Try it

\$\endgroup\$
1
2

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.