文字压缩


18

在下面的给定文本中,文本中有些单词在文本中重复多次。使用任何编程语言编写简短的代码即可压缩文本以显示它。换句话说,使用最少的字节数来显示文本。
文本是:

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
老实说,我很惊讶这并没有因为对Rick-Roll问题的欺骗而被关闭。我们不再这样做了吗?
Jo King

1
@JoKing 一个不同的字符串。有时在同一个挑战上有一些变化可能会很有趣。
moonheart08

@ moonheart08非常确定这一点在meta中被击落了。
魔术章鱼缸

Answers:



8

果冻 80 73 72 68 67 61  57 字节

“¡ŀṪ]ṃ{yṁ“Ñ3$ṘW5Ḍż⁸¢Hŀ“³ḌM“¡FỊİg“ÑɲʋØƥþƈƘ}“ṣɠ»“Ƙ9~ḷ’ṃFḊ”?

在线尝试!

怎么样?

“...“...“...“...“...“...»“Ƙ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)

5

泡泡糖73 71字节

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
您是如何剃掉泡泡糖答案的字节的?
Laikoni '18

2
@Laikoni最初的73个字节的答案是在gzip最高压缩级别(-9)上创建的,再加上head和使用的一些元数据刮削tail,而71字节的生成是使用zopfli生成的,我最初忘记了。Zopfli通常创建较短的DEFLATE流。
ovs

是啊,我已经试过了对zopfli 5000000次迭代,它不能在迭代3109找到任何过去的71个字节的一个
LegionMammal978


4

Python 2,115个字节

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

在线尝试!

打印多个逗号分隔的字符串,以在它们之间放置空格。


Python 3,115字节

print("1 a2.\nA2 1.\nIf 1 a2,\nWhere's the2 1?".translate({49:"Peter Piper picked",50:" peck of pickled peppers"}))

在线尝试!

Python 3 translate做繁重的工作。使用具有一位ASCII值的不可打印字符应节省两个字节。


1
exit为Python 3程序保存1个字节。
乔纳森·艾伦

4

果冻64 60 58 57字节

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

在线尝试!


哇,令人惊讶地类似于另一个答案,具有相同的语言和相同的字节数。我实际上不知道这种语言正在发生什么,所以代码基本上是相同的吗?
tox123

1
代码中的很多重叠之处是相同的压缩字符串,这不足为奇。
米沙·拉夫罗夫

1
@tox这两个程序当前不能以相同的方式工作(尽管我们两个人在修订历史中都使用了彼此相似的想法)。这是使用压缩字符串列表(“...“...»)形成四行中的大部分,然后将(ż)与较少重复的部分(例如',\nIf')交织在一起,再次与压缩字符串列表进行交织;您可以从说明中看到我的工作原理。
乔纳森·艾伦


2

V99 87字节

-12个字节:结果是2个替换项更短,这与其他所有人的解决方案基本相同(Bubblegum除外)?

i1 a0.
A0 1.
If 1 a0,
Where's the0 1?Í0/ peck of pickled peppers
Í1/Peter Piper picked

在线尝试!




2

小枝,105字节

这使用简单的替换来填补空白。

Twig的replace()过滤器允许您定义要替换为哈希键的值。幸运的是,它也可以用于数组,因为它们具有数字键。

{{"0a1.
A1 0.
If 0 a1,
Where's the1 0?"|replace(["Peter Piper picked"," peck of pickled peppers"])|raw}}

|raw需要,以避免逃逸,这竟然Where'sWhere's

您可以在https://twigfiddle.com/phqpts上尝试


由于将其编译为PHP,因此PHP的等效项为:

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

可以大大缩短。





1

干净,166字节

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

在线尝试!



1

jq, 110 characters

(106个字符的代码+ 4个字符的命令行选项)

"1 a2.
A2 1.
If 1 a2,
Where's the2 1?"|gsub("1";"Peter Piper picked")|gsub("2";" peck of pickled peppers")

样品运行:

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?

在线尝试!


1

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 <>小提琴


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


1

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.




0

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

0

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!


0

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

Use comma instead of concatenation to save 4 bytes: <?=$a=...," a",$b=...,...
Titus

0

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"



0

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!


0

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

1
It appears as though the space in the $b makes your submission both longer and invalid (TIO).
Jonathan Frech

1
Furthermore, I think you are missing a [...]Peter Piper picked? at your output's end.
Jonathan Frech
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.