A Spintax {Task|Problem|Question}
Pyth, 18
V5sOMcR\|scR\{cz\}
Try it online here
Explanation:
V5sOMcR\|scR\{cz\} : z = input()
V5 : do this 5 times
cz\} : split z on the } character
cR\{ : split each element of the resulting list on {
s : join the list of lists back into a list of strings
cR\| : split these strings on the | character
OM : Choose a random element from each inner list
s : join those choices, and print them
Ruby, 46
With command-line flag -p
, run
($_*=5).gsub!(/{(.+?)}/){$1.split(?|).sample}
Read in a string with a trailing newline. Concatenate it to itself 5 times, mutate it in place, and output the result. /{(.+?)}/
is a regular expression with a lazy quantifier: without the ?
, it would match the leftmost {
to the rightmost }
instead of the nearest one. $1
is a magic variable referring to the first capture group in each match, while the ?|
literal refers to the |
character. sample
is an array method returning a random element.
CJam, 25 22 19 18 bytes
q5*'}/{'{/~'|/mR}/
This code requires the input to contain a trailing newline.
Try it online in the CJam interpreter.
How it works
q e# Read from STDIN.
5* e# Repeat the input five times.
'}/ e# Split at right curly brackets.
{ e# For each chunk:
'{/ e# Split at left curly brackets.
~ e# Dump both chunks on the stack.
'|/ e# Split the right chunk at vertical bars.
mR e# Select a chunk, pseudo-randomly.
}/ e#
In each iteration, two chunks (one constant, one variable) are left on the stack and will be printed when the program exits.