| Bytes | Lang | Time | Link |
|---|---|---|---|
| 074 | Raku Perl 6 rakudo | 241212T194958Z | xrs |
| 064 | AWK | 190821T210738Z | Robert B |
| 8897 | Python 3 | 240920T093903Z | u111 |
| 072 | C clang | 190820T072911Z | AZTECCO |
| 089 | Setanta | 240908T174628Z | bb94 |
| 050 | Uiua | 240905T230628Z | noodle p |
| 029 | Stax | 190819T181935Z | Khuldrae |
| 030 | 05AB1E | 190820T075853Z | Kevin Cr |
| 061 | PHP | 190925T174738Z | Night2 |
| 076 | Haskell | 190830T003909Z | david |
| 102 | Clojure | 190823T163057Z | user8965 |
| 067 | Python 3 | 190819T174149Z | squid |
| 078 | 33 | 190820T083404Z | TheOnlyM |
| 7776 | Applesoft BASIC | 190820T141136Z | lee |
| 082 | TSQL | 190819T182828Z | BradC |
| 095 | SAP ABAP | 190822T080255Z | Maz |
| 070 | JavaScript | 190819T221059Z | Shaggy |
| 046 | V vim | 190821T190040Z | DJMcMayh |
| 056 | Perl 5 | 190821T163626Z | Grimmy |
| 067 | Python 3 | 190821T152729Z | Will Da |
| 050 | Bash | 190819T223815Z | spuck |
| 095 | R | 190821T142322Z | minhsphu |
| 378 | Python 2 | 190821T074347Z | Derte Tr |
| 133 | C# | 190819T181315Z | canttalk |
| 065 | C# Visual C# Interactive Compiler | 190819T183649Z | Andrew B |
| 056 | Julia 1.0 | 190820T200751Z | gggg |
| 061 | Perl 5 pa | 190820T195208Z | Xcali |
| 056 | Batch | 190820T194423Z | Somethin |
| 079 | Emacs Lips | 190820T191251Z | Jordon B |
| 058 | Bash | 190820T171007Z | gadzooks |
| 082 | Lua | 190820T160402Z | val - di |
| 030 | SOGL | 190819T174202Z | dzaima |
| 116 | Forth gforth | 190820T144503Z | reffu |
| 076 | Java JDK | 190820T143716Z | Olivier |
| 076 | R | 190819T191923Z | Robert S |
| 107 | VBA | 190820T133137Z | user3819 |
| 080 | Python 3 | 190819T175450Z | Tryer |
| 040 | MathGolf | 190820T104337Z | maxb |
| 075 | Rust | 190820T101239Z | ruohola |
| 087 | ZX Spectrum Basic | 190820T085731Z | Radovan |
| 074 | Scala | 190820T082201Z | Soapy |
| 061 | Dart | 190820T082123Z | Elcan |
| 079 | Excel | 190820T080934Z | Wernisch |
| 049 | Pyth | 190820T062234Z | ar4093 |
| 077 | Red | 190820T070526Z | Galen Iv |
| 039 | Japt S | 190819T180727Z | Shaggy |
| 071 | Ruby | 190819T193257Z | Value In |
| 037 | Jelly | 190819T183650Z | Nick Ken |
| 068 | Wolfram Language Mathematica | 190819T232119Z | att |
| 039 | Canvas | 190819T195507Z | dzaima |
| 039 | Jelly | 190819T195757Z | Jonathan |
| 072 | JavaScript V8 | 190819T181745Z | IronFlar |
| 065 | PowerShell | 190819T175757Z | Veskah |
| 059 | APL Dyalog Unicode | 190819T175330Z | J. Sall& |
| 072 | PHP | 190819T174643Z | 640KB |
| 059 | Kotlin | 190819T174423Z | Quinn |
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?"
AWK, 64 bytes
$0="How much "$1" would a "$1$2" "$2" if a "$1$2" could "$2" "$1
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"?"
- 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);}
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+"?"}
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:
∩.pushes each string twice, filling the first four slots⊂joins the two strings, filling the next slot: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);
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)
PHP (7.4), 63 bytes
fn($a,$b)=>"How much $a would a $a$b $b if a $a$b could $b $a?"
Haskell, 76 bytes
a?b=a++" "++b
a!b="How much"?a?"would a"?a++b?b?"if a"?a++b?"could"?b?a++"?"
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))
Python 3, 70 67 bytes
"How much {0} would a {0}{1} {1} if a {0}{1} could {1} {0}?".format
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
Takes the input as command-line arguments.
Bonus: 91 bytes
"How much "p1bztp" would a "p1bztp2bztp" "p2bztp" if a "p1bztp2bztp" could "p2bztp" "p1bztp
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:
- Use of
?in place ofPRINTwhen 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
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:
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}?`
Mildly less boring!
a=>"How much 0 would a 01 1 if a 01 could 1 0?".replace(/\d/g,x=>a[x])
V (vim), 46 bytes
iHow much ò would a aò b if a òò could b ò?
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 .?
Python 3, 67 bytes
'How much {0} would a {0}{1} {1} if a {0}{1} could {1} {0}?'.format
Point-free Python
Bash, 50 bytes
echo How much $2 {would,$1\ if}\ a\ $2$1 could $@?
-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='')
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))+'?'
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}?"
same as everyone else, except C#. -1 byte by using currying strat a=>b=>c instead of (a,b)=>c
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})))
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,ŗ
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 ." ?";
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+"?"
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?"]]
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)
when in rome, use str format.
Edited using squid's trick.
MathGolf, 40 bytes
▌_l@lll╩Bßx¬╩5╩♦Γ╫¬╩-╩♦?߬╩à╩e?߬]─ uδ'?
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)
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?"
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\?
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"?"]]
Ruby, 69 71 bytes
->a,b{"How much #{a} would a #{a+b} #{b} if a #{a+b} could #{b} #{a}?"}
Jelly, 41 37 bytes
⁾be,y“Ø[gœıJ9°m.OṚuHlh3Ƥ⁾$ɲ0øḲʂṇHẎṆȥ»
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,#<>"?"}&
Canvas, 42 40 39 bytes
;⁸⁸⁷⁷“UNqO1Uŗ7_WW7eV↷:7⁹`3u5G!oa⁷⁴d:⌐Q‟
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.
...Or (also a full program accepting a list of two strings)
⁽4ṀDBịs€2ṭ€€⁶“þ>Æƈ)taJṖ;ạʂ\4S%dñl»Ỵ¤ż”?
JavaScript (V8), 72 bytes
(a,b)=>['How much',a,'would a',c=a+b,b,'if a',c,'could',b,a+'?'].join` `
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`?"
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 '⍵' '⍺'?'}
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?";
Or:
PHP, 72 bytes
How much <?=![,$a,$b]=$argv,"$a would a $a$b $b if a $a$b could $b $a?";
Input from command line, output to STDOUT.
