69
\$\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\$ Commented Aug 19, 2019 at 18:12
  • 5
    \$\begingroup\$ ...I guess "", "" means no :p \$\endgroup\$ Commented 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\$ Commented Aug 20, 2019 at 11:10
  • 7
    \$\begingroup\$ Two. It would golf two code. \$\endgroup\$ Commented 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
    Commented Aug 20, 2019 at 23:48

56 Answers 56

41
\$\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\$
5
  • 3
    \$\begingroup\$ Maybe irrelevant, but the automatic format string is not readily available in python versions prior to 3.6 \$\endgroup\$
    – M.Herzkamp
    Commented Aug 20, 2019 at 8:51
  • 3
    \$\begingroup\$ @M.Herzkamp So? \$\endgroup\$
    – ruohola
    Commented Aug 20, 2019 at 10:09
  • 20
    \$\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
    Commented Aug 20, 2019 at 15:09
  • 10
    \$\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\$ Commented Aug 21, 2019 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\$ Commented Aug 22, 2019 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
  • 3
    \$\begingroup\$ +1000000 virtual trains for megatrain \$\endgroup\$ Commented Aug 19, 2019 at 19:34
  • \$\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
    Commented Aug 22, 2019 at 21:20
18
\$\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\$
8
  • \$\begingroup\$ -3 bytes \$\endgroup\$
    – Grimmy
    Commented Aug 20, 2019 at 12:23
  • 1
    \$\begingroup\$ -2 bytes more \$\endgroup\$ Commented Aug 20, 2019 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\$
    – Grimmy
    Commented Aug 20, 2019 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
    Commented Aug 21, 2019 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
    Commented Aug 25, 2019 at 4:11
17
\$\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 25 bytes with restrictions on input

Æ♂r▓╚£╛☻→Θ─|3ëE3╒╢Ik¶A◙#Θ

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\$
2
  • \$\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
    Commented Aug 20, 2019 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
    Commented Aug 20, 2019 at 17:38
14
\$\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\$
3
  • 1
    \$\begingroup\$ Are you allowed to output a curried function like in that first example? \$\endgroup\$ Commented Aug 22, 2019 at 16:04
  • 1
    \$\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
    Commented Aug 22, 2019 at 16:22
  • 1
    \$\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\$ Commented Aug 23, 2019 at 3:42
7
\$\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\$
8
  • \$\begingroup\$ +2 internet points for one of the most random restrictions I can remember (in your shorter solution) \$\endgroup\$ Commented Aug 19, 2019 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\$ Commented Aug 19, 2019 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\$ Commented Aug 19, 2019 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
    Commented Aug 19, 2019 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
    Commented Aug 20, 2019 at 14:34
5
\$\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\$
2
  • \$\begingroup\$ It's nice to show slightly-cheaty versions too :) \$\endgroup\$ Commented Aug 20, 2019 at 16:37
  • 3
    \$\begingroup\$ Oh, Speccy! To be young again... \$\endgroup\$
    – chx
    Commented Aug 21, 2019 at 4:34
5
\$\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\$
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\$
5
  • 2
    \$\begingroup\$ Can do a double gsub for -13 bytes instead. tio.run/… \$\endgroup\$
    – Sumner18
    Commented Aug 19, 2019 at 20:08
  • \$\begingroup\$ @Sumner18 Thanks. Yeah, gsub definitely makes more sense. \$\endgroup\$
    – Robert S.
    Commented Aug 19, 2019 at 20:15
  • \$\begingroup\$ If you feel like using pryr, you can get it down to 73. \$\endgroup\$ Commented Aug 19, 2019 at 21:14
  • 5
    \$\begingroup\$ You should specify that you assume the input x doesn't include the character 2; it fails in this case (TIO). \$\endgroup\$ Commented Aug 19, 2019 at 21:24
  • 4
    \$\begingroup\$ 76 with aliasing of gsub \$\endgroup\$
    – Giuseppe
    Commented Aug 19, 2019 at 22:19
4
\$\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\$
2
  • \$\begingroup\$ Kotlin sounds like a lot of fun! \$\endgroup\$
    – M.Herzkamp
    Commented Aug 20, 2019 at 9:28
  • 1
    \$\begingroup\$ @M.Herzkamp it sure is! \$\endgroup\$
    – Quinn
    Commented Aug 20, 2019 at 13:11
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\$
2
  • \$\begingroup\$ But one big template string is 1 character shorter: Try it online!. \$\endgroup\$
    – manatwork
    Commented Aug 19, 2019 at 18:27
  • \$\begingroup\$ @manatwork I tried this but included an extra space by mistake :P Thanks! \$\endgroup\$
    – IronFlare
    Commented Aug 19, 2019 at 18:32
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\$
2
  • 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
    Commented Aug 20, 2019 at 22:25
  • \$\begingroup\$ @Mark, Thanks for tip! \$\endgroup\$
    – lee
    Commented Aug 21, 2019 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\$
1
  • \$\begingroup\$ Said this out loud, now I need to clean all the spit off my monitor. \$\endgroup\$
    – BradC
    Commented Aug 22, 2019 at 14:26
4
\$\begingroup\$

05AB1E, 37 35 31 30 bytes

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

Try it online or verify all test cases.

Or alternatively:

…a ÿªðì₁“Howƒ×2€Þ65€¬6ƒˆ52?“r‡

Try it online or verify all test cases.

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

…a ÿ    # Push dictionary string "a ÿ",
        # where the `ÿ` are automatically filled with the (implicit) input-list,
        # implicitly joined together to a single string
        #  i.e. ["wood","chuck"] → "a woodchuck"
    ª   # Append this to the (implicit) input-list: ["wood","chuck","a woodchuck"]
     ðì # Prepend a space before each string: [" wood"," chuck"," a woodchuck"]
₁       # Push builtin 256
“Howƒ×2€Þ65€¬6ƒˆ52?“
        # Push dictionary string "How much2 would65 if6 could52?"
r       # Reverse the values on the stack
 ‡      # Transliterate [2,5,6] to [" wood"," chuck"," a woodchuck"] in the string
        #  → "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?" and “Howƒ×2€Þ65€¬6ƒˆ52?“ is "How much2 would65 if6 could52?".

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 31 \$\endgroup\$
    – Grimmy
    Commented Aug 20, 2019 at 11:51
  • 1
    \$\begingroup\$ 30 \$\endgroup\$
    – Grimmy
    Commented Aug 20, 2019 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\$ Commented Aug 20, 2019 at 12:15
  • 1
    \$\begingroup\$ Alternate 30. I'm not finding any 29, so your explanation is safe for now ;) \$\endgroup\$
    – Grimmy
    Commented Aug 20, 2019 at 12:18
4
\$\begingroup\$

C (clang), 78 72 bytes

#define f(a,b)"How much "a" would a "a b" "b" if a "a b" could "b" "a"?"

Try it online!

  • thanks to @jdt for 6 bytes saved.

Using stringification


C (gcc), 85 bytes

f(a,b){printf("How much %s would a %s%s %s if a %s%s could %s %s?",a,a,b,b,a,b,b,a);}

Try it online!

Thanks to @ErikF suggestion to use gcc, btw I've seen that clang accepts f(*a,*b){ // which is 2 Bytes expensive anyway

Saved 2 thanks to @ceilingcat.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ For the second submission, if you use gcc instead of clang you can remove the parameter types (85 bytes): Try it online! \$\endgroup\$
    – ErikF
    Commented Aug 22, 2019 at 4:31
  • 2
    \$\begingroup\$ You could also use printf positional arguments TIO (but it's still 85 bytes). Oh well. \$\endgroup\$
    – 640KB
    Commented Aug 22, 2019 at 21:00
  • 1
    \$\begingroup\$ 72 bytes \$\endgroup\$
    – jdt
    Commented Sep 8 at 23:49
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\$
3
  • \$\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\$ Commented Aug 25, 2019 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\$ Commented Aug 26, 2019 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\$ Commented Aug 27, 2019 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\$
12
  • \$\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\$ Commented Aug 19, 2019 at 18:29
  • \$\begingroup\$ Wow didn't know about that! Thanks for the info \$\endgroup\$ Commented Aug 19, 2019 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
    Commented Aug 20, 2019 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\$ Commented Aug 20, 2019 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\$ Commented Aug 20, 2019 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\$

Wolfram Language (Mathematica), 68 bytes

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

Try it online!

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

Japt -S, 44 39 bytes

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

Try it

\$\endgroup\$
1
  • \$\begingroup\$ Nice idea to use \$\endgroup\$ Commented Sep 8 at 23:22
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\$
7
  • 3
    \$\begingroup\$ Wouldn't this be invalid? It's a snippet - not a function or a program, right? \$\endgroup\$
    – squid
    Commented Aug 19, 2019 at 17:56
  • \$\begingroup\$ Yeah; needs a lambda n: before it, and can drop the print(). \$\endgroup\$
    – wizzwizz4
    Commented Aug 19, 2019 at 18:13
  • \$\begingroup\$ You should change your code to print How much ... instead of how much .... \$\endgroup\$
    – ruohola
    Commented Aug 20, 2019 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
    Commented Aug 20, 2019 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
    Commented Aug 20, 2019 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\$
2
  • \$\begingroup\$ Change function signature from Func<string,string,string> to Func<string,Func<string,string>> and change (x,y) to x=>y=> \$\endgroup\$
    – Gymhgy
    Commented Aug 20, 2019 at 2:07
  • \$\begingroup\$ Seems like a cheap tactic, but meta discussion says it's fair game, so might as well. Thanks. \$\endgroup\$ Commented Aug 21, 2019 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\$
3
  • 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\$ Commented Aug 21, 2019 at 14:35
  • \$\begingroup\$ Thanks @cairdcoinheringaahing for the comment. I rewrote it in function form, hope it complies with the rules now. \$\endgroup\$ Commented Aug 21, 2019 at 14:47
  • 1
    \$\begingroup\$ Using stringr saves you a few. \$\endgroup\$ Commented Aug 21, 2019 at 15:41
2
\$\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\$
2
\$\begingroup\$

Uiua, 52 50 bytes

$"How much _ would a __ _ if a _ could _ _?"⊃∩.⊃⊂:

Try it: Uiua pad

Explanation: Each _ in the $"..." string is replaced with a value from the stack. ⊃∩.⊃⊂: is used to put the strings on the stack in the proper order:

⊃∩.⊃⊂: is a fork that does three things:

  1. ∩. pushes each string twice, filling the first four slots
  2. joins the two strings, filling the next slot
  3. : flips the two, filling the last two slots.
\$\endgroup\$
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\$
2
  • \$\begingroup\$ Did you count the Greek letters as one byte? \$\endgroup\$ Commented Aug 20, 2019 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é
    Commented Aug 20, 2019 at 13:07
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\$
0

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.