With the given text below, there are some words in the text that repeats several times in the text. Use any programming language to write a short code that compresses the text to display it. Or in other words, use the smallest number of bytes to display the text.
The Text is:
Peter Piper picked a peck of pickled peppers.
A peck of pickled peppers Peter Piper picked.
If Peter Piper picked a peck of pickled peppers,
Where's the peck of pickled peppers Peter Piper picked?
-
7\$\begingroup\$ I'm honestly surprised this didn't get closed as a dupe of that Rick-Roll question. Are we not doing that anymore? \$\endgroup\$Jo King– Jo King2018年10月15日 12:00:37 +00:00Commented Oct 15, 2018 at 12:00
-
1\$\begingroup\$ @JoKing it is a different string. A little variety on the same challenge can be fun sometimes. \$\endgroup\$moonheart08– moonheart082018年10月15日 17:23:32 +00:00Commented Oct 15, 2018 at 17:23
-
1\$\begingroup\$ @moonheart08 pretty sure that point was shot down in meta. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年10月16日 18:34:23 +00:00Commented Oct 16, 2018 at 18:34
35 Answers 35
R, 106 bytes
"["=gsub
cat(1["Peter Piper picked",2[" peck of pickled peppers","1 a2.
A2 1.
If 1 a2,
Where's the2 1?"]])
-
1\$\begingroup\$ That is a very clever use of aliasing!! \$\endgroup\$Giuseppe– Giuseppe2018年10月13日 22:45:46 +00:00Commented Oct 13, 2018 at 22:45
-
1\$\begingroup\$ Great solution ! It beats also the memCompress approach 47+79=126 bytes \$\endgroup\$digEmAll– digEmAll2018年10月14日 08:57:26 +00:00Commented Oct 14, 2018 at 8:57
-
1
-
\$\begingroup\$ Wow, I didn't spot that golf. That's really nice. \$\endgroup\$J.Doe– J.Doe2018年10月15日 14:15:17 +00:00Commented Oct 15, 2018 at 14:15
Jelly, (削除) 80 73 72 68 67 61 (削除ここまで) 57 bytes
"¡l·Ṫ]ṃ{yṁ"Ñ3$ṘW5Ḍż8¢Hl·"3ḌM"¡FỊİg"ÑɲʋØƥþƈƘ}"ṣɠ»"Ƙ9~ḷ’ṃFḊ"?
How?
"..."..."..."..."..."...»"Ƙ9~ḷ’ṃFḊ"? - Main Link: no arguments
"..."..."..."..."..."...» - list of compressed strings
- = [" Peter Piper picked",
- " peck of pickled peppers",
- ".\nA",
- ".\nIf",
- ",\nWhere's the",
- " a"]
"Ƙ9~ḷ’ - base 250 literal X = 2331781969
ṃ - base decompress - i.e. use the list of strings as if
- they were the digits [1,2,3,4,5,0]
- X in base 6 is [1,0,2,3,2,1,4,1,0,2,5,2,1], so:
- [" Peter Piper picked",
- " a",
- " peck of pickled peppers",
- ".\nA"," peck of pickled peppers",
- " Peter Piper picked",
- ".\nIf",
- " Peter Piper picked",
- " a",
- " peck of pickled peppers",
- ",\nWhere's the",
- " peck of pickled peppers",
- " Peter Piper picked"]
F - flatten
Ḋ - dequeue (remove the leading space)
"? - literal '?' character (causes print of previous)
- implicit print (of the '?' character)
Bubblegum, (削除) 73 (削除ここまで) 71 bytes
00000000: 0b48 2d49 2d52 08c8 2c00 9205 99c9 d9a9 .H-I-R..,.......
00000010: 290a 890a 05a9 c9d9 0af9 6960 819c d414 ).........i`....
00000020: 20bf 0028 5fac c7e5 884b 4a01 d31c 3d2e ..(_....KJ...=.
00000030: cf34 0552 8cd7 e10a cf48 2d4a 552f 5628 .4.R.....H-JU/V(
00000040: c948 25c1 227b 00 .H%."{.
-
2\$\begingroup\$ How did you shave off bytes of a bubblegum answer? \$\endgroup\$Laikoni– Laikoni2018年10月13日 21:15:07 +00:00Commented Oct 13, 2018 at 21:15
-
2\$\begingroup\$ @Laikoni the original 73 bytes answer was created using
gzipon the highest compression level (-9) plus some metadata shaving usingheadandtail, the 71 byter is generated using zopfli, which I initially forgot about. Zopfli usually creates shorter DEFLATE streams. \$\endgroup\$ovs– ovs2018年10月13日 21:24:48 +00:00Commented Oct 13, 2018 at 21:24 -
\$\begingroup\$ Yeah, I've tried up to 5,000,000 iterations on zopfli, it couldn't find anything past the 71-byte one on iteration 3109. \$\endgroup\$LegionMammal978– LegionMammal9782018年10月13日 22:30:40 +00:00Commented Oct 13, 2018 at 22:30
JavaScript (SpiderMonkey), 114 bytes
print(`0 a1.
A1 0.
If 0 a1,
Where's the1 0?`.replace(/\d/g,n=>+n?' peck of pickled peppers':'Peter Piper picked'))
I would claim this answer is from ovs, anyway, 19 bytes saved.
Thanks Arnauld, saves 3 bytes.
-
1
Python 2, 115 bytes
a="Peter Piper picked"
b=" peck of pickled peppers"
print a,"a%s.\nA"%b+b,a+".\nIf",a,"a%s,\nWhere's the"%b+b,a+"?"
Prints multiple commas-separated strings to put spaces in between them.
Python 3, 115 bytes
print("1 a2.\nA2 1.\nIf 1 a2,\nWhere's the2 1?".translate({49:"Peter Piper picked",50:" peck of pickled peppers"}))
Python 3's translate does the heavy lifting. Using non-printable characters with single-digit ASCII value should save two bytes.
-
1\$\begingroup\$
exitsaves 1 byte for the Python 3 program. \$\endgroup\$Jonathan Allan– Jonathan Allan2018年10月14日 20:14:44 +00:00Commented Oct 14, 2018 at 20:14
Jelly, (削除) 64 (削除ここまで) (削除) 60 (削除ここまで) (削除) 58 (削除ここまで) 57 bytes
"¡l·Ṫ]ṃ{yṁ"Ñ3$ṘW5Ḍż8¢Hl·»j) a,Ṛẋ2ż"3ḌM"¡FỊİg"ÑɲʋØƥþƈƘ}»FḊ"?
-
\$\begingroup\$ Wow, surprisingly similar to another answer, with the same language and same byte count. I don't actually know what's happening in this language, so is the code basicically the same? \$\endgroup\$tox123– tox1232018年10月14日 22:14:15 +00:00Commented Oct 14, 2018 at 22:14
-
1\$\begingroup\$ A lot of the overlap in the code is the identical compressed strings, which is not surprising. \$\endgroup\$Misha Lavrov– Misha Lavrov2018年10月14日 23:34:58 +00:00Commented Oct 14, 2018 at 23:34
-
1\$\begingroup\$ @tox the two programs are currently not working in the same way (although both of us have used similar ideas as each other in the history of revisions). This one is using compressed string lists (
"..."...») to form most of the four lines and then interleaving (ż) with the less repetitive parts (like',\nIf'), again with compressed string lists; you can see how mine works from the description. \$\endgroup\$Jonathan Allan– Jonathan Allan2018年10月15日 12:31:22 +00:00Commented Oct 15, 2018 at 12:31
Bash, 99
- 4 bytes saved thanks to @manatwork.
echo "${P=Peter Piper picked} a${p= peck of pickled peppers}.
A$p $P.
If $P a$p,
Where's the$p $P?"
-
3\$\begingroup\$ You could move the variable declarations to their first usages with assign default value parameter expansions: Try it online!. \$\endgroup\$manatwork– manatwork2018年10月13日 21:26:50 +00:00Commented Oct 13, 2018 at 21:26
-
1\$\begingroup\$ @manatwork Wow, I had no idea you could do that. Pretty cool to get under 100 - Thanks! This technique would make a good bash tips answer. \$\endgroup\$Digital Trauma– Digital Trauma2018年10月14日 02:08:17 +00:00Commented Oct 14, 2018 at 2:08
V, (削除) 99 (削除ここまで) 87 bytes
-12 bytes: turns out 2 substitutions are shorter which is basically the same as everyone else's solution (except Bubblegum?)
i1 a0.
A0 1.
If 1 a0,
Where's the0 1?Í0/ peck of pickled peppers
Í1/Peter Piper picked
Python 3, (削除) 120 (削除ここまで) (削除) 117 (削除ここまで) 116 bytes
a,b="Peter Piper picked"," peck of pickled peppers"
exit(f"{a} a{b}.\nA{b} {a}.\nIf {a} a{b},\nWhere's the{b} {a}?")
Format strings were shorter than addition(129 bytes) and a join(140 bytes).
-3 thanks to Jo King, -1 thanks to Jonathan Allen
-
1
-
1\$\begingroup\$ Programs may output to STDERR, so save 1 by replacing
printwithexit. \$\endgroup\$Jonathan Allan– Jonathan Allan2018年10月14日 20:12:50 +00:00Commented Oct 14, 2018 at 20:12
Java (JDK), 123 bytes
v->"".format("%s a%s.%nA%2$s %1$s.%nIf %1$s a%2$s,%nWhere's the%2$s %1$s?","Peter Piper picked"," peck of pickled peppers")
-
\$\begingroup\$ Are you allowed to take a required argument like that? \$\endgroup\$Quintec– Quintec2018年10月15日 11:50:18 +00:00Commented Oct 15, 2018 at 11:50
-
2
Twig, 105 bytes
This uses a simple replacement to fill in the gaps.
Twig's replace() filter allows you to define the values to replace as the keys of an hash.
Luckly, it also works with arrays, as they have numerical keys.
{{"0a1.
A1 0.
If 0 a1,
Where's the1 0?"|replace(["Peter Piper picked"," peck of pickled peppers"])|raw}}
The |raw is needed to avoid escaping, which turned Where's into Where's.
You can try it on https://twigfiddle.com/phqpts
Since this is compiled down to PHP, the equivalent for PHP would be:
<?php
$array = array("Peter Piper picked", " peck of pickled peppers");
$string = "0 a1.
A1 0.
If 0 a1,
Where's the1 0?";
echo str_replace(array_keys($array), $array, $string);
Which can be shortened significatively.
C (gcc), 123 bytes
f(){printf("%s a%s.\nA%2$s %1$s.\nIf %1$s a%2$s,\nWhere's the%2$s %1$s?","Peter Piper picked"," peck of pickled peppers");}
Clean, 166 bytes
import StdEnv,Text;f="peck of pickled";g="picked";u="peppers";p="Peter Piper";s=join" "[p,g,"a",f,u+".\nA",f,u,p,g+".\nIf",p,g,"a",f,u+",\nWhere's","the",f,u,p,g+"?"]
sed, (削除) 101 (削除ここまで) 100 bytes
s/^/0 a1.\nA1 0.\nIf 0 a1,\nWhere's the1 0?/
s/0/Peter Piper picked/g
s/1/ peck of pickled peppers/g
-1 byte thanks to @DigitalTrauma
-
\$\begingroup\$ Replace
.*with^to save a byte \$\endgroup\$Digital Trauma– Digital Trauma2018年10月14日 02:10:00 +00:00Commented Oct 14, 2018 at 2:10
jq, 110 characters
(106 characters code + 4 characters command line options)
"1 a2.
A2 1.
If 1 a2,
Where's the2 1?"|gsub("1";"Peter Piper picked")|gsub("2";" peck of pickled peppers")
Sample run:
bash-4.4$ jq -nr '"1 a2.
A2 1.
If 1 a2,
Where'"'"'s the2 1?"|gsub("1";"Peter Piper picked")|gsub("2";" peck of pickled peppers")'
Peter Piper picked a peck of pickled peppers.
A peck of pickled peppers Peter Piper picked.
If Peter Piper picked a peck of pickled peppers,
Where's the peck of pickled peppers Peter Piper picked?
SQL Server, 211
declare @a char(18)='Peter Piper picked'
declare @b char(24)=' peck of pickled peppers'
declare @c char=char(10)
print @a+' a'+@b+'.'+@c+'A'+@b+' '+@a+'.'+@c+'If '+@a+' a'+@b+','+@c+'Where''s the'+@b+' '+@a+'?'
-
\$\begingroup\$ Nice solution! A few ways to improve: for multiple variables use a comma instead of restating
declare; use an actual line break in the string instead ofchar(10), in fact you can put the line breaks directly in theprintstatement and eliminate@centirely. Pick your most-used variable and use@by itself (its valid!) \$\endgroup\$BradC– BradC2018年10月15日 17:21:26 +00:00Commented Oct 15, 2018 at 17:21
-
1\$\begingroup\$ Here's one byte shorter unpacked, and this one seems to be making a profound statement about existence. \$\endgroup\$Khuldraeseth na'Barya– Khuldraeseth na'Barya2019年08月23日 15:53:27 +00:00Commented Aug 23, 2019 at 15:53
-
\$\begingroup\$
"i am. Am i. If i am, Where's them i?"I can't stop laughing. This is gold. \$\endgroup\$recursive– recursive2019年08月23日 17:50:58 +00:00Commented Aug 23, 2019 at 17:50 -
\$\begingroup\$ Descartes ain't not nothin' on me. \$\endgroup\$Khuldraeseth na'Barya– Khuldraeseth na'Barya2019年08月23日 18:05:32 +00:00Commented Aug 23, 2019 at 18:05
Windows Batch, 179 bytes
4D534346000000009C00000000000000
2C000000000000000301010001000000
000000004200000001000100DF000000
000000000000544DB5682000612E6261
7400E59D45D15200DF00434B73484DCE
C85708482D492D5208C82C00920599C9
D9A9290A890A05A9C9D90AF96960811C
A048416A0150BE588F970BA2C911970A
2CC6C13579A69164990E4C5F78466A51
AA7AB14249462A09D6DA0300
extract %0 .bat
.bat
Self-extracting Cabinet file using Batch/CAB polyglot. The blank line is needed for the batch file processor to find the actual batch code, but can be LF alone instead of CR/LF. The Cabinet file is just a series of "@echo <string>" lines, and the '@' symbol suppresses the "echo" itself from being displayed.
T-SQL, 137 bytes
SELECT p+a+k+'.
A'+k+' '+p+'.
If '+p+a+k+',
Where''s the'+k+' '+p+'?'
FROM(SELECT'Peter Piper picked'p,' a'a,' peck of pickled peppers'k)b
That last return before the FROM is for readability only, the rest are part of the string concatenation.
Different method than SeanC's SQL solution.
Kotlin, 150 bytes
var s="Peter Piper picked"
var z=" peck of pickled peppers"
var v=s+" a"+z
var x=z+" "+s
print(v+".\n"+"A"+x+".\n"+"If "+v+",\n"+"Where's the "+x+"?")
Retina 0.8.2, 85 bytes
1 a0.¶A0 1.¶If 1 a0,¶Where's the0 1?
1
Peter Piper picked
0
peck of pickled peppers
Try it online! Same idea as everyone else.
Red, 116 bytes
prin rejoin[a:"Peter Piper picked"" a"b:" peck of pickled peppers"".^/A"b" "a".^/If "a" a"b",^/Where's the"b" "a"?"]
Explanation:
The job is done by the rejoin funcion, which reduces and joins a block of values.
prin rejoin [ ; print the reduced (evaluated) and joined block
a: "Peter Piper picked" ; save the text to a
" a" ; literal " a"
b: " peck of pickled peppers" ; save the text to b
".^/A" ; literal newline followed by "A"
b ; " peck of pickled peppers"
" " ; literal " "
a ; "Peter Piper picked"
".^/If " ; literal ".^/If "
a ; "Peter Piper picked"
" a" ; literal " a"
b ; " peck of pickled peppers"
",^/Where's the" ; literal "," folowwed by a newline by "Where's the"
b ; " peck of pickled peppers"
" " ; literal " "
a ; "Peter Piper picked"
"?" ; literal "?"
]
J, 121 bytes
echo('1 a2.',CR,'A2 1.',CR,'If 1 a2,',CR,'Where''s the2 1?')rplc('1';'Peter Piper picked';'2';' peck of pickled peppers')
PHP, 107 bytes
<?=($a="Peter Piper picked")." a".($b=" peck of pickled peppers").".
A$b $a.
If $a a$b,
Where's the$b $a?";
-
\$\begingroup\$ Missing a point in the first line. \$\endgroup\$G B– G B2018年10月15日 10:11:34 +00:00Commented Oct 15, 2018 at 10:11
-
\$\begingroup\$ Use comma instead of concatenation to save 4 bytes:
<?=$a=...," a",$b=...,...\$\endgroup\$Titus– Titus2018年10月15日 14:07:29 +00:00Commented Oct 15, 2018 at 14:07
05AB1E, (削除) 78 (削除ここまで) (削除) 76 (削除ここまで) (削除) 74 (削除ここまで) 72 bytes
’0 a1.
A10.
If0 a1,
W€Î's €10ドル?’TS.•1~1⁄4 ¿•"±æ€‚ ÿÇì"'p0ǝ„íÎ ́ŒTM„r3⁄4Ы‚ðì:¦
Explanation:
’0 a1.
A10.
If0 a1,
W€Î's €10ドル?’ # String "0 a1.\nA10.\nIf0 a1,\nWhere's the10?"
TS # 10 to digits: ["1","0"]
.•1~1⁄4 ¿• # String "pickled"
"±æ€‚ ÿÇì" # String "neck of ÿ pepper", where the "ÿ" will
# automatically be replaced with the top value of the stack
'p0ǝ # Replace the character at index 0 with a "p":
# "peck of pickled pepper"
„íÎ ́Œ # String "peter pipe"
TM # Titlecased: "Peter Pipe"
„r3⁄4Ð # String "r picked"
« # Merge them together: "Peter Piper pickled"
‚ # Pair them together:
# ["peck of pickled pepper","Peter Piper pickled"]
ðì # Prepend a space before each:
# [" peck of pickled pepper"," Peter Piper pickled"]
: # Replace the ["1","0"] with this list of strings
¦ # Remove the leading space (and output implicitly)
See this 05AB1E tip of mine to understand why:
’0 a1.\nA10.\nIf0 a1,\nW€Î's €10ドル?’is"0 a1.\nA10.\nIf0 a1,\nWhere's the10?".•1~1⁄4 ¿•is"pickled""±æ€‚ ÿÇì"is"neck of ÿ pepper"„íÎ ́Œis"peter pipe"„r3⁄4Ðis"r picked"
Haskell, 132 bytes
g x y=x++y++x
p=g"Peter Piper picked"
q=g" peck of pickled peppers"
a=g" ".("a"++).q
f=p(a".\nA"++p".\nIf "++a",\nWhere's the")++"?"
C# (.NET Core), (削除) 123 (削除ここまで) (削除) 118 (削除ここまで) 116 bytes
v=>@"0 a1.
A1 0.
If 0 a1,
Where's the1 0?".Replace("0","Peter Piper picked").Replace("1"," peck of pickled peppers")
Inspired by @Olivier Grégoire's java answer
5 bytes saved by @sebbs
PHP, 102 bytes
Basically just change the repeater words or sentences with numbers, and then apply php-strtr
<?=strtr("0 a 1.
A 1 0.
If 0 a 1,
Where's the 1 0?",["Peter Piper picked","peck of pickled peppers"]);
Or
PHP, 144 bytes
<?=strtr("0 1 25 a 3 of 2l5 4.
A 3 of 2l5 4 0 1 25.
If 0 1 25 a 3 of 2l5 4,
Where's the 3 of 2l5 4 0 1 25?",[Peter,Piper,pick,peck,peppers,ed]);
Explore related questions
See similar questions with these tags.