Skip to main content
Bounty Ended with 100 reputation awarded by Adám
added 4 characters in body
Source Link
Kamila Szewczyk
  • 12.6k
  • 1
  • 31
  • 61

APL (Dyalog Unicode), 2348 bytes

f←{1≥≢∘∪⍵1≥≢∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}

Try it online!Try it online!

Alternate versions of 1+@(⊃⍋⍵)⊢⍵: ⍵+(⍳≢⍵)=⊃⍋⍵, ⊢+⍳∘≢=⊃∘⍋. -12 thanks to @Adam and @Razetime.

Explanation:

{1≥≢∘∪⍵1≥≢∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}
 1≥≢∘∪⍵1≥≢∪⍵                   ⍝ if all elements are identical
        ⍵,1               ⍝ then yield ⍵ with 1 appended to the end.
           ⋄              ⍝ ... else,
               (⊃⍋⍵)      ⍝ index of the smallest value
            1+@     ⊢⍵    ⍝ increment the value from ⍵

APL (Dyalog Unicode), 23 bytes

{1≥≢∘∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}

Try it online!

Alternate versions of 1+@(⊃⍋⍵)⊢⍵: ⍵+(⍳≢⍵)=⊃⍋⍵, ⊢+⍳∘≢=⊃∘⍋. -1 thanks to @Adam.

Explanation:

{1≥≢∘∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}
 1≥≢∘∪⍵                   ⍝ if all elements are identical
        ⍵,1               ⍝ then yield ⍵ with 1 appended to the end.
           ⋄              ⍝ ... else,
               (⊃⍋⍵)      ⍝ index of the smallest value
            1+@     ⊢⍵    ⍝ increment the value from ⍵

APL (Dyalog Unicode), 48 bytes

f←{1≥≢∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}

Try it online!

Alternate versions of 1+@(⊃⍋⍵)⊢⍵: ⍵+(⍳≢⍵)=⊃⍋⍵, ⊢+⍳∘≢=⊃∘⍋. -2 thanks to @Adam and @Razetime.

Explanation:

{1≥≢∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}
 1≥≢∪⍵                   ⍝ if all elements are identical
       ⍵,1               ⍝ then yield ⍵ with 1 appended to the end.
          ⋄              ⍝ ... else,
              (⊃⍋⍵)      ⍝ index of the smallest value
           1+@     ⊢⍵    ⍝ increment the value from ⍵
added 5 characters in body
Source Link
Kamila Szewczyk
  • 12.6k
  • 1
  • 31
  • 61

APL (Dyalog Unicode), 2123 bytes

{=/⍵1≥≢∘∪⍵:⍵,1⋄⍵+1⋄1+@(⍳≢⍵⊃⍋⍵)=⊃⍋⍵⊢⍵}

Try it online!

Alternate versionversions of 1+@(⊃⍋⍵)⊢⍵: ⍵+(⍳≢⍵)=⊃⍋⍵:, ⊢+⍳∘≢=⊃∘⍋. -1 thanks to @Adam.

Explanation:

{=/⍵1≥≢∘∪⍵:⍵,1⋄⍵+1⋄1+@(⍳≢⍵⊃⍋⍵)=⊃⍋⍵⊢⍵}
 =/⍵   1≥≢∘∪⍵                   ⍝ if every indiceall iselements equalare (doesn'tidentical
 work for ⍵=0)
     ⍵,1                  ⍝ then yield ⍵ with 1 appended to the end.
                         ⍝ ... else,
                 (⊃⍋⍵) compute the index of the smallest element
           (⍳≢⍵)          ⍝ compute indices from 1 to the length of the inputvalue
                =1+@     ⊢⍵ a vector with 1 at the position to increment
         ⍵+               ⍝ yield the vectors'value sumfrom ⍵

Try it online!

APL (Dyalog Unicode), 21 bytes

{=/⍵:⍵,1⋄⍵+(⍳≢⍵)=⊃⍋⍵}

Alternate version of ⍵+(⍳≢⍵)=⊃⍋⍵: ⊢+⍳∘≢=⊃∘⍋

Explanation:

{=/⍵:⍵,1⋄⍵+(⍳≢⍵)=⊃⍋⍵}
 =/⍵                      ⍝ if every indice is equal (doesn't work for ⍵=0)
     ⍵,1                  ⍝ then yield ⍵ with 1 appended to the end.
                         ⍝ ... else,
                 ⊃⍋⍵      ⍝ compute the index of the smallest element
           (⍳≢⍵)          ⍝ compute indices from 1 to the length of the input
                = a vector with 1 at the position to increment
         ⍵+               ⍝ yield the vectors' sum

Try it online!

APL (Dyalog Unicode), 23 bytes

{1≥≢∘∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}

Try it online!

Alternate versions of 1+@(⊃⍋⍵)⊢⍵: ⍵+(⍳≢⍵)=⊃⍋⍵, ⊢+⍳∘≢=⊃∘⍋. -1 thanks to @Adam.

Explanation:

{1≥≢∘∪⍵:⍵,1⋄1+@(⊃⍋⍵)⊢⍵}
 1≥≢∘∪⍵                   ⍝ if all elements are identical
        ⍵,1               ⍝ then yield ⍵ with 1 appended to the end.
                         ⍝ ... else,
               (⊃⍋⍵)      ⍝ index of the smallest value
            1+@     ⊢⍵    ⍝ increment the value from ⍵
Source Link
Kamila Szewczyk
  • 12.6k
  • 1
  • 31
  • 61

APL (Dyalog Unicode), 21 bytes

{=/⍵:⍵,1⋄⍵+(⍳≢⍵)=⊃⍋⍵}

Alternate version of ⍵+(⍳≢⍵)=⊃⍋⍵: ⊢+⍳∘≢=⊃∘⍋

Explanation:

{=/⍵:⍵,1⋄⍵+(⍳≢⍵)=⊃⍋⍵}
 =/⍵                      ⍝ if every indice is equal (doesn't work for ⍵=0)
     ⍵,1                  ⍝ then yield ⍵ with 1 appended to the end.
        ⋄                 ⍝ ... else,
                 ⊃⍋⍵      ⍝ compute the index of the smallest element
           (⍳≢⍵)          ⍝ compute indices from 1 to the length of the input
                =         ⍝ a vector with 1 at the position to increment
         ⍵+               ⍝ yield the vectors' sum

Try it online!