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?
  • 6
    I'm honestly surprised this didn't get closed as a dupe of that Rick-Roll question. Are we not doing that anymore? – Jo King Oct 15 at 12:00
  • 1
    @JoKing it is a different string. A little variety on the same challenge can be fun sometimes. – moonheart08 Oct 15 at 17:23
  • @moonheart08 pretty sure that point was shot down in meta. – Magic Octopus Urn Oct 16 at 18:34

34 Answers 34

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!

  • 1
    That is a very clever use of aliasing!! – Giuseppe Oct 13 at 22:45
  • 1
    Great solution ! It beats also the memCompress approach 47+79=126 bytes – digEmAll Oct 14 at 8:57
  • 1
    This idea saved me another byte here as well! – Giuseppe Oct 15 at 14:10
  • Wow, I didn't spot that golf. That's really nice. – J.Doe Oct 15 at 14:15

Jelly,  80 73 72 68 67 61  57 bytes

“¡ŀṪ]ṃ{yṁ“Ñ3$ṘW5Ḍż⁸¢Hŀ“³Ḍ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)

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!

  • 2
    How did you shave off bytes of a bubblegum answer? – Laikoni Oct 13 at 21:15
  • 2
    @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. – ovs Oct 13 at 21:24
  • 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. – LegionMammal978 Oct 13 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'))

Try it online!

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

Thanks Arnauld, saves 3 bytes.

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.

  • 1
    exit saves 1 byte for the Python 3 program. – Jonathan Allan Oct 14 at 20:14

Jelly, 64 60 58 57 bytes

“¡ŀṪ]ṃ{yṁ“Ñ3$ṘW5Ḍż⁸¢Hŀ»j⁾ a,Ṛẋ2ż“³ḌM“¡FỊİg“ÑɲʋØƥþƈƘ}»FḊ”?

Try it online!

  • 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? – tox123 Oct 14 at 22:14
  • 1
    A lot of the overlap in the code is the identical compressed strings, which is not surprising. – Misha Lavrov Oct 14 at 23:34
  • 1
    @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. – Jonathan Allan Oct 15 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?"

Try it online!

  • 3
    You could move the variable declarations to their first usages with assign default value parameter expansions: Try it online!. – manatwork Oct 13 at 21:26
  • 1
    @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. – Digital Trauma Oct 14 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

Try it online!

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

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!

  • Are you allowed to take a required argument like that? – Quintec Oct 15 at 11:50
  • 2
    @Quintec Yes, if the input is unused – Jo King Oct 15 at 11:58

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.

Ruby, 104 bytes

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

Try it online!

///, 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!

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!

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!

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

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!

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

  • 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!) – BradC Oct 15 at 17:21

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

Try it online!

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

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

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!

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!

  • Missing a point in the first line. – G B Oct 15 at 10:11
  • Use comma instead of concatenation to save 4 bytes: <?=$a=...," a",$b=...,... – Titus Oct 15 at 14:07

05AB1E, 78 76 74 72 bytes

’0 a1.
A10.
If0 a1,
W€Î's €€10?’TS.•1~¼ ¿•“±æ€‚ ÿÇì“'p0ǝ„íδŒ™„r¾Ð«‚ðì:¦

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~¼ ¿•                  # 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"
     ™                    # Titlecased: "Peter Pipe"
      „r¾Ð                # 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~¼ ¿• is "pickled"
  • “±æ€‚ ÿÇì“ is "neck of ÿ pepper"
  • „íδŒ is "peter pipe"
  • „r¾Ð 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")++"?"

Try it online!

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

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!

Powershell, 99 101 bytes

$a='Peter Piper picked'
$b=' peck of pickled peppers'
"$a a$b.
A$b $a.
If $a a$b,
Where's the$b $a`?"
  • 1
    Shouldn't $a='Peter Piper picked' ? – wooshinyobject Oct 16 at 15:56
  • Ah, sure! Thanks. – mazzy Oct 16 at 16:12
  • 1
    It appears as though the space in the $b makes your submission both longer and invalid (TIO). – Jonathan Frech Oct 16 at 16:22
  • 1
    Furthermore, I think you are missing a [...]Peter Piper picked? at your output's end. – Jonathan Frech Oct 16 at 16:23
  • Thanks!, Fixed. – mazzy Oct 16 at 16:33

MBASIC, 146 143 bytes

1 P$="Peter Piper picked":Q$=" peck of pickled peppers":PRINT P$" a"Q$".":PRINT"A"Q$" "P$".":PRINT"If "P$" a"Q$",":PRINT"Where's the"Q$" "P$"?"

It's not very clever, but it works.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.