Perl练习题(更新完毕)

练习1: 屏幕提示用户输入一个高度值, 并在屏幕上打印出一个相应层
数的三角形, 如详细要求中的图示意。
while(1){
 print ("please input num:(\"q\" to exit):");
 $Inp = ;
 chomp($Inp);
 if($Inp eq "q"){
  last;
 }
 for($i = 1; $i <= $Inp ; $i++){
  for($j = 0; $j< $Inp-$i ; $j++ ){
   print(" ");
  }
  for($j = 0; $j< 2*$i-1 ; $j++){
   print("*");
  }
  print("\n");
 }
}
练习1源代码下载 ex1_print_triangle.pl

练习2:随机生成含有100道两位数”加/减/乘/除”的数学题, 要求运 算数随机,运算符也随机, 结果保存到文件中。
另,假设用户群体为小学低年级。减法结果不得小于零;除法必须整 除。
$i = 0;
while($i<100){
   print ("$Num1-$Num2 = \t");
   $i++;
  }
 }elsif($op > 2.5 and $op<=5){
  if($Num1+$Num2<=100){
   print ("$Num1+$Num2 = \t");
   $i++;
  }
 }elsif($op >5 and $op <=7.5){
  if( $Num1*$Num2 >=100){
   print ("$Num1*$Num2 = \t");
   $i++;
  }
 }elsif($op >7.5 and $op <=10){
  if($Num2 != 0 and int($Num1/$Num2)==$Num1/$Num2){
   print ("$Num1/$Num2 = \t");
   $i++;
  }
 }
}
练习2源代码下载 ex2_print_100_questions.pl

练习3:独立编写查词典和词频统计程序。
词典为dict.txt
词频统计的对象是mytext
open(Input , "dict.txt");
while($Line = ){
 chomp($Line);
 ($K,$V) = split("=>",$Line);
 $hash{$K} = $V;
}
close(Input);

while(1){
 print "Input the word u want to search:(\"q\" to exit)\n";
 $Line = ;
 chomp($Line);
 if($Line eq "q"){
  last;
 }
 if( defined $hash{$Line}){
  print "$hash{$Line}\n";
 }else{
  print "没有找到\n";
 }
}
练习3查字典源代码下载 ex3_dict.pl
dict.txt文件下载
词频统计
open(Input , "mytext");
while($Line = ){
 chomp($Line);
 foreach $Item( split(" ",$Line) ){
  $hash{$Item}++;
 }
}
close(Input);

foreach $K(sort keys %hash){
 print "$K $hash{$K} \n";
}
练习3词频分析 ex_frequency.pl
词频分析用到的mytext

练习4:统计两个文件中共同出现的单词,并输出它们在每个文件中出现 的次数。两个文件在files.rar中。
open(Input , "eng1.txt");
while($Line = ){
 chomp($Line);
 foreach $Item(split(" ",$Line)){
  $hash1{$Item}++;
 }
}
close(Input);

open(Input , "eng2.txt");
while($Line = ){
 chomp($Line);
 foreach $Item(split(" ",$Line)){
  if( defined $hash1{$Item}){
   $hash2{$Item}++;
  }
 } 
}
close(Input);

foreach $K(sort keys %hash2){
 if(defined $hash1{$K}){
  print "$K $hash1{$K} $hash2{$K}\n";
 }
}

练习4源程序下载 ex4_CountWords.pl
练习4用到的eng1eng2

练习5:合并两部词典,要求词条唯一,译文合并。词典见附件

需要合并的两部字典

练习6:输出指定路径中所有重复出现的文件的文件名(文件名包括后缀)和出现次数。


练习7:统计一个目录下文件出现哪些词,每个词出现的总频次.用函数形式,统计每个文件出现的词的情况。

请提交代码和词频表(从高频到低频排序);词频表为文本文件
(.txt);格式举例
寺庙 123
大佛 111
……
详细要求: 射雕英雄传

练习8:统计词典中所有拼音(带音调)出现的频率;
请提交代码和频率表(从高频到低频排列,使用它.txt文档)格式例子:
tian2 111
pai4 134
……

详细要求: IdiomPY

练习9:作业要求: 统计语料中的声调出现情况。输出每种音调的出现次数。

格式为(音调以数字表示)
4 1417
5 773
2 721
1 688
3 667

仅需提交代码。语料为Uebungen.txt
详细要求: 统计语料中的声调出现情况
练习9 统计语料中的声调出现情况

参考答案:

open(iD,".txt");
while($line=<iD>)
{
chomp $line;
if($line=~/^\<PY\>/)
{
@array=$line=~/([0,1,2,3,4,5])/g;
foreach  (@array) {
$hash{$_}++;
}
}
}
close(iD);
foreach  (sort keys %hash) {
print $_."\t".$hash{$_}."\n";
}


练习10:作业要求: 从语料中生成索引为拼音的字典。语料为Uebungen.txt
格式为:
mang2 茫;茫;忙
leng3 冷
you4 又
jing4 静;净;镜;境
tui3 腿
………

仅需上传代码。

详细要求: 从语料中生成索引为拼音的字典
练习10 从语料中生成索引为拼音的字典

参考答案:

open(iD,"12.txt");
while($line=<iD>)
{
chomp $line;
if($line=~/^\<PY\>/)
{
@array=$line=~/([^\>\s]+)\[([^\]]+)\]/g;
for($i=0;$i<@array;$i++){
if($i%2==1)
{
@py=$array[$i]=~/(\S+)/g;
@hz=$array[$i-1]=~/.{2}/g;
for($j=0;$j<@py;$j++)
{
$hash{$py[$j]}=$hash{$py[$j]}.';'.$hz[$j];
}
}
}
}
}
close(iD);
#tian1 天;天;天;……
open(out,">pyHzDict.txt");
foreach $ele (sort keys %hash) {
@berry=$hash{$ele}=~/([^\;]+)/g;
foreach  (@berry) {
$temp{$_}=0;
}
print out $ele."\t";
foreach  (sort keys %temp) {
print out $_.';';
}
print out "\n";
%temp=();
}
close(out);

作业11:

作业要求: 生成汉字-拼音表
从IdiomPY.txt(见参考资料-Perl语料)中生成汉字-拼音表(注
意多
音字,需要把所有拼音标出)
格式:

行 xing2;hang2
猪 zhu1;
……

作业12:

根据transcript.txt中拼音信息,生成一个带有拼音的词表(词有多音)

答案:

open(in,"transcript.txt");
while ($line=<in>) {
    chomp $line;
    if($line=~/^\<PY\>/){
        @array=$line=~/([^\>\s]+)\[([^\]]+)\]/g;
        #print @array;
        #print "\n";
        for($i=0;$i<@array-1;$i=$i+2){
            @tmpchar=$array[$i]=~/(.{2})/g;
            @tmpPY=$array[$i+1]=~/(\S+)/g;
            for($j=0;$j<@tmpchar;$j++){
                ${$dict{$tmpchar[$j]}}{$tmpPY[$j]}=0;
            }
        }
    }
}
close(in);
foreach $ele (sort keys %dict) {
    print $ele.":  ";
    foreach $e(sort keys %{$dict{$ele}}) {
        print $e.";";
    }
    print "\n";
}



练习13:

作业要求: 在练习12所使用语料库中,统计连续的单字(多字词之间、单字
和多字词之间)的转移概率。如全文共有N个单字对。每个单字对
的出现概率为其频次F除以N;
请编程输出语料库中所有的连续单字对的出现概率。
同理,输出语料库中所有的连续单字的拼音对出现的概率。

格式如
之_后 0.0003212
之_前 0.0012345
……
详细要求: 单字转移概率和拼音转移概率

练习14:

作业要求: 实现一个成语接龙游戏;

例如:
由人输入:国富民安,
机器回答:安邦定国
由人输入:……
及其回答:……
(仅仅举例,实际有的词成语词典里未必有)

练习16:
编写一个PDF爬虫,爬取中华人民共和国环保部的环境保护标准。

Related Articles

0 评论 :

发表评论

Quote Of The Day