Mad Libs: Difference between revisions

Content deleted Content added
m Remove a pointless comment that was accidentally left in C solution
Made more functional
Line 1,080: Line 1,080:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Like some other examples, this prompts the user for a filename for a story template rather than reading the story in from user input.
Like some other examples, this prompts the user for a filename for a story template rather than reading the story in from user input.
<lang Mathematica>filename = InputString["Enter the filename of the story template: "];
<lang Mathematica>text = Import[
InputString["Enter the filename of the story template:"]];
text = Import[filename];
answers =
listofblanks = StringCases[text, RegularExpression["<[^>]+>"]] // Union;
InputString[
listofanswers = {};
"Enter a" <>
Do[
If[StringMatchQ[#, Alternatives @@ Characters["aeiou"] ~~ ___],
answer = InputString["Enter a/an: " <> listofblanks[[i]]];
"n", ""] <> " " <> # <> ":"] & /@
AppendTo[listofanswers, answer];
StringTrim[StringCases[text, RegularExpression["<[^>]+>"]],
, {i, 1, Length[listofblanks]}
"<" | ">"];
]
Print[ToString[
Do[
StringForm @@
text = StringReplace[text, listofblanks[[i]] -> listofanswers[[i]]]
Prepend[answers,
, {i, 1, Length[listofblanks]}
StringReplace[text, RegularExpression["<[^>]+>"] -> "``"]]]];</lang>
]
text</lang>
{{out}}
{{out}}
<pre>George went for a walk in the park. he
<pre>George went for a walk in the park. he