g≠ićs¨.rIθJ
Try it online or verify some more inputs.
Fun 12 11 bytes alternative:
g͸1.ø£€.rJ
Try it online or verify some more inputs.
Both version 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
≠i # If this length is not 1:
# 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"
r=id
. \$\endgroup\$id
is the identity function. I would still like to see Haskell solution to this problem in less than 100 characters. \$\endgroup\$