PHP(240个字符)
将html分为字符串(用作分隔符),然后运行几个正则表达式以提取说出的姓名和单词。节省朗读到数组中的单词的长度。打高尔夫球:
<?@$p=preg_match_all;foreach(explode('/bl',implode(file(trim(fgets(STDIN)))))as$c)if($p('/=s.*?b>(.*?):?</',$c,$m)){$p('/=\d.*?>(.*?)</',$c,$o);foreach($m[1]as$n)@$q[$n]+=strlen(implode($o[1]));}arsort($q);foreach($q as$n=>$c)echo"$n $c\n";
取消高尔夫:
<?php
$html = implode(file(trim(fgets(STDIN))));
$arr = explode('/bl',$html);
foreach($arr as $chunk){
if(preg_match_all('/=s.*?b>(.*?):?</',$chunk,$matches)){
$name = $matches[1];
preg_match_all('/=\d.*?>(.*?)</',$chunk,$matches);
foreach($name as $n)
@$names[$n] += strlen(implode($matches[1]));
}
}
arsort($names);
foreach($names as $name=>$count)
echo "$name $count\n";
注意:这将“全部”视为一个单独的字符。
例:
$php shakespeare.php <<< "http://shakespeare.mit.edu/hamlet/full.html"
HAMLET 60063
KING CLAUDIUS 21461
LORD POLONIUS 13877
HORATIO 10605
LAERTES 7519
OPHELIA 5916
QUEEN GERTRUDE 5554
First Clown 3701
ROSENCRANTZ 3635
Ghost 3619
MARCELLUS 2350
First Player 1980
OSRIC 1943
Player King 1849
GUILDENSTERN 1747
Player Queen 1220
BERNARDO 1153
Gentleman 978
PRINCE FORTINBRAS 971
VOLTIMAND 896
Second Clown 511
First Priest 499
Captain 400
Lord 338
REYNALDO 330
FRANCISCO 287
LUCIANUS 272
First Ambassador 230
First Sailor 187
Messenger 185
Prologue 94
All 94
Danes 75
Servant 49
CORNELIUS 45