Skip to main content
1 of 3
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#05AB1E, 12 bytes

g3›ićs¨.rIθJ

Try it online or verify some more inputs.

Fun 12 bytes alternative:

g͸1.ø£ε.r}J

Try it online or verify some more inputs.

Both alternatives also work for strings with less than 3 characters. The first one could be just ćs¨.rIθJ (8 bytes), but then it doesn't work for single-character strings ("a" becomes "aa").

Explanation:

g               # Take the length of the (implicit) input
                #  i.e. "howaboutthis" → 12
                #  i.e. "a" → 1
 3›i            # If this length is larger than 3:
                #   i.e. 12 → 1 (truthy)
                #   i.e. 1 → 0 (falsey)
    ć           #  Extract the head of the (implicit) input
                #   i.e. "howaboutthis" → "owaboutthis" and "h"
     s          #  Swap so the list (minus head) is at the top of the stack again
      ¨         #  Remove the last character
                #   i.e. "owaboutthis" → "owaboutthi"
       .r       #  Randomly shuffle the characters
                #   i.e. "owaboutthi" → "oohbtwtiua"
         Iθ     #  Take the last character of the input
                #   i.e. "howaboutthis" → "s"
           J    #  Join the values on the stack together (and output implicitly)
                #   i.e. "h", "oohbtwtiua", "s" → "hoohbtwtiuas"
                # (Implicit else)
                #  (Output the input as is implicitly)
                #   i.e. "a"

g               # Take the length of the (implicit) input
                #  i.e. "howaboutthis" → 12
                #  i.e. "a" → 1
 Í              # Subtract 2
                #  i.e. 12 → 10
                #  i.e. 1 → -1
  ¸             # Wrap it into a list
                #  i.e. 10 → [10]
                #  i.e. -1 → [-1]
   1.ø          # Surround it with 1s
                #  i.e. [10] → [1,10,1]
                #  i.e. [-1] → [1,-1,1]
      £         # Split the (implicit) input into parts of that size
                #  i.e. "howaboutthis" and [1,10,1] → ["h","owaboutthi","s"]
                #  i.e. "a" and [-1,1,1] → ["a","",""]
       ε  }     # Map each value to:
        .r      #  Randomly shuffle the characters
                #   i.e. ["h","owaboutthi","s"] → ["h","oohbtwtiua","s"]
                #   i.e. ["a","",""] → ["a","",""]
           J    # Join the values in the list together (and output implicitly)
                #  i.e. ["h","oohbtwtiua","s"] → "hoohbtwtiuas"
                #  i.e. ["a","",""] → "a"
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384