63
\$\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\$
15
  • 2
    \$\begingroup\$ May we assume the two words will be two distinct words? \$\endgroup\$ Aug 19, 2019 at 18:12
  • 5
    \$\begingroup\$ ...I guess "", "" means no :p \$\endgroup\$ Aug 19, 2019 at 18:25
  • 1
    \$\begingroup\$ I am mildly disappointed that none of the test cases were "ground", "hog" (since "woodchuck" is another name for a "groundhog"...) \$\endgroup\$ Aug 20, 2019 at 11:10
  • 7
    \$\begingroup\$ Two. It would golf two code. \$\endgroup\$ Aug 20, 2019 at 20:31
  • 2
    \$\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, 2019 at 23:48

52 Answers 52

1
2
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\$
1
\$\begingroup\$

Batch, 56 bytes

@echo How much %1 would a %1%2 %2 if a %1%2 could %2 %1?
\$\endgroup\$
1
\$\begingroup\$

Python 3, 67 bytes

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

Try it online!

Point-free Python

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to the site! I'd recommend you add a link to an online interpreter (like Try it online!), so that others can verify your program! \$\endgroup\$ Aug 21, 2019 at 15:33
  • \$\begingroup\$ Thanks for the suggestion/link @cairdcoinheringaahing. I added the link, and I'll spend some time to familiarize myself with TIO. \$\endgroup\$ Aug 21, 2019 at 15:52
1
\$\begingroup\$

Perl 5, 56 bytes

/ /;$_="How much $' would a $'$` $` if a $'$` could $_?"

Try it online!

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

PHP, 61 bytes

<?=strtr('How much 1 would a 12 2 if a 12 could 2 1?',$argv);

Try it online!

Taking advantage of PHP's strtr array replacement mode. In strtr, when second parameter is an array, it replaces array keys with the corresponding value for that key.

PHP's $argv index/key 0 is always filled by PHP and starting from index 1, command line arguments are put in it. So I pass the two words as command arguments which will be placed at index 1 and 2 in $argv and then the 1s and 2s in the string will be replaced by those values.


PHP (7.4), 62 bytes

fn($a)=>strtr('How much 0 would a 01 1 if a 01 could 1 0?',$a)

Try it online!


PHP (7.4), 63 bytes

fn($a,$b)=>"How much $a would a $a$b $b if a $a$b could $b $a?"

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Canvas, 42 40 39 bytes

;⁸⁸⁷⁷“UNqO1Uŗ7_WW7eV↷:7⁹`3u5G!oa⁷⁴d:⌐Q‟

Try it here!

Canvas's compression unfortunately currently doesn't have an English dictionary mode as I'm lazy.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 69 71 bytes

->a,b{"How much #{a} would a #{a+b} #{b} if a #{a+b} could #{b} #{a}?"}

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I'm afraid you misread the challenge. The 1st modal verb is “would”. \$\endgroup\$
    – manatwork
    Aug 19, 2019 at 20:20
  • 2
    \$\begingroup\$ Noooooo my beautiful byte saves... \$\endgroup\$
    – Value Ink
    Aug 20, 2019 at 0:07
0
\$\begingroup\$

Red, 77 bytes

func[a b][rejoin["How much "a" would a "a b" "b" if a "a b" could "b" "a"?"]]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 49 bytes

AQjd["How much"G"would a"J+GHH"if a"J"could"H+G\?

Try it online!

AQ:  Take the two-element list Q (input) and assign the first element to G, the second to H
jd:   join on d (=" ") the following list
[:    starts list definition, ends implicitly with the end of the program.
      All following values are elements of the list
J+GH: concatenate G and H so it won't be joined with a space between, save it to J
\?:   One-character string consisting of "?"
\$\endgroup\$
0
\$\begingroup\$

Excel, 79 bytes

=SUBSTITUTE(SUBSTITUTE("How much 0 would a 01 1 if a 01 could 1 0?",0,A1),1,A2)

Simple substitution. First string cannot contain 1.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ As it turns out, this is the same length as the more straightforward approach which has no restrictions on input: ="How much "&A1&" would a "&A1&A2&" "&A2&" if a "&A1&A2&" could "&A2&" "&A1&"?" \$\endgroup\$ Aug 20, 2019 at 12:07
  • \$\begingroup\$ Not if you use named ranges A and B. ="How much "&A&" would a "&A&B&" "&B&" if a "&A&B&" could "&B&" "&A&"?" \$\endgroup\$ Aug 20, 2019 at 13:36
  • \$\begingroup\$ @user3819867, would that not be ="How much "&A:A&" would a "&A:A&B:B&" "&B:B&" if a "&A:A&B:B&" could "&B:B&" "&A:A&"?" \$\endgroup\$
    – Wernisch
    Aug 20, 2019 at 14:03
  • \$\begingroup\$ Named range, you can define the name in names manager (Ctrl+F3). You can enter any non-reserved string, including 1 char length. \$\endgroup\$ Aug 21, 2019 at 9:04
  • \$\begingroup\$ Have toyed with named ranges before. Don;t think its in the spirit of CodeGolf. Do we add bytecount for the naming? \$\endgroup\$
    – Wernisch
    Aug 21, 2019 at 9:16
0
\$\begingroup\$

Dart, 61 bytes

f(a,b)=>"How much $a would a $a$b $b if a $a$b could $b $a?";

Try it online!

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

Scala, 74 bytes

(a:String,b:String)=>s"How much $a would a $a$b $b if a $a$b could $b $a?"

Try it online!

\$\endgroup\$
0
\$\begingroup\$

MathGolf, 40 bytes

▌_l@lll╩Bßx¬╩5╩♦Γ╫¬╩-╩♦?߬╩à╩e?߬]─ uδ'?

Try it online!

Probably suboptimal. Relies on manipulating the stack, and generating the sentence starting from the end. I might write an explanation if someone really wants it, nothing magic is happening.

\$\endgroup\$
0
\$\begingroup\$

Java (JDK), 76 bytes

a->b->"How much "+a+" would a "+a+b+" "+b+" if a "+a+b+" could "+b+" "+a+"?"

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Emacs Lips, 79 bytes

(lambda(a b)(concat"How much "a" would a "a b" "b" if a "a b" could "b" "a"?"))
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -pa, 61 bytes

$_="How much 0 would a 01 1 if a 01 could 1 0?";s/\d/$F[$&]/g

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Julia 1.0, 56 bytes

a/b="How much $a would a $a$b $b if a $a$b could $b $a?"

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 2, Python 3 - 78 bytes

lambda a,b:' '.join(('How much',a,'would a',a+b,b,'if a',a+b,'could',b,a))+'?'

Try it online!

no format strings, a tad shorter than @Tryer 's format

\$\endgroup\$
0
\$\begingroup\$

V (vim), 46 bytes

iHow much ò would a aò b if a òò could b ò?

Try it online!

Hexdump:

00000000: 6948 6f77 206d 7563 6820 f220 776f 756c  iHow much . woul
00000010: 6420 6120 1261 f220 1262 2069 6620 6120  d a .a. .b if a 
00000020: f2f2 2063 6f75 6c64 2012 6220 f23f       .. could .b .?
\$\endgroup\$
0
\$\begingroup\$

AWK, 64 bytes

$0="How much "$1" could a "$1$2" "$2" if a "$1$2" could "$2" "$1

Try it online!

I tried being clever with string substitutions and/or print statements, but I couldn't make it shorter.

\$\endgroup\$
0
\$\begingroup\$

SAP ABAP, 95 bytes

FORM f USING a b.WRITE:'How much',a,'would a',a && b,b,'if a',a && b,'could',b,a &&'?'.ENDFORM.

Input should be of type string to preserve spaces/not add any extra ones. Example program:

REPORT z.
PARAMETERS:
    a TYPE string LOWER CASE,
    b TYPE string LOWER CASE.
PERFORM f using a b.

Rather boring code, not much to golf. Removing spaces where they are not needed. String concatenation using a && b is obviously shorter than doing a NO-GAP,b.

While modern ABAP can do string interpolation (WRITE:|{ a } { b }|.), it doesn't save a single byte, because as per usual there are too many mandatory spaces. The boring alternative (WRITE:a,b.) even implicitly appends a space to the output after each ,, making the choice an easy one...

For comparison:
WRITE:|How much { a } would a { a }{ b } { b } etc...|.
WRITE:'How much',a,'would a',a && b,b,'etc...'

\$\endgroup\$
1
  • \$\begingroup\$ I could lose 10 bytes here if it's legal to use the variables defined in the surrounding program (this allows me to remove USING a b). But then my subroutine depends on the correct naming of these two variables in the calling program, which leads me to believe that it's probably not allowed. \$\endgroup\$
    – Maz
    Aug 22, 2019 at 8:24
0
\$\begingroup\$

Clojure, 102 bytes

(#(println"How much"%1"would a"(str %1%2)%2"if a"(str %1%2)"could"%2(str %1\?))(read-line)(read-line))

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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