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";
참고 : 'All'은 별도의 문자로 간주됩니다.
예:
$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