g | x | w | all
Bytes Lang Time Link
074Raku Perl 6 rakudo241212T194958Zxrs
064AWK190821T210738ZRobert B
8897Python 3240920T093903Zu111
072C clang190820T072911ZAZTECCO
089Setanta240908T174628Zbb94
050Uiua240905T230628Znoodle p
029Stax190819T181935ZKhuldrae
03005AB1E190820T075853ZKevin Cr
061PHP190925T174738ZNight2
076Haskell190830T003909Zdavid
102Clojure190823T163057Zuser8965
067Python 3190819T174149Zsquid
07833190820T083404ZTheOnlyM
7776Applesoft BASIC190820T141136Zlee
082TSQL190819T182828ZBradC
095SAP ABAP190822T080255ZMaz
070JavaScript190819T221059ZShaggy
046V vim190821T190040ZDJMcMayh
056Perl 5190821T163626ZGrimmy
067Python 3190821T152729ZWill Da
050Bash190819T223815Zspuck
095R190821T142322Zminhsphu
378Python 2190821T074347ZDerte Tr
133C#190819T181315Zcanttalk
065C# Visual C# Interactive Compiler190819T183649ZAndrew B
056Julia 1.0190820T200751Zgggg
061Perl 5 pa190820T195208ZXcali
056Batch190820T194423ZSomethin
079Emacs Lips190820T191251ZJordon B
058Bash190820T171007Zgadzooks
082Lua190820T160402Zval - di
030SOGL190819T174202Zdzaima
116Forth gforth190820T144503Zreffu
076Java JDK190820T143716ZOlivier
076R190819T191923ZRobert S
107VBA190820T133137Zuser3819
080Python 3190819T175450ZTryer
040MathGolf190820T104337Zmaxb
075Rust190820T101239Zruohola
087ZX Spectrum Basic190820T085731ZRadovan
074Scala190820T082201ZSoapy
061Dart190820T082123ZElcan
079Excel190820T080934ZWernisch
049Pyth190820T062234Zar4093
077Red190820T070526ZGalen Iv
039Japt S190819T180727ZShaggy
071Ruby190819T193257ZValue In
037Jelly190819T183650ZNick Ken
068Wolfram Language Mathematica190819T232119Zatt
039Canvas190819T195507Zdzaima
039Jelly190819T195757ZJonathan
072JavaScript V8190819T181745ZIronFlar
065PowerShell190819T175757ZVeskah
059APL Dyalog Unicode190819T175330ZJ. Sall&
072PHP190819T174643Z640KB
059Kotlin190819T174423ZQuinn

Raku (Perl 6) (rakudo), 74 bytes

my ($a,$b)=@*ARGS;say "How much $a would a $a$b $b if a $a$b could $b $a?"

Attempt This Online!

AWK, 64 bytes

$0="How much "$1" would 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.

Python 3, 88 bytes and 97 bytes

88 bytes

x=input();y=input();print(f'How much {x} would a {x}{y} {y} if a {x}{y} could {y} {x}?')

97 bytes

x=input();y=input();print('How much %s would a %s%s %s if a %s%s could %s %s?'%(x,x,y,y,x,y,y,x))

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!

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.

Setanta, 89 bytes

gniomh(w,c){toradh"How much "+w+" would a "+w+c+" "+c+" if a "+w+c+" could "+c+" "+w+"?"}

try-setanta.ie link

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.

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.

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

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!

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.

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!

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

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

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:

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

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

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

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!

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

Perl 5, 56 bytes

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

Try it online!

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

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.

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!

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

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.

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

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!

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!

Batch, 56 bytes

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

Emacs Lips, 79 bytes

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

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!

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.

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.

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

Java (JDK), 76 bytes

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

Try it online!

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!

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.

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.

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.

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.

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

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!

Dart, 61 bytes

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

Try it online!

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.

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

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!

Japt -S, 44 39 bytes

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

Try it

Ruby, 69 71 bytes

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

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.

Wolfram Language (Mathematica), 68 bytes

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

Try it online!

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.

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!

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.

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

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.

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.

Kotlin, 59 bytes

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

Try it online!