41
\$\begingroup\$

Inspired by a question over at Stack Overflow. The title here is entirely my fault.


The challenge

Given a list of positive integers containing at least two entries, replace each number by the minimum of all entries excluding itself.

Test cases

[4 3 2 5]    ->  [2 2 3 2]
[4 2 2 5]    ->  [2 2 2 2]
[6 3 5 5 8]  ->  [3 5 3 3 3]
[7 1]        ->  [1 7]
[9 9]        ->  [9 9]
[9 8 9]      ->  [8 9 8]

Rules

The algorithm should theoretically work for any input size (greater than one) and values (positive integers). It's accepted if the program is limited by time, memory or data types and so only works for numbers up to a given value, or for input size up to a given value.

Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.

Input can be taken by any reasonable means; and with any format. Same for output. Input and output formats may be different.

Shortest code in bytes wins.

\$\endgroup\$
6
  • \$\begingroup\$ What should [4 3 2 2 5] output? \$\endgroup\$
    – user41805
    Apr 27 '17 at 9:03
  • \$\begingroup\$ @KritixiLithos didn't the second test case cover this? \$\endgroup\$
    – Leaky Nun
    Apr 27 '17 at 9:04
  • \$\begingroup\$ @KritixiLithos For input [4 3 2 2 5] the output would be [2 2 2 2 2] (this is similar to the second test case) \$\endgroup\$
    – Luis Mendo
    Apr 27 '17 at 9:04
  • \$\begingroup\$ Oh, I missed the second test case. But now I understand how it works \$\endgroup\$
    – user41805
    Apr 27 '17 at 9:08
  • \$\begingroup\$ @LuisMendo You have changed "integer" to "any input size and values". Does that mean we need to account for all real numbers? \$\endgroup\$
    – Leaky Nun
    Apr 27 '17 at 9:34

33 Answers 33

1
2
1
\$\begingroup\$

Haskell, 76 bytes

This is considerably longer than the earlier Haskell entries, but it's the first that only performs a linear number of comparisons and a linear amount of additional work.

f(x:y)|(z,w)<-x!y=z:w
a![x]=(x,[a])
a!(x:y)|(p,q)<-a#x!y=(x#p,a#p:q)
(#)=min

Try it online!

Explanation

! takes two arguments: a running minimum and a nonempty list. It returns the minimum value in the list and the result of processing the given list using the running minimum.

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

MathGolf, 9 7 bytes

s_╓?m=§

Try it online!

Explanation

Basically a port of Kevin Cruijssen's 05AB1E answer, but I lose 2 bytes thanks to having to do things explicitly.

s         sort(array)
 _        duplicate TOS
  ╓       minimum of two elements, min of list, minimum by filter
   ?      rot3 pops input on top of stack again
    m=    explicit map to check equality
      §   get from sorted array for each
\$\endgroup\$
1
\$\begingroup\$

APL(NARS), 47 chars, 94 bytes

{m←⌊/⍵⋄⍵{⍵>≢⍺:⍬⋄t←⍺∇⍵+1⋄m=⍵⊃⍺:(⌊/⍺∼⍦m),t⋄m,t}1}

∼⍦ it seems is using for remove one element only 1 times; possible it is too much long for you... test:

  f←{m←⌊/⍵⋄⍵{⍵>≢⍺:⍬⋄t←⍺∇⍵+1⋄m=⍵⊃⍺:(⌊/⍺∼⍦m),t⋄m,t}1}
  f 4 3 2 5
2 2 3 2 
  f 4 2 2 5
2 2 2 2 
  f 6 3 5 5 8
3 5 3 3 3 
  f 7 1
1 7 
  f 9 9
9 9 
  f 8 9 8
8 8 8 
  f 9 8 9
8 9 8 
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.