14
1
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 code-golf, fewest bytes wins!
16-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. – Cody Gray – 2017-07-30T11:44:06.353
3Well, I get why he accepted an answer within 24 hours, it's impossible to beat. – Zacharý – 2017-07-30T14:28:40.253
3@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. – Dada – 2017-07-30T16:07:59.550
I'll just leave this here. Lots of good suggestions in there; this one is especially relevant.
– Cody Gray – 2017-07-30T16:12:30.7901Using a 1 character builtin is hardly practice. Easy doesn't necessarily mean solvable using only builtins. – JAD – 2017-07-31T07:36:26.867
I mean, let's just suppose for the sake of argument that a 1-character answer using a built-in is a good, competitive answer and should be accepted. It is still not true that the answer is "unbeatable". It could certainly be tied, and then what would you do? Oh, look—and it already has been tied with an APL answer. Which goes back to it's just generally unsporting to accept an answer to a code-golf question the same day that you post the question. – Cody Gray – 2017-07-31T08:46:01.767
2The best solution in such cases is to forget about te accept feature, which isn't really relevant anyway here. – Mr. Xcoder – 2017-07-31T09:01:48.083
The test cases only have 1 digit numbers; my solution only works well on 1 digit numbers. Is there any problem? – sergiol – 2017-10-21T17:28:48.823