建立一个ASCII图表的最常用的单词在一个给定的文本。
例如:
示例的文本可以在这里找到,“爱丽丝在仙境的历险记”,刘易斯·卡罗尔著。
这一具体案文将产生以下图表:
_________________________________________________________________________
|_________________________________________________________________________| she
|_______________________________________________________________| you
|____________________________________________________________| said
|____________________________________________________| alice
|______________________________________________| was
|__________________________________________| that
|___________________________________| as
|_______________________________| her
|____________________________| with
|____________________________| at
|___________________________| s
|___________________________| t
|_________________________| on
|_________________________| all
|______________________| this
|______________________| for
|______________________| had
|_____________________| but
|____________________| be
|____________________| not
|___________________| they
|__________________| so
以上图表所依据的频率如下:
[('she', 553), ('you', 481), ('said', 462), ('alice', 403), ('was', 358), ('that
', 330), ('as', 274), ('her', 248), ('with', 227), ('at', 227), ('s', 219), ('t'
, 218), ('on', 204), ('all', 200), ('this', 181), ('for', 179), ('had', 178), ('
but', 175), ('be', 167), ('not', 166), ('they', 155), ('so', 152)]
第二(检查是否实现了完整的规范):
________________________________________________________________
|________________________________________________________________| she
|_______________________________________________________| superlongstringstring
|_____________________________________________________| said
|______________________________________________| alice
|________________________________________| was
|_____________________________________| that
|______________________________| as
|___________________________| her
|_________________________| with
|_________________________| at
|________________________| s
|________________________| t
|______________________| on
|_____________________| all
|___________________| this
|___________________| for
|___________________| had
|__________________| but
|_________________| be
|_________________| not
|________________| they
|________________| so
排行:
Language Relaxed Strict
========= ======= ======
GolfScript 130 143
Perl 185
Windows PowerShell 148 199
Mathematica 199
Ruby 185 205
Unix Toolchain 194 228
Python 183 243
Clojure 282
Scala 311
Haskell 333
Awk 336
R 298
Javascript 304 354
Groovy 321
Matlab 404
C# 422
Smalltalk 386
PHP 450
F# 452
TSQL 483 507
数字表示特定语言中最短解的长度。“
只包括小于500个字符的解决方案。语言列表按“严格”方案的长度排序。“
发布于 2018-01-03 13:26:31
程序从左到右执行:
发布于 2018-01-03 14:34:15
(主要基于其他Ruby解决方案)
w=($<.read.downcase.scan(/[a-z]+/)-%w{the and of to a i it in or is}).group_by{|x|x}.map{|x,y|[-y.size,x]}.sort[0,22]
k,l=w[0]
puts [?\s+?_*m=76-l.size,w.map{|f,x|?|+?_*(f*m/k)+"| "+x}]
可以使用命令行开关,将文件名作为参数传递即可。(即:ruby1.9 wordfrequency.rb Alice.txt
)
https://stackoverflow.com/questions/-100007087
复制相似问题