随着最近有关在代码高尔夫中使用压缩工具的讨论,我认为编写自己的文本压缩器和解压缩器将是一个不错的挑战。
挑战:
编写两个程序:一个程序将ASCII文本压缩为一个字节序列,另一个程序将其解压缩。程序不必使用相同的语言。
第一个程序应读取一段ASCII文本(从文件或标准输入,或使用该语言最自然的任何机制),并输出其压缩版本。(压缩的输出可以包含任意字节,也可以是任意字节;它不必可读。)第二个程序应读取第一个程序的输出并重新创建原始输入文本。
得分:
解决方案的分数将是以下三个计数的总和:
- 压缩程序的长度(以字符为单位)。
- 的输出的长度给出下面的测试输入,以字节为单位的压缩机。
- 解压缩程序的长度(如果与压缩程序不同),以字符为单位。
您应该在回答中记下所有三个计数及其总和。由于这是标准高尔夫,得分越低越好。
规则和限制:
您可能不会使用任何预先存在的压缩或解压缩工具或库,即使它们与您选择的语言捆绑在一起也是如此。如果不确定是否允许使用给定的工具或功能,请询问。
您的压缩程序必须能够处理由任何可打印ASCII文本组成的输入,包括制表符(ASCII 9)和换行符(ASCII 10)。您可能但不是必须处理任意Unicode和/或二进制输入。
您的解压缩程序必须产生与作为输入的压缩器完全相同的输出。特别是,如果输入中没有尾随换行符,请注意不要输出。(下面的测试输入确实包含尾随换行符,因此您需要对此进行单独测试。GolfScript提示:
'':n
。)您的压缩器和解压缩器可能是同一程序(选择了适当的模式,例如使用命令行开关)。在这种情况下,其长度仅计算一次。
程序不应过慢或占用大量内存。如果压缩或解压缩测试输入在不是很新的台式机(2.2GHz AMD Athlon64 X2)上花费了超过一分钟,或者消耗了超过1 GB的RAM,那么我将裁定该解决方案无效。这些限制是故意放宽的-请尽量不要推动它们。(请参阅下面的修订:在这些限制之内,您必须至少能够处理100 kB的输入。)
即使仅测试输入对评分很重要,您也至少应努力压缩任意输入文本。仅针对测试输入即可达到不错的压缩比的解决方案在技术上是有效的,但不会对我不利。
您的压缩程序和解压缩程序应该是独立的。特别是,如果它们依赖于能够读取不属于所选语言的标准运行时环境的一部分的文件或网络资源,则该文件或资源的长度应计为程序长度的一部分。(这是不允许“压缩器”将输入与网络上的文件进行比较,如果匹配则输出零字节。对不起,但这不再是新技巧了。)
修订和澄清:
您的压缩器必须能够在合理的时间和内存使用量(最多一分钟,一GB内存)内处理至少100 kB的典型英文文本。您的解压缩器必须能够在相同限制内解压缩结果输出。当然,能够处理比该时间更长的文件是完全可以的,值得称赞。可以将长输入文件分成多个块并分别进行压缩,也可以使用其他方法来权衡压缩效率,以提高长输入的速度。
您的压缩器可能需要使用首选平台的本机换行符表示形式(LF,CR + LF,CR等)来提供其输入,只要您的解压缩器在其输出中使用相同的换行符表示形式即可。当然,只要您的解压缩器输出与原始输入中相同类型的换行符,压缩器也可以接受任何类型的换行符(甚至不考虑平台,甚至仅接受Unix换行符)也是可以的。
测试输入:
为了判断答案的压缩效率,将使用以下测试输入(Edgar Allan Poe撰写的The Raven,由Gutenberg项目提供):
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore,
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
"'T is some visiter," I muttered, "tapping at my chamber door--
Only this, and nothing more."
Ah, distinctly I remember it was in the bleak December,
And each separate dying ember wrought its ghost upon the floor.
Eagerly I wished the morrow:--vainly I had sought to borrow
From my books surcease of sorrow--sorrow for the lost Lenore--
For the rare and radiant maiden whom the angels name Lenore--
Nameless here for evermore.
And the silken sad uncertain rustling of each purple curtain
Thrilled me--filled me with fantastic terrors never felt before;
So that now, to still the beating of my heart, I stood repeating
"'T is some visiter entreating entrance at my chamber door
Some late visiter entreating entrance at my chamber door;--
This it is, and nothing more."
Presently my soul grew stronger; hesitating then no longer,
"Sir," said I, "or Madam, truly your forgiveness I implore;
But the fact is I was napping, and so gently you came rapping,
And so faintly you came tapping, tapping at my chamber door,
That I scarce was sure I heard you"--here I opened wide the door;--
Darkness there, and nothing more.
Deep into that darkness peering, long I stood there wondering, fearing,
Doubting, dreaming dreams no mortal ever dared to dream before;
But the silence was unbroken, and the darkness gave no token,
And the only word there spoken was the whispered word, "Lenore!"
This I whispered, and an echo murmured back the word, "Lenore!"
Merely this and nothing more.
Back into the chamber turning, all my soul within me burning,
Soon again I heard a tapping, somewhat louder than before.
"Surely," said I, "surely that is something at my window lattice;
Let me see, then, what thereat is, and this mystery explore--
Let my heart be still a moment and this mystery explore;--
'T is the wind and nothing more!"
Open here I flung the shutter, when, with many a flirt and flutter,
In there stepped a stately Raven of the saintly days of yore.
Not the least obeisance made he; not a minute stopped or stayed he;
But, with mien of lord or lady, perched above my chamber door--
Perched upon a bust of Pallas just above my chamber door--
Perched, and sat, and nothing more.
Then this ebony bird beguiling my sad fancy into smiling,
By the grave and stern decorum of the countenance it wore,
"Though thy crest be shorn and shaven, thou," I said, "art sure no craven,
Ghastly grim and ancient Raven wandering from the Nightly shore,--
Tell me what thy lordly name is on the Night's Plutonian shore!"
Quoth the Raven, "Nevermore."
Much I marvelled this ungainly fowl to hear discourse so plainly,
Though its answer little meaning--little relevancy bore;
For we cannot help agreeing that no living human being
Ever yet was blessed with seeing bird above his chamber door--
Bird or beast upon the sculptured bust above his chamber door,
With such name as "Nevermore."
But the Raven, sitting lonely on the placid bust, spoke only
That one word, as if his soul in that one word he did outpour.
Nothing further then he uttered--not a feather then he fluttered--
Till I scarcely more than muttered, "Other friends have flown before--
On the morrow _he_ will leave me, as my hopes have flown before."
Then the bird said, "Nevermore."
Startled at the stillness broken by reply so aptly spoken,
"Doubtless," said I, "what it utters is its only stock and store,
Caught from some unhappy master whom unmerciful Disaster
Followed fast and followed faster till his songs one burden bore--
Till the dirges of his Hope that melancholy burden bore
Of 'Never--nevermore.'"
But the Raven still beguiling all my sad soul into smiling,
Straight I wheeled a cushioned seat in front of bird and bust and door;
Then, upon the velvet sinking, I betook myself to linking
Fancy unto fancy, thinking what this ominous bird of yore--
What this grim, ungainly, ghastly, gaunt and ominous bird of yore
Meant in croaking "Nevermore."
This I sat engaged in guessing, but no syllable expressing
To the fowl whose fiery eyes now burned into my bosom's core;
This and more I sat divining, with my head at ease reclining
On the cushion's velvet lining that the lamplight gloated o'er,
But whose velvet violet lining with the lamplight gloating o'er
_She_ shall press, ah, nevermore!
Then, methought, the air grew denser, perfumed from an unseen censer
Swung by seraphim whose foot-falls tinkled on the tufted floor.
"Wretch," I cried, "thy God hath lent thee--by these angels he hath sent thee
Respite--respite and nepenthe from thy memories of Lenore!
Quaff, oh quaff this kind nepenthe, and forget this lost Lenore!"
Quoth the Raven, "Nevermore."
"Prophet!" said I, "thing of evil!--prophet still, if bird or devil!--
Whether Tempter sent, or whether tempest tossed thee here ashore,
Desolate yet all undaunted, on this desert land enchanted--
On this home by Horror haunted--tell me truly, I implore--
Is there--_is_ there balm in Gilead?--tell me--tell me, I implore!"
Quoth the Raven, "Nevermore."
"Prophet!" said I, "thing of evil--prophet still, if bird or devil!
By that Heaven that bends above, us--by that God we both adore--
Tell this soul with sorrow laden if, within the distant Aidenn,
It shall clasp a sainted maiden whom the angels name Lenore--
Clasp a rare and radiant maiden whom the angels name Lenore."
Quoth the Raven, "Nevermore."
"Be that word our sign of parting, bird or fiend!" I shrieked, upstarting--
"Get thee back into the tempest and the Night's Plutonian shore!
Leave no black plume as a token of that lie thy soul hath spoken!
Leave my loneliness unbroken!--quit the bust above my door!
Take thy beak from out my heart, and take thy form from off my door!"
Quoth the Raven, "Nevermore."
And the Raven, never flitting, still is sitting, still is sitting
On the pallid bust of Pallas just above my chamber door;
And his eyes have all the seeming of a demon's that is dreaming,
And the lamplight o'er him streaming throws his shadow on the floor;
And my soul from out that shadow that lies floating on the floor
Shall be lifted--nevermore!
正确的测试输入(使用Unix风格的LF换行符编码)应为7043个字节长,并具有十六进制的MD5哈希值286206abbb7eca7b1ab69ea4b81da227
。(md5sum -t
即使在DOS / Windows上使用CR + LF换行符,也应产生相同的哈希值。)解压缩器的输出应具有相同的长度和哈希值。
附言 请记住,这一挑战只是您所面对的。实际上,任何低于7043的东西都算是一个好成绩。(在天平的另一端,我会非常若有人2500下实现了深刻的印象分)