Moving modest minimum
Python 2, 41 bytes
lambda l:[sorted(l)[x==min(l)]for x in l]
Try it online!
For each element x
we check whether x==min(l)
. If not, this is False
, which is treated as 0
when used as a list index into sorted(l)
, giving the smallest element. Otherwise, it's True
aka 1
, giving the second-smallest element, since that element itself is smallest and should be ignored.
Jelly, 9 6 5 bytes
JḟЀ`ị⁸Ṃ€ṙJṖ€Ṃ€ṙJṖ«/ argument: 1D array (z) J [1,2,3,...,len(z)] ṙ rotate z by each of the above amount (current array is 2D) Ṗ remove the last array «/ reduce by [impliclitly vectorized] minimum
Try it online!
Verify all of them at once! (slightly modified)
I'm pretty sure Dennis can out-golf this.
How it works
The algorithm is rather convoluted. Let us observe what this does to [4,2,2,5]
.
Firstly, we use J
to obtain [1,2,3,4]
. Note that Jelly uses 1-indexing.
Then, we see ṙ
. It takes two arguments: an array and an integer. It rotates the array to the left by an amount specified by the integer. Here, ṙ
would see [4,2,2,5]
on its left and [1,2,3,4]
on its right (more about how this works can be found in the tutorial). In Jelly, commands implicitly vectorize. Therefore, this command will be performed over each individual element on the right, which is why we would create a 2D array:
Therefore, [4,2,2,5]ṙ[1,2,3,4]
becomes [[4,2,2,5]ṙ1,[4,2,2,5]ṙ2,[4,2,2,5]ṙ3,[4,2,2,5]ṙ4]
, which becomes:
[[2,2,5,4], [2,5,4,2], [5,4,2,2], [4,2,2,5]]
Notice that the original elements are on the last row, since in that row we rotated to the left by an amount equal to the length of the array, which is why we use Ṗ
next to remove that row, so that the columns are the collections of the elements of the array which are not at the current index:
[[2,2,5,4], [2,5,4,2], [5,4,2,2]]
The following operation, «/
, is also quite convoluted. Firstly, «
returns the minimum of the two numbers it sees on its left and on its right. For example, 5«3
returns 3
. Now, if the two arguments are arrays, then it would vectorize as I have said above. What this means it that [1,5,2,3]«[4,1,5,2]
would become [1«4,5«1,2«5,3«2]
which is [1,1,2,2]
. Now, /
is reduce
, which means that we do the operation over each row until the end. For example, [1,2,3,4]+/
would become ((1+2)+3)+4
, which is the sum of the array [1,2,3,4]
.
So, if we apply «/
to the 2D array we just obtained, we would get:
([2,2,5,4]«[2,5,4,2])«[5,4,2,2]
which, because of the vectorization, would be equivalent to:
[2«2«5,2«5«4,5«4«2,4«2«2]
which computes the minimum of every array without the element at the index.
Jelly, 5 bytes
=Ṃ‘ịṢ
Try it online!
How?
=Ṃ‘ịṢ - Main link: list a e.g. [4,3,2,5]
Ṃ - minimum of a 2
= - equals? (vectorises) [0,0,1,0]
‘ - increment [1,1,2,1]
Ṣ - sort a [2,3,4,5]
ị - index into [2,2,3,2]