# How much code would a codegolf golf if a codegolf could golf code?

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....)

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

# 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}?"

• Maybe irrelevant, but the automatic format string is not readily available in python versions prior to 3.6 – M.Herzkamp Aug 20 at 8:51
• @M.Herzkamp So? – ruohola Aug 20 at 10:09
• @ruohola I think M.Herzkamp is trying to say the title to this answer should be "Python 3.6" rather than "Python 3". – Mr Lister Aug 20 at 15:09
• 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. – Remco Haszing Aug 21 at 15:17
• 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 :) – Greg Martin Aug 22 at 21:46

# 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:

• +1000000 virtual trains for megatrain – Greg Martin Aug 19 at 19:34
• I assume that "w" and "c" stand for "wood" and "chuck" respectively :P – user253751 Aug 20 at 23:03
• 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 – Ben Aug 22 at 21:20

# 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. • -3 bytes – Grimmy Aug 20 at 12:23 • -2 bytes more – Nahuel Fouilleul Aug 20 at 13:57 • 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.) – Grimmy Aug 20 at 22:30 • @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! – spuck Aug 21 at 14:52 • @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]” – spuck Aug 25 at 4:11 # Stax, 333130 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~^$,y75\xQ)("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?"
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.

• "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. – recursive Aug 20 at 17:25
• 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. – recursive Aug 20 at 17:38

# 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!

• Are you allowed to output a curried function like in that first example? – Feathercrown Aug 22 at 16:04
• @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. – Shaggy Aug 22 at 16:22
• 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. – Feathercrown Aug 23 at 3:42

# 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.

• +2 internet points for one of the most random restrictions I can remember (in your shorter solution) – Greg Martin Aug 19 at 19:31
• @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. – Khuldraeseth na'Barya Aug 19 at 19:38
• 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? – Greg Martin Aug 19 at 20:36
• @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 – dzaima Aug 19 at 20:38
• Is it not possible for replacement target to be non-alpha to avoid having to ban an alpha from the input? – WGroleau Aug 20 at 14:34

# R, 9077 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!

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

[,$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.

# 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.

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

# 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$;"?"

• It's nice to show slightly-cheaty versions too :) – Greg Martin Aug 20 at 16:37
• Oh, Speccy! To be young again... – chx Aug 21 at 4:34

# 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.

# 05AB1E, 373531 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. 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?".

• 31 – Grimmy Aug 20 at 11:51
• 30 – Grimmy Aug 20 at 12:04
• @Grimy Very nice! I really like both of those. Ingenious idea of using the $ù like that to save the additional byte, thanks. – Kevin Cruijssen Aug 20 at 12:15 • Alternate 30. I'm not finding any 29, so your explanation is safe for now ;) – Grimmy Aug 20 at 12:18 # 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 • 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. – Mark Aug 20 at 22:25 • @Mark, Thanks for tip! – lee Aug 21 at 2:39 # 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 • Said this out loud, now I need to clean all the spit off my monitor. – BradC Aug 22 at 14:26 # Kotlin, 59 bytes {i,j->"How much$i would a $i$j $j if a$i$j could$j $i?"}  Try it online! • Kotlin sounds like a lot of fun! – M.Herzkamp Aug 20 at 9:28 • @M.Herzkamp it sure is! – Quinn Aug 20 at 13:11 # 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 # 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. • 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. – Taylor Scott Aug 25 at 18:19 • 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. – user3819867 Aug 26 at 7:58 • 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 – Taylor Scott Aug 27 at 19:52 # C#, 165148 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. • 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# – Andrew Baumher Aug 19 at 18:29
• Wow didn't know about that! Thanks for the info – canttalkjustcode Aug 19 at 19:09
• @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]}?") – JAD Aug 20 at 8:17 • 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: – canttalkjustcode Aug 20 at 11:34 • 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]}?"); – canttalkjustcode Aug 20 at 11:34

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.

# 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!

# 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.

• Wouldn't this be invalid? It's a snippet - not a function or a program, right? – Reinstate Monica Aug 19 at 17:56
• Yeah; needs a lambda n: before it, and can drop the print(). – wizzwizz4 Aug 19 at 18:13
• You should change your code to print How much ... instead of how much .... – ruohola Aug 20 at 12:03
• 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. – xnor Aug 20 at 18:03
• @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. – Tryer Aug 20 at 19:49

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 • Change function signature from Func<string,string,string> to Func<string,Func<string,string>> and change (x,y) to x=>y=> – Embodiment of Ignorance Aug 20 at 2:07 • Seems like a cheap tactic, but meta discussion says it's fair game, so might as well. Thanks. – Andrew Baumher Aug 21 at 0:14 # 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! # 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. # Wolfram Language (Mathematica), 68 bytes StringRiffle@{How,much,#,would,a,c=#<>#2,#2,if,a,c,could,#2,#<>"?"}&  Try it online! # 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.

• @JonathanAllan quite right! Thanks. – Nick Kennedy Aug 19 at 20:31

# Japt-S, 44 39 bytes

[How Û2UÙd aN=¬Vif aNÖdVU+'?]


Try it

# 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


# 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.

# 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!