55
\$\begingroup\$

Write a function or program that takes two words as input and outputs variants of the popular English tongue-twister "How much wood would a woodchuck chuck if a woodchuck could chuck wood?".

The output will use the first word four times

  • How much wood would a woodchuck chuck if a woodchuck could chuck wood?

and the second word four times

  • How much wood would a woodchuck chuck if a woodchuck could chuck wood?

with the rest of the output being the same for any inputs.

  • How muchwoodwould awoodchuckchuckif awoodchuckcouldchuckwood?

The input and output can be in any format that your language reasonably recognizes as dealing with strings of text. The output must be exactly in the indicated format, including capitalization, spaces and lack thereof, and the ending question mark. An optional trailing newline is acceptable.

Ideally your code will handle input containing any printable ASCII characters. However, it is permitted to restrict the input to reasonable subsets of printable ASCII; just indicate this in your answer. Handling larger character sets is of course fine.

Example input-output pairs:

"wood", "chuck"
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?"

"ground", "hog"
"How much ground would a groundhog hog if a groundhog could hog ground?"

"bar", "keep"
"How much bar would a barkeep keep if a barkeep could keep bar?"

"money", "belt"
"How much money would a moneybelt belt if a moneybelt could belt money?"

"rain", "fall"
"How much rain would a rainfall fall if a rainfall could fall rain?"

"hair", "cut"
"How much hair would a haircut cut if a haircut could cut hair?"

"green", "house"
"How much green would a greenhouse house if a greenhouse could house green?"

"jabber", "wock"
"How much jabber would a jabberwock wock if a jabberwock could wock jabber?"

"pine", "apple"
"How much pine would a pineapple apple if a pineapple could apple pine?"

"Rob", "Lowe"
"How much Rob would a RobLowe Lowe if a RobLowe could Lowe Rob?"

"code", "golf"
"How much code would a codegolf golf if a codegolf could golf code?"

"fish", ""
"How much fish would a fish  if a fish could  fish?"

"", "fish"
"How much  would a fish fish if a fish could fish ?"

"", ""
"How much  would a   if a  could  ?"

"  ", "     "
"How much    would a               if a         could         ?"

"would a", "how much"
"How much would a would a would ahow much how much if a would ahow much could how much would a?"

This is , so fewest bytes wins. Answers are welcome in all languages, even if some other language can do it in fewer bytes.

(Inspired by this meme, which uses one input pair better than this rigid pattern does....)

\$\endgroup\$
  • 2
    \$\begingroup\$ May we assume the two words will be two distinct words? \$\endgroup\$ – Jonathan Allan Aug 19 at 18:12
  • 4
    \$\begingroup\$ ...I guess "", "" means no :p \$\endgroup\$ – Jonathan Allan Aug 19 at 18:25
  • 1
    \$\begingroup\$ @Chronocidal I can't stand to see you disappointed.... \$\endgroup\$ – Greg Martin Aug 20 at 16:33
  • 6
    \$\begingroup\$ Two. It would golf two code. \$\endgroup\$ – user2357112 Aug 20 at 20:31
  • 1
    \$\begingroup\$ Aw, I was hoping this would be about a short program that can do some simple golfing transformation(s) on other code. \$\endgroup\$ – aschepler Aug 20 at 23:48

52 Answers 52

40
\$\begingroup\$

Python 3, 70 67 bytes

"How much {0} would a {0}{1} {1} if a {0}{1} could {1} {0}?".format

Try it online!

I mean, if the shoe fits..

Thanks to manatwork for catching a typo

Thanks to Remco Haszing for the excellent -3 bytes idea

I am running off of the assumption that this would still be a valid submission (because man, it's too cool not to try). If OP could clarify whether this is acceptable (because I haven't written a function, per se), that would be appreciated.

Update: Blessing received, all is good :)


Previous version:

lambda a,b:f"How much {a} would a {a+b} {b} if a {a+b} could {b} {a}?"
\$\endgroup\$
  • 2
    \$\begingroup\$ Maybe irrelevant, but the automatic format string is not readily available in python versions prior to 3.6 \$\endgroup\$ – M.Herzkamp Aug 20 at 8:51
  • 3
    \$\begingroup\$ @M.Herzkamp So? \$\endgroup\$ – ruohola Aug 20 at 10:09
  • 16
    \$\begingroup\$ @ruohola I think M.Herzkamp is trying to say the title to this answer should be "Python 3.6" rather than "Python 3". \$\endgroup\$ – Mr Lister Aug 20 at 15:09
  • 8
    \$\begingroup\$ Even shorter and Python 2 compatible (67 bytes): "How much {0} would a {0}{1} {1} if a {1}{0} could {1} {0}?".format. This returns a function bound to the unformatted string. \$\endgroup\$ – Remco Haszing Aug 21 at 15:17
  • 1
    \$\begingroup\$ I'm no Python expert, but since print("How much {0} would a {0}{1} {1} if a {0}{1} could {1} {0}?".format("wood","chuck")) generates the desired output, I see no reason not to accept it :) \$\endgroup\$ – Greg Martin Aug 22 at 21:46
25
\$\begingroup\$

T-SQL, 82 bytes

SELECT'How much '+w+' would a '+w+c+' '+c+' if a '+w+c+' could '+c+' '+w+'?'FROM t

Input is taken from pre-existing table \$t\$ with columns \$w\$ and \$c\$, per our IO rules.

One byte longer, but for some reason slightly more pleasing:

SELECT REPLACE(REPLACE('How much 1 would a 12 2 if a 12 could 2 1?',1,w),2,c)FROM t

This version works on a subset of inputs that don't include the numeral 2 in the first word \$w\$.

Because I'm in SQL, I can pre-load all examples into the table, and run them all at once:

enter image description here

\$\endgroup\$
  • 2
    \$\begingroup\$ +1000000 virtual trains for megatrain \$\endgroup\$ – Greg Martin Aug 19 at 19:34
  • 5
    \$\begingroup\$ I assume that "w" and "c" stand for "wood" and "chuck" respectively :P \$\endgroup\$ – immibis Aug 20 at 23:03
  • \$\begingroup\$ This can be done in 67 bytes in Oracle as called from SQL*Plus (so no table): select'How much &1 would a &1&2 &2 if a &1&2 could &2 &1?'from dual \$\endgroup\$ – Ben Aug 22 at 21:20
16
\$\begingroup\$

Bash, 50 bytes

echo How much $2 {would,$1\ if}\ a\ $2$1 could $@?

Try it online!

-5 bytes due to help from comments below.

\$\endgroup\$
  • \$\begingroup\$ -3 bytes \$\endgroup\$ – Grimy Aug 20 at 12:23
  • 1
    \$\begingroup\$ -2 bytes more \$\endgroup\$ – Nahuel Fouilleul Aug 20 at 13:57
  • 1
    \$\begingroup\$ You added an unnecessary backslash when adapting Nahuel's golf. Here's 53.(Though really it should be 50, nothing stops you from taking the first word as $2 and the second word as $1.) \$\endgroup\$ – Grimy Aug 20 at 22:30
  • 2
    \$\begingroup\$ @Grimy: I see the comment on the challenge from original poster allowing the reversing of the parameters. I'm updating the answer to the excellent answer of 50 bytes. Thanks! \$\endgroup\$ – spuck Aug 21 at 14:52
  • 1
    \$\begingroup\$ @roblogic: The backslashes quote the spaces to avoid word splitting on the spaces, and the curly braces are expanded twice to form two words, once with each string inside the braces, and the characters that trail the braces (but part of the word) are added to the end of each string. Adding square braces to help visualize the word splitting: “[a{bc,de}f]” becomes two words: “[abcf] [adef]”. So “[{would,$1\ if}\ a\ $2$1]” becomes “[would\ a\ $2$1] [$1\ if\ a\ $2$1]” \$\endgroup\$ – spuck Aug 25 at 4:11
15
\$\begingroup\$

Stax, 33 31 30 29 bytes

-1 thanks to recursive!

¢èO∩sP↑å♥|1╧ì}ò♂xb■δå«█Γ╨╦►Q²

Run and debug it at staxlang.xyz!

Push each component to the stack in reverse order, then join all with spaces.

Unpacked (35 bytes) and explanation:

X'?+;`IM'`x;+Y`~^$`,y`75\`x`Q)("`LJ
X                                      Set register X to the first word
                                       "wood"
 '?+                                   Append a question mark, popping from the input stack
                                       "wood?"
    ;                                  Peek from input stack and push to main stack
                                       "chuck" "wood?"
     `IM'`                             Literal "could"
                                       "could" "chuck" "wood?"
          x;+Y                         Peek register x. Peek input. Concatenate. Set register Y.
                                       "woodchuck" "could" "chuck" "wood?"
              et cetera, ad nauseam
                                  LJ   Listify the stack and join with spaces
                                       Implicit print

Everything between `` is compressed string literal. That comma is vital. The last time I read from the input stack, I must pop rather than peek to avoid an extra "chuck" on the end of my output.

You'll notice that I put both inputs on the same line for a few test cases, and that they're in reverse order. This is necessary in order to take empty strings or strings of spaces as input.

27 26 bytes with restrictions on input

å▓Zf╢7)╪♪²p╞8ó╪l▼]<¡REïSèΣ

Run and debug it at staxlang.xyz!

Just like @dzaima's SOGL, this will fail if the first input contains the lowercase letter 'y'. Pushes the string "How much b would a by y if a by could y b?", then makes a pair of replacements.

\$\endgroup\$
  • \$\begingroup\$ "You'll notice that I put both inputs on the same line for a few test cases, and that they're in reverse order. This is necessary to take empty strings or strings of spaces as input." If you use no input separator, the input will be undisturbed, empty strings and all. It just makes it kind of awkward to show multiple test cases. \$\endgroup\$ – recursive Aug 20 at 17:25
  • 1
    \$\begingroup\$ Additionally, it seems the leading , in your 31 byte solution can be removed. The following + implicitly pops from the input stack, so the behavior is identical. \$\endgroup\$ – recursive Aug 20 at 17:38
13
\$\begingroup\$

JavaScript, 70 bytes

Boring!

a=>b=>`How much ${a} would a ${a+b} ${b} if a ${a+b} could ${b} ${a}?`

Try it online!

Mildly less boring!

a=>"How much 0 would a 01 1 if a 01 could 1 0?".replace(/\d/g,x=>a[x])

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Are you allowed to output a curried function like in that first example? \$\endgroup\$ – Feathercrown Aug 22 at 16:04
  • \$\begingroup\$ @Feathercrown, I'm not outputting a function, I'm calling both (e.g., f(a)(b)) as you can see in the TIO. But yes, our consensuses to allow curried functions and I think we're close to a consensus on allowing a function to actually be returned. \$\endgroup\$ – Shaggy Aug 22 at 16:22
  • \$\begingroup\$ By output I meant the code returns a function when evaluated; no calling is in the given code that counts for bytes. I was just checking to make sure currying was okay, since I've only ever seen noncurried functions returned. \$\endgroup\$ – Feathercrown Aug 23 at 3:42
6
\$\begingroup\$

SOGL, 32 30 bytes

^.](9V;⅜‛°@Ε¬tπs%.½Ω‘⁽ b,ŗ y,ŗ

Try it here!

The first input can't contain the letter y, which seems to leave a reasonable subset of ASCII (and unicode) left.

½ouiīZģ9Ο|ΧyΚ⅞ō÷Jeq(‚7‘ is a compressed string of "how much b would a by y if a by could y b?" (characters chosen so the required words are all in the top 512 words of the dictionary which compress better), then b is replaced with the 1st input and y with the 2nd.

\$\endgroup\$
  • \$\begingroup\$ +2 internet points for one of the most random restrictions I can remember (in your shorter solution) \$\endgroup\$ – Greg Martin Aug 19 at 19:31
  • \$\begingroup\$ @GregMartin Not that random. I don't speak SOGL, but I ended up with the same restriction on 'n' when sequencing two replacements y -> wood and n -> chuck. A tenth of a Dignity Point says that's the case here as well. \$\endgroup\$ – Khuldraeseth na'Barya Aug 19 at 19:38
  • \$\begingroup\$ I think the main spirit of this question is to be open about allowing solutions ... only as an aside might I opine that not allowing an actual letter in an input is a bit sketchy. Anyway: why is b not allowed in the first input yet e is ok in the second input? \$\endgroup\$ – Greg Martin Aug 19 at 20:36
  • \$\begingroup\$ @GregMartin whoops, I meant to say that the 1st input can't contain e, as otherwise it'd be replaced with the 2nd input in the 2nd replacement \$\endgroup\$ – dzaima Aug 19 at 20:38
  • \$\begingroup\$ Is it not possible for replacement target to be non-alpha to avoid having to ban an alpha from the input? \$\endgroup\$ – WGroleau Aug 20 at 14:34
5
\$\begingroup\$

R, 90 77 76 bytes

-13 thanks to Sumner18
-1 thanks to Giuseppe

function(x,y,`[`=gsub)2[y,1[x,"How much 1 would a 12 2 if a 12 could 2 1?"]]

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ Can do a double gsub for -13 bytes instead. tio.run/… \$\endgroup\$ – Sumner18 Aug 19 at 20:08
  • \$\begingroup\$ @Sumner18 Thanks. Yeah, gsub definitely makes more sense. \$\endgroup\$ – Robert S. Aug 19 at 20:15
  • \$\begingroup\$ If you feel like using pryr, you can get it down to 73. \$\endgroup\$ – Khuldraeseth na'Barya Aug 19 at 21:14
  • 4
    \$\begingroup\$ You should specify that you assume the input x doesn't include the character 2; it fails in this case (TIO). \$\endgroup\$ – Robin Ryder Aug 19 at 21:24
  • 4
    \$\begingroup\$ 76 with aliasing of gsub \$\endgroup\$ – Giuseppe Aug 19 at 22:19
4
\$\begingroup\$

PHP, 72 bytes

[,$a,$b]=$argv;echo"How much $a would a $a$b $b if a $a$b could $b $a?";

Try it online!

Or:

PHP, 72 bytes

How much <?=![,$a,$b]=$argv,"$a would a $a$b $b if a $a$b could $b $a?";

Try it online!

Input from command line, output to STDOUT.

\$\endgroup\$
4
\$\begingroup\$

JavaScript (V8), 72 bytes

(a,b)=>['How much',a,'would a',c=a+b,b,'if a',c,'could',b,a+'?'].join` `

Try it online!

The variable assignment actually saves 0 bytes, but I figured I'd keep it in just to make this slightly unique.

\$\endgroup\$
  • \$\begingroup\$ But one big template string is 1 character shorter: Try it online!. \$\endgroup\$ – manatwork Aug 19 at 18:27
  • \$\begingroup\$ @manatwork I tried this but included an extra space by mistake :P Thanks! \$\endgroup\$ – IronFlare Aug 19 at 18:32
4
\$\begingroup\$

ZX Spectrum Basic, 87 bytes

Just for completeness, straightforward implementation:

INPUT a$,b$: PRINT "How much ";a$;" would a ";a$;b$;"  ";b$;" if a ";a$;b$;" could ";b$;" ";a$;"?"

Using the IF keyword (1 byte) golfes it down by 3 bytes, but breaks the "same capitalization" condition:

INPUT a$,b$: PRINT "How much ";a$;" would a ";a$;b$;"  ";b$;" IF a ";a$;b$;" could ";b$;" ";a$;"?"
\$\endgroup\$
  • \$\begingroup\$ It's nice to show slightly-cheaty versions too :) \$\endgroup\$ – Greg Martin Aug 20 at 16:37
  • 2
    \$\begingroup\$ Oh, Speccy! To be young again... \$\endgroup\$ – chx Aug 21 at 4:34
4
\$\begingroup\$

Rust, 75 bytes

|a,b|print!("How much {} would a {0}{} {1} if a {0}{1} could {1} {0}?",a,b)

Try it online!

Using this trick, which let's you skip the formatting index once per item to format.

Also using print!(), because it's one byte shorter than building a string with format!() and returning it.

\$\endgroup\$
4
\$\begingroup\$

05AB1E, 37 35 31 30 bytes

“Howƒ×1€Þ a ÿ0€¬ a ÿƒˆ01?“T$ú‡

-5 bytes thanks to @Grimy.

Takes a list of two items: wood as first value and chuck as second.

Try it online or verify all test cases.

Explanation:

“Howƒ×1€Þ a ÿ0€¬ a ÿƒˆ01?“
      # Push dictionary string "How much1 would a ÿ0 if a ÿ could01?",
      # where the `ÿ` are automatically filled with the (implicit) input-list,
      # implicitly joined together to a single string
      #  i.e. ["wood","chuck"] → "How much1 would a woodchuck0 if a woodchuck could01?"
T     # Push 10
 $    # Push the input-list and 1
  ù   # Pad the strings in the input-list with this 1 amount of leading spaces
      #  ["wood","chuck"] → [" wood"," chuck"]
   ‡  # Transliterate the 10 ([1,0]) to these strings in the sentence
      #  → "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
      # (after which the result is output implicitly)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why “Howƒ×1€Þ a ÿ0€¬ a ÿƒˆ01?“ is "How much1 would a ÿ0 if a ÿ could01?".

\$\endgroup\$
  • 1
    \$\begingroup\$ 31 \$\endgroup\$ – Grimy Aug 20 at 11:51
  • 1
    \$\begingroup\$ 30 \$\endgroup\$ – Grimy Aug 20 at 12:04
  • 1
    \$\begingroup\$ @Grimy Very nice! I really like both of those. Ingenious idea of using the like that to save the additional byte, thanks. \$\endgroup\$ – Kevin Cruijssen Aug 20 at 12:15
  • 1
    \$\begingroup\$ Alternate 30. I'm not finding any 29, so your explanation is safe for now ;) \$\endgroup\$ – Grimy Aug 20 at 12:18
4
\$\begingroup\$

Applesoft BASIC, 77 76 bytes

1INPUTA$,B$:?"How much "A$" would a "A$B$" "B$" if a "A$B$" could "B$" "A$"?

The above may not look like proper BASIC, but Applesoft allows for a few shortcuts when using the PRINT statement:

  • Use of ? in place of PRINT when entering the statement
  • Concatenation characters (either ; or +) may be omitted
  • If the statement ends in a quoted string, the final quote may be omitted Thanks, Mark!

The line number is required, or the INPUT statement will cause an ?ILLEGAL DIRECT ERROR

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you omit the trailing quotation mark? Applesoft Basic is a Microsoft Basic derivative, and many versions of Microsoft Basic let you omit the closing quote on a string if it ends the line. \$\endgroup\$ – Mark Aug 20 at 22:25
  • \$\begingroup\$ @Mark, Thanks for tip! \$\endgroup\$ – lee Aug 21 at 2:39
4
\$\begingroup\$

33, 78 bytes

"How much "p1btpt" would a "ptpz2btp" "ptbtp" if a "ptpbtp" could "ptbtp" "ptp

Try it online!

Takes the input as command-line arguments.

Bonus: 91 bytes

"How much "p1bztp" would a "p1bztp2bztp" "p2bztp" if a "p1bztp2bztp" could "p2bztp" "p1bztp

Try it online!

Gives output resembling itself when given inputs 1bztp and 2bztp

\$\endgroup\$
  • \$\begingroup\$ Said this out loud, now I need to clean all the spit off my monitor. \$\endgroup\$ – BradC Aug 22 at 14:26
3
\$\begingroup\$

Kotlin, 59 bytes

{i,j->"How much $i would a $i$j $j if a $i$j could $j $i?"}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Kotlin sounds like a lot of fun! \$\endgroup\$ – M.Herzkamp Aug 20 at 9:28
  • 1
    \$\begingroup\$ @M.Herzkamp it sure is! \$\endgroup\$ – Quinn Aug 20 at 13:11
3
\$\begingroup\$

PowerShell, 65 bytes

param($a,$b)"How much $a would a $a$b $b if a $a$b could $b $a`?"

Try it online!

The only thing of note is that you have to escape the question mark because those can be valid parts of a PowerShell identifier

\$\endgroup\$
3
\$\begingroup\$

VBA, 107 bytes

Function q(a,b)
b=b&" "
c="ould "
q="How much "&a&" w"&c&"a "&a&b&b&"if a "&a&b&"c"&c&b&a&"?"
End Function

Should run as VBScript too, I used two shortcuts: "ould " is repeating and "chuck" never appears without an additional space.

\$\endgroup\$
  • \$\begingroup\$ You can get this down to 75 bytes by converting to immediate window function as a=[A1]:b=[B1&" "]:o="ould ":?"How much "a" w"o"a "a b b"if a "a b"c"o b a"?. Takes input from [A1] and [B1]. We have Tips for golfing in VBA that you consider taking a look into. \$\endgroup\$ – Taylor Scott Aug 25 at 18:19
  • \$\begingroup\$ Then you could take input named range [A] and [B] as inputs bringing it down to 73. I don't quite understand how yours goes through though, feel free to add your own answer. \$\endgroup\$ – user3819867 Aug 26 at 7:58
  • 1
    \$\begingroup\$ That unfortunately goes against how STDIN and STDOUT are defined for Excel VBA - you can use any unnamed range on the activesheet, but are not permitted to use named ranges per an earlier decison by the community \$\endgroup\$ – Taylor Scott Aug 27 at 19:52
3
\$\begingroup\$

C#, 165 148 133 bytes

class P{static void Main(string[]a){System.Console.Write("How much {0} would a {0}{1} {1} if a {0}{1} could {1} {0}?\n",a[0],a[1]);}}

Thanks to Andrew Baumher for telling me about interpolated strings!!
EDIT: Full class now added
EDIT: Thanks to Kenneth K. for giving me a few tips for shortening it
EDIT: Thanks to Andrew again for telling me that using interpolated string is actually longer in this scenario.

\$\endgroup\$
  • \$\begingroup\$ Using modern C#'s '$' (interpolated strings), you can save a few bytes by replacing {0} with {a[0]}, or better yet, taking two variables instead of an array so you can just use a and b. So as to not steal your thunder, so to speak, I'll add it as a different version of c# \$\endgroup\$ – Andrew Baumher Aug 19 at 18:29
  • \$\begingroup\$ Wow didn't know about that! Thanks for the info \$\endgroup\$ – canttalkjustcode Aug 19 at 19:09
  • 2
    \$\begingroup\$ @canttalkjustcode generally, functions are accepted. For C#, this includes lambdas: a=>System.Console.WriteLine($"How much {a[0]} would a {a[0]}{a[1]} {a[1]} if a {a[0]}{a[1]} could {a[1]} {a[0]}?") \$\endgroup\$ – JAD Aug 20 at 8:17
  • \$\begingroup\$ This IS true but it's only in C# Interactive that you can use lambda expressions like this. In full C# you need a full lamda statement. A simple void statement would be faster as shown in the following snippet: \$\endgroup\$ – canttalkjustcode Aug 20 at 11:34
  • \$\begingroup\$ void M(string[]a){System.Console.WriteLine($"How much {a[0]} would a {a[0]}{a[1]} {a[1]} if a {a[0]}{a[1]} could {a[1]} {a[0]}?");} System.Func<string>M=a=>System.Console.WriteLine($"How much {a[0]} would a {a[0]}{a[1]} {a[1]} if a {a[0]}{a[1]} could {a[1]} {a[0]}?"); System.Action<string>M=a=>System.Console.WriteLine($"How much {a[0]} would a {a[0]}{a[1]} {a[1]} if a {a[0]}{a[1]} could {a[1]} {a[0]}?"); \$\endgroup\$ – canttalkjustcode Aug 20 at 11:34
3
\$\begingroup\$

Haskell, 76 bytes

a?b=a++" "++b
a!b="How much"?a?"would a"?a++b?b?"if a"?a++b?"could"?b?a++"?"

Try it online!

First try, so I hope I didn't break any rules.

\$\endgroup\$
2
\$\begingroup\$

Jelly, 39 bytes

ŒPKŒP“µkþ¿µ‘ị“þ>Æƈ)taJṖ;ạʂ\4S%dñl»Ỵ¤ż”?

A full program accepting a list of two strings.

Try it online!

...Or (also a full program accepting a list of two strings)

⁽4ṀDBịs€2ṭ€€⁶“þ>Æƈ)taJṖ;ạʂ\4S%dñl»Ỵ¤ż”?

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 80 bytes

lambda n:'How much {0} would a {0}{1} {1} if a {0}{1} could {1} {0}?'.format(*n)

Try it online!

when in rome, use str format.

Edited using squid's trick.

\$\endgroup\$
  • 3
    \$\begingroup\$ Wouldn't this be invalid? It's a snippet - not a function or a program, right? \$\endgroup\$ – squid Aug 19 at 17:56
  • \$\begingroup\$ Yeah; needs a lambda n: before it, and can drop the print(). \$\endgroup\$ – wizzwizz4 Aug 19 at 18:13
  • \$\begingroup\$ You should change your code to print How much ... instead of how much .... \$\endgroup\$ – ruohola Aug 20 at 12:03
  • 3
    \$\begingroup\$ It should suffice to just define an anonymous function as 'how much {0} would a {0}{1} {1} if a {0}{1} could {1} {0}?'.format. \$\endgroup\$ – xnor Aug 20 at 18:03
  • \$\begingroup\$ @xnor Still pretty new, what would the TIO look like for that then? I'd think that without some way to pass in an arg it would be runnable. \$\endgroup\$ – Tryer Aug 20 at 19:49
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 66 65 bytes

x=>y=>$"How much {x} would a {x+y} {y} if a {x+y} could {y} {x}?"

Try it online!

same as everyone else, except C#. -1 byte by using currying strat a=>b=>c instead of (a,b)=>c

\$\endgroup\$
  • \$\begingroup\$ Change function signature from Func<string,string,string> to Func<string,Func<string,string>> and change (x,y) to x=>y=> \$\endgroup\$ – Embodiment of Ignorance Aug 20 at 2:07
  • \$\begingroup\$ Seems like a cheap tactic, but meta discussion says it's fair game, so might as well. Thanks. \$\endgroup\$ – Andrew Baumher Aug 21 at 0:14
2
\$\begingroup\$

R, 95 bytes

function(a,b)cat("How much ",a," would a ",a,b," ",b," if a ",a,b," could ",b," ",a,"?",sep='')

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to the site! Your TIO link seems to link to the wrong code? In addition, as far as I can tell, you've assumed the input to be in the variables a and b, which is disallowed under our Default Input/Output rules \$\endgroup\$ – caird coinheringaahing Aug 21 at 14:35
  • \$\begingroup\$ Thanks @cairdcoinheringaahing for the comment. I rewrote it in function form, hope it complies with the rules now. \$\endgroup\$ – minhsphuc12 Aug 21 at 14:47
  • \$\begingroup\$ Using stringr saves you a few. \$\endgroup\$ – Khuldraeseth na'Barya Aug 21 at 15:41
1
\$\begingroup\$

APL (Dyalog Unicode), 56 59 bytes

{∊'How much '⍺' would a '⍺⍵' '⍵' if a '⍺⍵' could '⍵' '⍺'?'}

Try it online!

Pretty straightforward dfn. Saves a byte by cutting if we're allowed to return an array of strings instead of a single string.

3 bytes added because I'd forgotten to add the question mark.

\$\endgroup\$
  • \$\begingroup\$ Did you count the Greek letters as one byte? \$\endgroup\$ – Tomáš Zato Aug 20 at 13:04
  • \$\begingroup\$ @TomášZato yes. Dyalog APL's glyphs can be represented in a single byte using Adám's Single Byte Character Sheet which is standard for APL answers here. \$\endgroup\$ – J. Sallé Aug 20 at 13:07
1
\$\begingroup\$

Wolfram Language (Mathematica), 68 bytes

StringRiffle@{How,much,#,would,a,c=#<>#2,#2,if,a,c,could,#2,#<>"?"}&

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 41 37 bytes

⁾be,y“Ø[gœıJ9°m.OṚuHlh3Ƥ⁾$ɲ0øḲʂṇHẎṆȥ»

Try it online!

A full program taking a pair of strings as its argument and printing the processed string. A monadic link could be formed by adding a F to the end (thanks to @JonathanAllan for pointing this out).

I’ve now switched to using "b" and "e" as placeholders, inspired by @dzaima’s SOGL answer so be sure to upvote that one too! This does mean that the first word can’t include the letter e.

\$\endgroup\$
  • \$\begingroup\$ @JonathanAllan quite right! Thanks. \$\endgroup\$ – Nick Kennedy Aug 19 at 20:31
1
\$\begingroup\$

Japt -S, 44 39 bytes

[`How Û2`U`Ùd a`N=¬V`if a`N`Öd`VU+'?]

Try it

\$\endgroup\$
1
\$\begingroup\$

Forth (gforth), 116 bytes

: x 2over type ; : y 2dup type ; : f ." How much "x ."  would a "x y ."  "y ."  if a "x y ."  could "y ."  "x ." ?";

Try it online!

Code Explanation

\ x = output the first word
: x               \ start a new word definition
  2over type      \ copy the "first" word to the top of the stack and print it
;                 \ end word definition

\ y = output the second word
: y               \ start a new word definition
  2dup type       \ copy the "second" word to the top of the stack and print it
;                 \ end word definition

: f               \ start a new word definition
  ." How much "x  \ print "How much " followed by the first word   
  ."  would a "x  \ print " would a " followed by the first word
  y ."  if a "x   \ print the second word followed by " if a " and then the first word
  y ."  could "y  \ print the second word, then " could " then the second word again
  ."  "x ." ?"    \ print a space followed by the first word, followed by "?"
;                 \ end word definition
\$\endgroup\$
1
\$\begingroup\$

Lua, 82 bytes

a,b=...print((('How much x would a xy y if a xy could y x?'):gsub('.',{x=a,y=b})))

Try it online!

Full program, take input as arguments.

Nothing special here. Hope that there's shorter version, but no obvious ways to shorten this at first glance.

\$\endgroup\$
1
\$\begingroup\$

Bash, 58 bytes

echo "How much $1 could a $1$2 $2 if a $1$2 could $2 $1?"

Takes input as command line arguments. Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.