19
\$\begingroup\$

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?
xnor
150k26 gold badges287 silver badges676 bronze badges
asked Oct 13, 2018 at 15:50
\$\endgroup\$
3
  • 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\$ Commented 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\$ Commented Oct 15, 2018 at 17:23
  • 1
    \$\begingroup\$ @moonheart08 pretty sure that point was shot down in meta. \$\endgroup\$ Commented Oct 16, 2018 at 18:34

35 Answers 35

1
2
10
\$\begingroup\$

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

Try it online!

answered Oct 13, 2018 at 17:04
\$\endgroup\$
4
  • 1
    \$\begingroup\$ That is a very clever use of aliasing!! \$\endgroup\$ Commented Oct 13, 2018 at 22:45
  • 1
    \$\begingroup\$ Great solution ! It beats also the memCompress approach 47+79=126 bytes \$\endgroup\$ Commented Oct 14, 2018 at 8:57
  • 1
    \$\begingroup\$ This idea saved me another byte here as well! \$\endgroup\$ Commented Oct 15, 2018 at 14:10
  • \$\begingroup\$ Wow, I didn't spot that golf. That's really nice. \$\endgroup\$ Commented Oct 15, 2018 at 14:15
8
\$\begingroup\$

Jelly, (削除) 80 73 72 68 67 61 (削除ここまで) 57 bytes

"¡l·Ṫ]ṃ{yṁ"Ñ3$ṘW5Ḍż8¢Hl·"3ḌM"¡FỊİg"ÑɲʋØƥþƈƘ}"ṣɠ»"Ƙ9~ḷ’ṃFḊ"?

Try it online!

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)
answered Oct 13, 2018 at 21:05
\$\endgroup\$
5
\$\begingroup\$

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%."{.

Try it online!

answered Oct 13, 2018 at 16:46
\$\endgroup\$
3
  • 2
    \$\begingroup\$ How did you shave off bytes of a bubblegum answer? \$\endgroup\$ Commented Oct 13, 2018 at 21:15
  • 2
    \$\begingroup\$ @Laikoni the original 73 bytes answer was created using gzip on the highest compression level (-9) plus some metadata shaving using head and tail, the 71 byter is generated using zopfli, which I initially forgot about. Zopfli usually creates shorter DEFLATE streams. \$\endgroup\$ Commented 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\$ Commented Oct 13, 2018 at 22:30
5
\$\begingroup\$

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

Try it online!

I would claim this answer is from ovs, anyway, 19 bytes saved.

Thanks Arnauld, saves 3 bytes.

answered Oct 13, 2018 at 16:11
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 114 bytes. \$\endgroup\$ Commented Oct 13, 2018 at 17:29
4
\$\begingroup\$

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

Try it online!

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

Try it online!

Python 3's translate does the heavy lifting. Using non-printable characters with single-digit ASCII value should save two bytes.

answered Oct 14, 2018 at 5:10
\$\endgroup\$
1
  • 1
    \$\begingroup\$ exit saves 1 byte for the Python 3 program. \$\endgroup\$ Commented Oct 14, 2018 at 20:14
4
\$\begingroup\$

Jelly, (削除) 64 (削除ここまで) (削除) 60 (削除ここまで) (削除) 58 (削除ここまで) 57 bytes

"¡l·Ṫ]ṃ{yṁ"Ñ3$ṘW5Ḍż8¢Hl·»j) a,Ṛẋ2ż"3ḌM"¡FỊİg"ÑɲʋØƥþƈƘ}»FḊ"?

Try it online!

answered Oct 14, 2018 at 4:11
\$\endgroup\$
3
  • \$\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\$ Commented 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\$ Commented 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\$ Commented Oct 15, 2018 at 12:31
3
\$\begingroup\$

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

Try it online!

answered Oct 13, 2018 at 18:31
\$\endgroup\$
2
  • 3
    \$\begingroup\$ You could move the variable declarations to their first usages with assign default value parameter expansions: Try it online!. \$\endgroup\$ Commented 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\$ Commented Oct 14, 2018 at 2:08
2
\$\begingroup\$

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

Try it online!

answered Oct 13, 2018 at 18:42
\$\endgroup\$
2
\$\begingroup\$

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

answered Oct 14, 2018 at 1:18
\$\endgroup\$
2
  • 1
    \$\begingroup\$ That's not a format string. That's a format string. (117 bytes) \$\endgroup\$ Commented Oct 14, 2018 at 2:16
  • 1
    \$\begingroup\$ Programs may output to STDERR, so save 1 by replacing print with exit. \$\endgroup\$ Commented Oct 14, 2018 at 20:12
2
\$\begingroup\$

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

Try it online!

answered Oct 13, 2018 at 21:15
\$\endgroup\$
2
  • \$\begingroup\$ Are you allowed to take a required argument like that? \$\endgroup\$ Commented Oct 15, 2018 at 11:50
  • 2
    \$\begingroup\$ @Quintec Yes, if the input is unused \$\endgroup\$ Commented Oct 15, 2018 at 11:58
2
\$\begingroup\$

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.

answered Oct 15, 2018 at 10:06
\$\endgroup\$
2
\$\begingroup\$

Ruby, 104 bytes

/ a/=~$a="Peter Piper picked a peck of pickled peppers"
puts"#$a.
A#$' #$`.
If #$a,
Where's the#$' #$`?"

Try it online!

answered Oct 15, 2018 at 10:20
\$\endgroup\$
1
\$\begingroup\$

///, 86 bytes

/1/Peter Piper picked//2/ peck of pickled peppers/1 a2.
A2 1.
If 1 a2,
Where's the2 1?

Try it online!

answered Oct 14, 2018 at 2:20
\$\endgroup\$
1
\$\begingroup\$

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");}

Try it online!

answered Oct 14, 2018 at 3:55
\$\endgroup\$
1
\$\begingroup\$

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

Try it online!

answered Oct 14, 2018 at 5:52
\$\endgroup\$
1
\$\begingroup\$

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

Try it online!

-1 byte thanks to @DigitalTrauma

answered Oct 14, 2018 at 2:02
\$\endgroup\$
1
  • \$\begingroup\$ Replace .* with ^ to save a byte \$\endgroup\$ Commented Oct 14, 2018 at 2:10
1
\$\begingroup\$

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?

Try it online!

answered Oct 15, 2018 at 9:55
\$\endgroup\$
1
\$\begingroup\$

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+'?'

db<>fiddle

answered Oct 15, 2018 at 15:32
\$\endgroup\$
1
  • \$\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 of char(10), in fact you can put the line breaks directly in the print statement and eliminate @c entirely. Pick your most-used variable and use @ by itself (its valid!) \$\endgroup\$ Commented Oct 15, 2018 at 17:21
1
\$\begingroup\$

Stax, (削除) 60 (削除ここまで) 56 bytes

╣lF╤╨┴+Y╟W╪▄,しろまるF«↑•L°T»`┼◄ü√}x![Ñ$Θ☼2qσQ1⁄4▬ôZ¡▄╙╥⌂å╛►¶▓&╗s

Run and debug it

answered Oct 16, 2018 at 21:29
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Here's one byte shorter unpacked, and this one seems to be making a profound statement about existence. \$\endgroup\$ Commented 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\$ Commented Aug 23, 2019 at 17:50
  • \$\begingroup\$ Descartes ain't not nothin' on me. \$\endgroup\$ Commented Aug 23, 2019 at 18:05
1
\$\begingroup\$

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.

answered Oct 20, 2018 at 20:52
\$\endgroup\$
1
\$\begingroup\$

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.

answered Oct 15, 2018 at 17:14
\$\endgroup\$
0
\$\begingroup\$

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

Try it online!

answered Oct 13, 2018 at 19:03
\$\endgroup\$
0
\$\begingroup\$

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.

answered Oct 13, 2018 at 20:59
\$\endgroup\$
0
\$\begingroup\$

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

Try it online!

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 "?"
]
answered Oct 14, 2018 at 6:47
\$\endgroup\$
0
\$\begingroup\$

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

Try it online!

answered Oct 14, 2018 at 7:35
\$\endgroup\$
0
0
\$\begingroup\$

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

Try it online!

answered Oct 13, 2018 at 23:26
\$\endgroup\$
2
  • \$\begingroup\$ Missing a point in the first line. \$\endgroup\$ Commented Oct 15, 2018 at 10:11
  • \$\begingroup\$ Use comma instead of concatenation to save 4 bytes: <?=$a=...," a",$b=...,... \$\endgroup\$ Commented Oct 15, 2018 at 14:07
0
\$\begingroup\$

05AB1E, (削除) 78 (削除ここまで) (削除) 76 (削除ここまで) (削除) 74 (削除ここまで) 72 bytes

’0 a1.
A10.
If0 a1,
W€Î's €10ドル?’TS.•1~1⁄4 ¿•"±æ€‚ ÿÇì"'p0ǝ„íÎ ́ŒTM„r3⁄4Ы‚ðì:¦

Try it online.

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"
answered Oct 15, 2018 at 14:00
\$\endgroup\$
0
\$\begingroup\$

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

Try it online!

answered Oct 16, 2018 at 6:48
\$\endgroup\$
0
\$\begingroup\$

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

Try it online!

Inspired by @Olivier Grégoire's java answer

5 bytes saved by @sebbs

answered Oct 15, 2018 at 9:34
\$\endgroup\$
0
\$\begingroup\$

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"]);

Try it online!

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

Try it online!

answered Oct 16, 2018 at 12:54
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.