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

57

14

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

Greg Martin

Posted 2019-08-19T17:28:14.037

Reputation: 13 940

2May we assume the two words will be two distinct words? – Jonathan Allan – 2019-08-19T18:12:13.050

4...I guess "", "" means no :p – Jonathan Allan – 2019-08-19T18:25:55.847

@dzaima Edited to clarify. I don't want the input character set to be the main focus, so I'll trust golfers for the most part. – Greg Martin – 2019-08-19T19:30:07.727

IMHO I think a) the phrase doesn't make sense with "could", since maybe it wouldn't anyway b) you really need the homophone. – Neil – 2019-08-19T19:49:02.200

How reasonable is "reasonable"? – Khuldraeseth na'Barya – 2019-08-19T21:17:09.027

Is input allowed to be taken in reverse order: e.g. 'chuck' 'wood'? – maxb – 2019-08-20T09:27:51.947

I am mildly disappointed that none of the test cases were "ground", "hog" (since "woodchuck" is another name for a "groundhog"...) – Chronocidal – 2019-08-20T11:10:54.237

1@Chronocidal I can't stand to see you disappointed.... – Greg Martin – 2019-08-20T16:33:37.807

@maxb I guess I don't see any reason to disallow the "wrong" order for inputs ... a tiny bit confusing maybe, but just saying so in the answer seems like enough to me. – Greg Martin – 2019-08-20T16:35:01.150

6Two. It would golf two code. – user2357112 supports Monica – 2019-08-20T20:31:19.770

2Aw, I was hoping this would be about a short program that can do some simple golfing transformation(s) on other code. – aschepler – 2019-08-20T23:48:52.943

1I am going to have the "jabber", "wock" one stuck in my head, now. – David Conrad – 2019-08-21T16:00:01.680

It doesn't quite fit the pattern, but I'm fond of "How many dice would a dyed die dye if a dyed die could dye dice?" – Nathan – 2019-08-22T03:54:31.283

1Shouldn't the invariant space between "chuck" and "wood" in the example's third form also be code-quoted? – Leif Willerts – 2019-08-22T19:20:01.730

"How much would a would a would ahow much how much if a would ahow much could how much would a?" - ROFL – Thomas Weller – 2019-08-22T22:47:18.007

Answers

41

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

Reinstate Monica

Posted 2019-08-19T17:28:14.037

Reputation: 1 382

2Maybe irrelevant, but the automatic format string is not readily available in python versions prior to 3.6 – M.Herzkamp – 2019-08-20T08:51:03.957

3@M.Herzkamp So? – ruohola – 2019-08-20T10:09:10.297

17@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 – 2019-08-20T15:09:56.973

8Even 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 – 2019-08-21T15:17:32.090

1I'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 – 2019-08-22T21:46:26.637

25

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

BradC

Posted 2019-08-19T17:28:14.037

Reputation: 6 099

2+1000000 virtual trains for megatrain – Greg Martin – 2019-08-19T19:34:14.673

5I assume that "w" and "c" stand for "wood" and "chuck" respectively :P – user253751 – 2019-08-20T23:03:03.680

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 – 2019-08-22T21:20:33.083

16

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.

spuck

Posted 2019-08-19T17:28:14.037

Reputation: 649

-3 bytes – Grimmy – 2019-08-20T12:23:34.650

1-2 bytes more – Nahuel Fouilleul – 2019-08-20T13:57:16.407

@NahuelFouilleul: Your edits are clever, but they require reversing the order of the input parameters from the original challenge. – spuck – 2019-08-20T17:03:22.533

@Grimy: A good use of $@, but it requires reversing the order of the input. – spuck – 2019-08-20T17:20:40.013

1

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 – 2019-08-20T22:30:11.910

2@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 – 2019-08-21T14:52:20.600

Could you please elaborate how this works? The brace expansion and backslashes are doing my head in! – roblogic – 2019-08-24T13:21:49.267

1@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 – 2019-08-25T04:11:36.107

15

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.

Khuldraeseth na'Barya

Posted 2019-08-19T17:28:14.037

Reputation: 2 608

"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 – 2019-08-20T17:25:04.213

1Additionally, 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 – 2019-08-20T17:38:07.023

14

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!

Shaggy

Posted 2019-08-19T17:28:14.037

Reputation: 24 623

Are you allowed to output a curried function like in that first example? – Feathercrown – 2019-08-22T16:04:42.707

@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 – 2019-08-22T16:22:07.690

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 – 2019-08-23T03:42:46.227

6

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.

dzaima

Posted 2019-08-19T17:28:14.037

Reputation: 19 048

+2 internet points for one of the most random restrictions I can remember (in your shorter solution) – Greg Martin – 2019-08-19T19:31:13.823

@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 – 2019-08-19T19:38:22.690

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 – 2019-08-19T20:36:35.613

@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 – 2019-08-19T20:38:37.290

Is it not possible for replacement target to be non-alpha to avoid having to ban an alpha from the input? – WGroleau – 2019-08-20T14:34:14.977

@WGroleau I do have 36 bytes with the forbidden character being ")", but as can be seen, that's 4 bytes longer because b and be are in the top 512 words, which compress better

– dzaima – 2019-08-20T14:43:27.880

@WGroleau writing that, I finally understood why it was so better - I had thought that SOGL compressed the top 256 words, but the top 512 also has the letter "y", so that's another -2 – dzaima – 2019-08-20T14:48:26.627

In English, the letters ‘e’ and ‘t’ are the two worst to forbid. ‘y’ is way down the list. – WGroleau – 2019-08-20T14:54:09.223

5

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!

Robert S.

Posted 2019-08-19T17:28:14.037

Reputation: 1 253

@Sumner18 Thanks. Yeah, gsub definitely makes more sense. – Robert S. – 2019-08-19T20:15:00.900

If you feel like using pryr, you can get it down to 73.

– Khuldraeseth na'Barya – 2019-08-19T21:14:50.040

5

You should specify that you assume the input x doesn't include the character 2; it fails in this case (TIO).

– Robin Ryder – 2019-08-19T21:24:36.990

476 with aliasing of gsub – Giuseppe – 2019-08-19T22:19:08.690

5

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

Radovan Garabík

Posted 2019-08-19T17:28:14.037

Reputation: 437

It's nice to show slightly-cheaty versions too :) – Greg Martin – 2019-08-20T16:37:14.187

3Oh, Speccy! To be young again... – chx – 2019-08-21T04:34:55.203

4

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.

640KB

Posted 2019-08-19T17:28:14.037

Reputation: 7 149

4

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.

IronFlare

Posted 2019-08-19T17:28:14.037

Reputation: 141

But one big template string is 1 character shorter: Try it online!.

– manatwork – 2019-08-19T18:27:41.610

@manatwork I tried this but included an extra space by mistake :P Thanks! – IronFlare – 2019-08-19T18:32:51.963

4

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?".

Kevin Cruijssen

Posted 2019-08-19T17:28:14.037

Reputation: 67 575

131 – Grimmy – 2019-08-20T11:51:38.100

130 – Grimmy – 2019-08-20T12:04:22.043

1@Grimy Very nice! I really like both of those. Ingenious idea of using the like that to save the additional byte, thanks. – Kevin Cruijssen – 2019-08-20T12:15:42.910

1Alternate 30. I'm not finding any 29, so your explanation is safe for now ;) – Grimmy – 2019-08-20T12:18:15.310

4

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

TheOnlyMrCat

Posted 2019-08-19T17:28:14.037

Reputation: 1 079

Said this out loud, now I need to clean all the spit off my monitor. – BradC – 2019-08-22T14:26:26.517

4

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.

ruohola

Posted 2019-08-19T17:28:14.037

Reputation: 414

4

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

lee

Posted 2019-08-19T17:28:14.037

Reputation: 91

1Can 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 – 2019-08-20T22:25:26.223

@Mark, Thanks for tip! – lee – 2019-08-21T02:39:54.363

3

Kotlin, 59 bytes

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

Try it online!

Quinn

Posted 2019-08-19T17:28:14.037

Reputation: 1 153

Kotlin sounds like a lot of fun! – M.Herzkamp – 2019-08-20T09:28:09.183

1@M.Herzkamp it sure is! – Quinn – 2019-08-20T13:11:25.200

3

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

Veskah

Posted 2019-08-19T17:28:14.037

Reputation: 3 580

3

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.

canttalkjustcode

Posted 2019-08-19T17:28:14.037

Reputation: 131

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 – 2019-08-19T18:29:27.310

Wow didn't know about that! Thanks for the info – canttalkjustcode – 2019-08-19T19:09:23.030

2@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 – 2019-08-20T08:17:50.550

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 – 2019-08-20T11:34:00.167

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 – 2019-08-20T11:34:22.887

And to as why I am doing classes instead of functions? Just because most people using C# on this site do it too. Its just to keep fair competition – canttalkjustcode – 2019-08-20T11:40:34.643

@JoKing It takes it from standard input. – Kenneth K. – 2019-08-20T14:13:20.697

int M instead of void M saves you a byte. And you don't need the public--neither class nor function. Also, Console.Write with a trailing \n saves a couple of bytes. – Kenneth K. – 2019-08-20T14:15:07.413

But int M requires me to have a return value right? – canttalkjustcode – 2019-08-20T14:17:39.880

Ooh, sorry, you're correct. – Kenneth K. – 2019-08-20T14:18:05.057

Learned about interpolated strings from other code golfers myself, actually. Just doing my part, i guess, lol. Though if you're using a[0] and a[1], it might actually be shorter to use {0} and {1}. You'll save 24 bytes in the text section, then lose 10 bytes defining those values, for a net gain of 14. It works better with single character variables. – Andrew Baumher – 2019-08-21T00:41:15.763

It's crazy. Have learned so much on this site in 3 days. Using {0} and {1} is indeed shorter. Why did the byte count decrease in the first place then? – canttalkjustcode – 2019-08-21T01:17:43.357

3

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.

user3819867

Posted 2019-08-19T17:28:14.037

Reputation: 439

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 – 2019-08-25T18:19:30.093

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 – 2019-08-26T07:58:39.973

1That 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 – 2019-08-27T19:52:42.410

3

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.

david

Posted 2019-08-19T17:28:14.037

Reputation: 180

2

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.

Tryer

Posted 2019-08-19T17:28:14.037

Reputation: 71

3Wouldn't this be invalid? It's a snippet - not a function or a program, right? – Reinstate Monica – 2019-08-19T17:56:40.530

Yeah; needs a lambda n: before it, and can drop the print(). – wizzwizz4 – 2019-08-19T18:13:57.797

You should change your code to print How much ... instead of how much .... – ruohola – 2019-08-20T12:03:02.160

3It 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 – 2019-08-20T18:03:45.157

@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 – 2019-08-20T19:49:24.570

@Tryer Try it online!, see also my tip about object methods

– xnor – 2019-08-20T22:51:52.057

I didn't even know you could define a function in that way to have it take arguments without using lambda. Thank you! – Tryer – 2019-08-21T12:24:41.993

2

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

Andrew Baumher

Posted 2019-08-19T17:28:14.037

Reputation: 351

Change function signature from Func<string,string,string> to Func<string,Func<string,string>> and change (x,y) to x=>y=> – Embodiment of Ignorance – 2019-08-20T02:07:54.830

Seems like a cheap tactic, but meta discussion says it's fair game, so might as well. Thanks. – Andrew Baumher – 2019-08-21T00:14:32.157

2

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!

Jonathan Allan

Posted 2019-08-19T17:28:14.037

Reputation: 67 804

2

Wolfram Language (Mathematica), 68 bytes

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

Try it online!

attinat

Posted 2019-08-19T17:28:14.037

Reputation: 3 495

2

# C (clang), 78 bytes

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

Try it online!

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.

AZTECCO

Posted 2019-08-19T17:28:14.037

Reputation: 2 441

1

For the second submission, if you use gcc instead of clang you can remove the parameter types (85 bytes): Try it online!

– ErikF – 2019-08-22T04:31:27.780

2

You could also use printf positional arguments TIO (but it's still 85 bytes). Oh well.

– 640KB – 2019-08-22T21:00:49.983

2

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!

minhsphuc12

Posted 2019-08-19T17:28:14.037

Reputation: 21

2

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

– caird coinheringaahing – 2019-08-21T14:35:12.700

Thanks @cairdcoinheringaahing for the comment. I rewrote it in function form, hope it complies with the rules now. – minhsphuc12 – 2019-08-21T14:47:12.650

Using stringr saves you a few. – Khuldraeseth na'Barya – 2019-08-21T15:41:20.380

1

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.

J. Sallé

Posted 2019-08-19T17:28:14.037

Reputation: 3 233

Did you count the Greek letters as one byte? – Tomáš Zato - Reinstate Monica – 2019-08-20T13:04:30.337

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

– J. Sallé – 2019-08-20T13:07:17.923

1

Japt -S, 44 39 bytes

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

Try it

Shaggy

Posted 2019-08-19T17:28:14.037

Reputation: 24 623

1

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.

Nick Kennedy

Posted 2019-08-19T17:28:14.037

Reputation: 11 829

@JonathanAllan quite right! Thanks. – Nick Kennedy – 2019-08-19T20:31:11.617

1

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

reffu

Posted 2019-08-19T17:28:14.037

Reputation: 1 361

1

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.

val says Reinstate Monica

Posted 2019-08-19T17:28:14.037

Reputation: 409

1

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!

gadzooks02

Posted 2019-08-19T17:28:14.037

Reputation: 527

1

Batch, 56 bytes

@echo How much %1 would a %1%2 %2 if a %1%2 could %2 %1?

SomethingDark

Posted 2019-08-19T17:28:14.037

Reputation: 211

1

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

Will Da Silva

Posted 2019-08-19T17:28:14.037

Reputation: 111

1

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!

– caird coinheringaahing – 2019-08-21T15:33:43.607

Thanks for the suggestion/link @cairdcoinheringaahing. I added the link, and I'll spend some time to familiarize myself with TIO. – Will Da Silva – 2019-08-21T15:52:30.437

1

Perl 5, 56 bytes

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

Try it online!

Grimmy

Posted 2019-08-19T17:28:14.037

Reputation: 12 521

1

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!

Night2

Posted 2019-08-19T17:28:14.037

Reputation: 5 484

0

Ruby, 69 71 bytes

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

Try it online!

Value Ink

Posted 2019-08-19T17:28:14.037

Reputation: 10 608

1I'm afraid you misread the challenge. The 1st modal verb is “would”. – manatwork – 2019-08-19T20:20:46.667

2Noooooo my beautiful byte saves... – Value Ink – 2019-08-20T00:07:59.670

0

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.

dzaima

Posted 2019-08-19T17:28:14.037

Reputation: 19 048

0

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

ar4093

Posted 2019-08-19T17:28:14.037

Reputation: 531

0

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!

Galen Ivanov

Posted 2019-08-19T17:28:14.037

Reputation: 13 815

0

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.

Wernisch

Posted 2019-08-19T17:28:14.037

Reputation: 2 534

1As 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&"?" – Engineer Toast – 2019-08-20T12:07:12.707

Not if you use named ranges A and B. ="How much "&A&" would a "&A&B&" "&B&" if a "&A&B&" could "&B&" "&A&"?" – user3819867 – 2019-08-20T13:36:13.263

@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&"?" – Wernisch – 2019-08-20T14:03:50.360

Named range, you can define the name in names manager (Ctrl+F3). You can enter any non-reserved string, including 1 char length. – user3819867 – 2019-08-21T09:04:36.593

Have toyed with named ranges before. Don;t think its in the spirit of CodeGolf. Do we add bytecount for the naming? – Wernisch – 2019-08-21T09:16:16.727

0

Dart, 61 bytes

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

Try it online!

Elcan

Posted 2019-08-19T17:28:14.037

Reputation: 913

You can move the function name to the header and omit it from the byte-count. – Oliver – 2019-08-21T12:50:59.723

@Oliver I'm curious where this has been specified, I haven't read anything regarding that anywhere and always assumed function names had to be included – Elcan – 2019-08-21T13:54:21.147

0

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!

Soapy

Posted 2019-08-19T17:28:14.037

Reputation: 251

0

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.

maxb

Posted 2019-08-19T17:28:14.037

Reputation: 5 754

0

Java (JDK), 76 bytes

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

Try it online!

Olivier Grégoire

Posted 2019-08-19T17:28:14.037

Reputation: 10 647

0

Emacs Lips, 79 bytes

(lambda(a b)(concat"How much "a" would a "a b" "b" if a "a b" could "b" "a"?"))

Jordon Biondo

Posted 2019-08-19T17:28:14.037

Reputation: 1 030

0

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!

Xcali

Posted 2019-08-19T17:28:14.037

Reputation: 7 671

0

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!

gggg

Posted 2019-08-19T17:28:14.037

Reputation: 1 715

0

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

Derte Trdelnik

Posted 2019-08-19T17:28:14.037

Reputation: 101

0

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

James

Posted 2019-08-19T17:28:14.037

Reputation: 54 537

0

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.

Robert Benson

Posted 2019-08-19T17:28:14.037

Reputation: 1 339

0

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

Maz

Posted 2019-08-19T17:28:14.037

Reputation: 191

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. – Maz – 2019-08-22T08:24:37.113

0

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!

user89655

Posted 2019-08-19T17:28:14.037

Reputation: 31