PERL练习题
- 格式:pdf
- 大小:174.80 KB
- 文档页数:27
sub Fw_Print_Step {my ($step,$description) = @_;print ("\n\n==========================\n");print ("题$step:");if ($description) {print ("$description");}print ("\n==========================\n");}#*******************##题1:#设置变量int1的值为2my $int1=2;#设置变量int2的值为10my $int2=10;#比较变量int1与int2的大小,并打印出比较结果#*******************#Fw_Print_Step ($step++,"比较变量int1与int2的大小,并打印出比较结果"); print "\n变量int1=$int1,int2=$int2";print "\n比较结果:";if ($int1<$int2) {print "$int1 < $int2";} elsif ($int1>$int2) {print "$int1 > $int2";} else {print "$int1 = $int2";}#*******************##题2:#使用for循环打印出如下的字符。
# 1# 12# 123# 12345#*******************#Fw_Print_Step ($step++,"使用for循环打印出如下的字符。
11212312345");my $str= "";$str= $str.$_;if ($_==4) {$str= $str.$_+1;}print " $str\n";}#*******************##题3:my $str1 = "abc";my $str2 = "efg";#将上述2个字符串连接起来,并输出合并后的字符串长度#*******************#Fw_Print_Step ($step++,"将上述2个字符串\"$str1\"和\"$str2\"连接起来,并输出合并后的字符串长度");my $str =$str1.$str2;my $str_length=length($str);print "新字串$str的长度为:$str_length";#*******************##题4:#以逆序方式打印出字符串包含的各个字符,如变量为"123456789"则输出为"9","8",..."2","1". my $str1="abc123def456";#*******************#Fw_Print_Step ($step++,"以逆序方式打印出字符串包含的各个字符,如变量为\"123456789\"则输出为\"9\",\"8\",...\"2\",\"1\".");my $str=$str1;print "以逆序方式打印出字符串\"$str1\"包含的各个字符:\n";for($length=length($str1); $length>0; $length--) {$sub_str=chop($str);if ($length>1) {print "\"$sub_str\",";} else {print "\"$sub_str\".";}}#*******************##题5:#分别使用for与while循环来计算1+2+3+...+100的值#*******************#Fw_Print_Step ($step++,"分别使用for与while循环来计算1+2+3+...+100的值");print "用for循环计算1+2+3+...+100的值:\n ";my $result=0;for (1..100) {$result=$result+$_;}print "1+2+3+...+100=$result";print "\n用while循环计算1+2+3+...+100的值:\n ";my $result=0;my $num=1;while ($num<=100) {$result=$result+$num;$num++;}print "1+2+3+...+100=$result";#*******************##题6:#以逆序的方式打印出端口列表包含的成员口my @cmdArray = ("config", "int fa 0/1", "no shutdonw", "end");#*******************#Fw_Print_Step ($step++,"以逆序的方式打印出端口列表包含的成员口"); for (my $start=$#cmdArray; $start>=0; $start--) {my $array=$cmdArray[$start];print "$array\n";}#*******************##题7:#使用foreach打印出Hash表的所有下标与值my %map = ('red', 0xff0000,'green', 0x00ff00,'blue',0x0000ff);#*******************#Fw_Print_Step ($step++,"使用foreach打印出Hash表的所有下标与值"); foreach $capword (sort keys(%map)) {print ("$capword: $map{$capword}\n");}#while( ($key, $value) = Each (%Map)) {# Print "\N$key=$value;"}#*******************##题8:#使用正则匹配判断字符串是否包含error,若是打印提示信息。
Python练习题+参考答案一、单选题(共57题,每题1分,共57分)1.关于Python的全局变量和局部变量,以下选项中描述错误的是( )A、简单数据类型变量无论是否与全局变量重名,仅在函数内部创建和使用,函数退出后变量被释放B、全局变量指在函数之外定义的变量,一般没有缩进,在程序执行全过程有效C、使用global保留字声明简单数据类型变量后,该变量作为全局变量使用D、局部变量指在函数内部使用的变量,当函数退出时,变量依然存在,下次函数调用可以继续使用正确答案:D2.以下关于循环结构的描述,错误的是:A、遍历循环对循环的次数是不确定的B、遍历循环的循环次数由遍历结构中的元素个数来体现C、非确定次数的循环用 while 语句来实现,确定次数的循环用 for 语句来实现D、非确定次数的循环的次数是根据条件判断来决定的正确答案:A3.以下不能创建一个字典的语句是( )A、dict = {(4,5,6):‘dictionary’}B、dict = {[4,5,6]:‘dictionary’}C、dict= {4:6}D、dict = {}正确答案:B4.下面哪一个不是Python语言的合法命名( )A、3monthlyB、monthlyC、monTHlyD、_Monthly3_正确答案:A5.以下选项中不是文件操作函数或方法的是( )A、readB、writelinesC、readlinesD、load正确答案:D6.以下关于Python循环结构的描述中,错误的是( )A、遍历循环中的遍历结构可以是字符串、文件、组合数据类型和range()函数B、continue只结束本次循环C、break用来结束当前次语句,但不跳出当前的循环体D、Python通过for、while等保留字构建循环结构正确答案:C7.在print函数的输出字符串中可以将( )作为参数,代表后面指定要输出的一个字符。
A、%dB、%tC、%cD、%s正确答案:C8.下列快捷键中能够中断(Interrupt Execution)Python程序运行的是( )A、F6B、Ctrl + QC、Ctrl + CD、Ctrl + F6正确答案:C9.字符串是一个字符序列,例如,字符串s,从右侧向左取第3个字符用( )索引?A、s[0:-3]B、s[-3]C、s[3]D、s[:-3]正确答案:B10."下面代码的输出结果是( ) for a in ‘mirror’: print(a, end="") if a == ‘r’: break"A、MirrorB、mirC、mirrorD、mi正确答案:B11.字符串是一个连续的字符序列,用( )方式打印出可以换行的字符串。
pink练习题一、基础知识类1. 请列举出五种常见的编程语言及其主要用途。
2. 简述面向对象编程中的三大特性。
3. 请解释什么是数据结构,并列举出三种常见的数据结构。
4. 描述操作系统的五大功能。
5. 请说明计算机网络中的OSI七层模型。
6. 简述数据库的基本概念,包括数据库、数据库管理系统和SQL语言。
7. 请解释什么是算法,并列举出三种常见的排序算法。
8. 描述软件工程的五大过程模型。
9. 请说明计算机硬件系统的主要组成部分。
10. 简述计算机软件的分类。
二、编程实践类1. 编写一个Python程序,实现输入一个整数,输出它的阶乘。
2. 编写一个C++程序,实现输入一个字符串,输出它的反转形式。
3. 编写一个Java程序,实现一个简单的计算器功能,包括加、减、乘、除。
4. 编写一个JavaScript程序,实现一个简单的网页时钟。
5. 编写一个HTML和CSS代码,实现一个简单的网页布局。
6. 编写一个SQL查询语句,查询学生表中年龄大于18岁的学生信息。
7. 编写一个PHP程序,实现用户登录功能。
8. 编写一个React组件,实现一个待办事项列表。
9. 编写一个Node.js程序,实现一个简单的HTTP服务器。
10. 编写一个TypeScript程序,实现一个简单的类和对象。
三、算法与数据结构类1. 请用伪代码描述冒泡排序算法的实现过程。
2. 请用Python实现快速排序算法。
3. 请用C++实现链表的基本操作,包括插入、删除和查找。
4. 请用Java实现二叉树的前序遍历、中序遍历和后序遍历。
5. 请用JavaScript实现堆排序算法。
6. 请用PHP实现图的邻接矩阵表示和深度优先搜索。
7. 请用C实现哈希表的基本操作,包括插入、删除和查找。
8. 请用Go实现红黑树的插入操作。
9. 请用Rust实现跳表的数据结构。
10. 请用Swift实现并查集的数据结构。
四、操作系统与计算机网络类1. 请解释进程和线程的区别。
2.12 练习写一个程序,计算半径为12.5的圆的周长。
圆周长等于2π(π约为3.1415926)乘以半径。
答案为78.5。
#!/usr/bin/perl$r=12.5;$pai=3.1415926 ;$C=2*$pai*$r;Print “$C\n”;修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入12.5,则应得到和上题一样的结果。
#!/usr/bin/perl$r=<STDIN>;$pai=3.1415926 ;$C=2*$pai*$r;Print “$C\n”;修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
#!/usr/bin/perl$r=<STDIN>;$pai=3.1415926 ;if($r>=0){$C=2*$pai*$r;}If($r<0){$C=0;}Print “$C\n”;写一个程序,用户能输入2 个数字(不在同一行)。
输出为这两个数的积。
#!/usr/bim/perl$a=<STDIN>;$b=<STDIN>;$c=$a*$b;Print”$c”;写一个程序,用户能输入1 个字符串和一个数字(n)(不在同一行)。
输出为,n 行这个字符串,1 次1 行(提示,使用“x”操作符)。
例如,如果用户输入的是“fred”和“3”,则输出为:3 行,每一行均为fred。
如果输入为“fred”和“299792”,则输出为299792 行,每一行均为fred。
#!/usr.bin/perl$string=<STDIN>;$int=<STDIN>;$output=$string x $intprint $output;3.9练习写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。
如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z.写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。
PERL复习题(答案不是标准答案,仅供参考)一、选择题1. What is the simplest type of data that Perl can work with?A elementB scalarC vectorD component2. Which operator can be used to take the bottom item from an array?A popB pushC pullD plant3. Which operator is used to arrange items in character order?A ascendB sortC arrangeD descend4. Rather than using print, what is often used in Perl when formatting is important?A printfB formatC alignD show5. Which modifier can be used when matching in Perl to ignore case?A sB vC iD c6. Which operator can be used to break up a string into more than one part based upon a separator?A chopB splitC divideD parse7. What option do you use when starting Perl to tell it to run in warning mode? _-w \ use warnings (Fill in the blank.)8. Which control structure can be used to execute only a condition is false?A untilB unlessC whileD without9. Which of the following commands should be used to open a filehandle named KAREN to an existing file named sw?A open KAREN “>sw”;B open KAREN, “>sw”;C open “sw” KAREN;D open “>sw”, KAREN;10. Within Perl, which operator is used to change the working directory?A cdB chdirC pwdD wd11. Which operator can be used to access the first item in an array?A shiftB startC right$D left$12. Within a loop, which operator immediately ends execution of the loop?A nextB lastC redoD leave13. Which function can be used to make sure your program exits with a nonzero status even if there a standard error?A hashB noexitC nozeroD die14. Which of the following operators work most like simple searching/pattern matching?A /fB /gC s///D m//15. Which keyword is used in Perl to define a subroutine?A branchB subC splitD make16. You want to close the directory handle EV AN. Which of the following should you use to accomplish this?A close EV AN;B closedir EV AN;C close_directory EV AN;D none of the above17. Which of the following lines of code accomplishes the same thing as $price = $price + 9;?A $price +9;B $price =+9;C $price +=9;D 9 + $price18. What value do variables have before they are first assigned?A undefB nullC 0D nil19. Which Perl function can be used to launch a child process to run a program?A splitB spinC forkD system20. Which Perl function can be used to identify the first found occurrence of a substring?A findB indexC locateD allocate21. Which control structure can be used to loop as long as a conditional expression returns true?A untilB unlessC whileD without22. Which operator can be used to create private variables within a subroutine?A nativeB limitedC myD regional23. Which of the following operators is used to return a value in a subroutine?A returnB sendC giveD show24. Your script has just read in a variable from the keyboard and you want to strip that variable of the newline character that came in. What operator should you use to perform this operation?A tidyB trimC chompD slim25. What is the default sort order in Perl?A alphabeticB numericC ASCIID none of the above26. Within a loop, which operator jumps to the end of the loop but does not exit the loop?A nextB lastC redoD leave27. (不确定)Which of the following should be used to print values in an array that do not contain newlines and add them to the end of each entry?A print @array;B print @array\n;C print “@array\n”;D print “$@array \\”;28. Which string comparison operator would be equivalent to the numeric > operator?A neB eqC geD gt29. Which function can be thought of as the opposite of split?A linkB joinC uniteD bond30. Which operator can be used to add an item to the bottom of an array?A popB pushC pullD plant31. Which of the following mathematical operations has the highest precedence in Perl?A **B ++C +D -32. 以下哪一个字符串直接量的定义方式是错误的()(1)'thank you'(2)" "(3)"a "friend" of yours"(4)"a \"friend\" of yours"33. 以下哪一条语句是错误的()(1)$_= 'hello world';(2)$a='hello world';(3)my $b,$a='hello world';(4)my ($a,$b)=(0,'hello world');34. 要使下面的程序正常运行,while后的表达式应选()。
Perl语言入门实战习题[试题] 《Perl语言入门实战习题》一、计算FASTA文件中每条序列的长度; 输入文件,FASTA格式:注:如果输入文件在windows下产生,在Linux系统下操作时,宜先用dos2unix处理:用法:dos2unix 输入文件输出文件:Perl代码:#!/usr/bin/perl -wuse strict;unless (@ARGV==2) { # @ARGV 传给脚本的命令行参数列表die"Usage: perl $0 <input.fa> <out.len>\n"; # 当命令行参数不是2的时候输出使用说明 }my ($infile,$outfile) = @ARGV; # 把命令行参数赋值给输入文件和输出文件 open IN,$infile || die"error: can't open infile: $infile"; # 打开输入文件句柄IN open OUT,">$outfile" || die$!; # 打开输出文件句柄OUT $/=">";<IN>; # 设置输入记录分隔符为”>”,并去除第一个”>”while ( my $seq = <IN>){ # 把序列ID行和序列赋值给$seqmy $id = $1 if($seq =~ /^(\S+)/); # 获取序列IDchomp $seq; # 去掉末尾的”>”$seq =~ s/^.+?\n//; # 删除第一行$seq =~ s/\s//g; # 删除序列中的空白字符my $len = length($seq); # 计算序列长度print OUT "$id\t$len\n"; # 输出结果到输出文件}$/="\n"; # 把输入记录分隔符改为默认值close IN; # 关闭输入文件句柄close OUT; # 关闭输出文件句柄二、计算FASTA文件中每条序列的GC含量; 输入文件同上,输出文件:Perl代码:#!/usr/bin/perl -wuse strict;unless (@ARGV==2) {# @ARGV 传给脚本的命令行参数列表die"Usage: perl $0 <input.fa> <out.gc>\n";# 当命令行参数不是2的时候输出使用说明}my ($infile,$outfile) = @ARGV;# 把命令行参数赋值给输入文件和输出文件open IN,$infile || die"error: can't open infile: $infile";# 打开输入文件句柄IN open OUT,">$outfile" || die$!;# 打开输出文件句柄OUT$/=">";<IN>;# 设置输入记录分隔符为”>”,并去除第一个”>” while(<IN>){# $_=<IN>,把序列ID行和序列赋值给$_,$_= 可以省略不写my $id = $1 if(/^(\S+)/);# 获取序列IDchomp; # 去掉末尾的”>”s/^.+?\n//;# 删除第一行s/\s//g; # 删除序列中的空白字符my $GC = (tr/GC/GC/);#计算G或C碱基个数my $AT = (tr/AT/AT/);#计算A或T碱基个数my $len = $GC + $AT;# 计算序列非N长度my $gc_cont = $len ? $GC / $len : 0; #计算GC含量,如果长度为0,GC含量算0print OUT "$id\t$gc_cont\n"; # 输出结果到输出文件 }$/="\n";# 把输入记录分隔符改为默认值close IN; # 关闭输入文件句柄close OUT;# 关闭输出文件句柄三、求反相互补序列;输入文件同上,输出文件也是FASTA格式 Perl代码:#!/usr/bin/perl -wuse strict;unless (@ARGV==2) {# @ARGV 传给脚本的命令行参数列表die"Usage: perl $0 <input.fa> <out.gc>\n";# 当命令行参数不是2的时候输出使用说明}my ($infile,$outfile) = @ARGV;# 把命令行参数赋值给输入文件和输出文件open IN,$infile || die"error: can't open infile: $infile";# 打开输入文件句柄IN open OUT,">$outfile" || die$!;# 打开输出文件句柄OUT $/=">";<IN>;# 设置输入记录分隔符为”>”,并去除第一个”>” while (<IN>){# $_=<IN>,把序列ID行和序列赋值给$_,$_= 可以省略不写my $id = $1 if(/^(\S+)/);# 获取序列IDchomp; # 去掉末尾的”>”s/^.+?\n//;# 删除第一行s/\s//g; # 删除序列中的空白字符$_ = reverse $_; # 序列方向tr/ATCG/TAGC/; # 序列互补print OUT ">$id\n",$_,"\n"; # 输出结果到输出文件}$/="\n";# 把输入记录分隔符改为默认值close IN; # 关闭输入文件句柄close OUT;# 关闭输出文件句柄四、列表信息整合;输入列表1:序列长度文件输入文件2:序列测序覆盖深度文件输出文件:把上述两个列表的信息整合成一个列表,并且最后一行给出汇总结果:Perl代码#!/usr/bin/perl -wuse strict;(@ARGV==3) || die"Usage: perl $0 <list1> <list2> <combine.list>\n"; # 当命令行参数不是3的时候输出使用说明my ($infile1,$infile2,$outfile) = @ARGV;# 把命令行参数赋值给输入文件1、输入文件2和输出文件 my %id_len; # 定义一个哈希open IN1,$infile1 || die$!; # 打开第一个文件句柄while(<IN1>){my ($id,$len) = split /\s+/,$_; # split函数用空白符号切割每一行的内容$id_len{$id} = $len; # 哈希赋值:id => length}close IN1; # 关闭第一个文件句柄open IN2,$infile2 || die$!; # 打开第2个文件句柄open OUT,">$outfile" || die$!; # 打开输出文件句柄my $tol_len = 0; # 定义总长度变量,并赋值为0my $tol_depth = 0; # 定义总深度变量,并赋值为0while (<IN2>){my ($id,$depth) = split; # split函数用空白符号切割每一行的内容my $len = $id_len{$id}; # 序列长度print OUT join("\t",$id,$len,$depth),"\n"; # 输出整合信息到输出文件$tol_len += $len; # 长度累加$tol_depth += $len * $depth; # 深度累加}$tol_depth /= $tol_len; # 计算总体平均深度print OUT "Total\t$tol_len\t$tol_depth\n"; # 输出汇总结果到输出文件close IN2; # 关闭第二个输入文件句柄close OUT; # 关闭输出文件句柄五、串流程;Perl在工作中常用于串流程,现有同事写了3个perl脚本分三步将输入文件,infile.txt处理成最终的final.result:第1步:perl step1.pl infile.txt output1 第2步:perl step2.pl infile.txt output2 第3步:perl step3.pl output1 output2 final.result为提高工作效率,现需要写一个脚本使用infile.txt 作为输入文件,直接得到final.result,中间产生的文件结果不保留。
Perl语言入门(第四版)习题答案————————————————————————————————作者:————————————————————————————————日期:《Perl语言入门习题答案》2.12 练习1、写一个程序,计算半径为12.5的圆的周长。
圆周长等于2π(π约为3.1415926)乘以半径。
答案为78.5。
-----------------------/home/confish/perl/girth#!/usr/bin/perl -w#this program calculate a circle's girth#confish@ubuntu7.10$r=12.5;$g=12.5*2*3.1415;print "the girth of the circle is $g\n";-----------------------/home/confish/perl/girth2、修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入12.5,则应得到和上题一样的结果。
-----------------------/home/confish/perl/girthpro#!/usr/bin/perl -w#a better one to calculate girth#confish@ubuntu7.10print"enter the radius of the circle\n";chomp($r=<STDIN>);if($r>0){print"the girth of the circle is ".$r*2*3.1415."\n";}else{print"nonavailable!\n";}-----------------------/home/confish/perl/girthpro3、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
Python练习题(附答案)一、单选题(共57题,每题1分,共57分)1.Python中,用于获取用户输入的命令为( )A、inputB、readC、getD、for正确答案:A2.关于Python的分支结构,以下选项中描述错误的是( )A、分支结构使用if保留字B、Python中if-elif-else语句描述多分支结构C、分支结构可以向已经执行过的语句部分跳转D、Python中if-else语句用来形成二分支结构正确答案:C3."给出如下代码TempStr ="Hello World" 可以输出“World”子串的是( )"A、print(TempStr[–5:0])B、print(TempStr[–5:])C、print(TempStr[–5: –1])D、print(TempStr[–4: –1])正确答案:B4.以下不能创建一个字典的语句是 ( )A、dict3 = {[1,2,(2,3)]: “uestc”}B、dict2 = { 3 : 5 }C、dict1 = {}D、dict4 = {(1,2,3): “uestc”}正确答案:A5.关于lambda函数,以下选项中描述错误的是( )A、lambda函数也称为匿名函数B、lambda函数将函数名作为函数结果返回C、定义了一种特殊的函数D、lambda不是Python的保留字正确答案:D6.下列说法中正确的是( )。
A、continue能结束循环,而break只能结束本次循环B、break能结束循环,而continue只能结束本次循环C、break用在for语句中,而continue用在while语句中D、break用在while语句中,而continue用在for语句中正确答案:B7.关于函数的返回值,以下选项中描述错误的是( )A、函数可以有return,也可以没有B、return可以传递0个返回值,也可以传递任意多个返回值C、函数可以返回0个或多个结果D、函数必须有返回值正确答案:D8.下列不合法的Python变量名是( )A、Python2B、Hello_WorldC、N.xD、sum正确答案:C9.下列表达式的值为True的是( )A、not(1==1 and 0!=1)B、1==1 and 2!=1C、3>2>2D、(2**=3)<(2*=3)正确答案:B10.关于Python循环结构,以下选项中描述错误的是( )A、遍历循环中的遍历结构可以是字符串、文件、组合数据类型和range()函数等B、continue用来结束当前当次语句,但不跳出当前的循环体C、Python通过for、while等保留字构建循环结构D、continue结束整个循环过程,不再判断循环的执行条件正确答案:D11.在Python函数中,用于获取用户输入的是( )A、get()B、input()C、Eval()D、print()正确答案:B12.面代码的执行结果是( ) print(pow(3,0.5)*pow(3,0.5)==3)A、pow(3,0.5)*pow(3,0.5)==3B、FalseC、3D、True正确答案:B13.优先级最高的运算符为( )。
2.12 练习写一个程序,计算半径为12.5的圆的周长。
圆周长等于2n (n约为3.1415926)乘以半径。
答案为78.5。
#!/usr/bin/perl$r=12.5; $pai=3.1415926 ;$C=2*$pai*$r;Print “$C\n”;修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入12.5,则应得到和上题一样的结果。
#!/usr/bin/perl$r=<STDIN>;$pai=3.1415926 ;$C=2*$pai*$r;Print “$C\n”;修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数#!/usr/bin/perl$r=<STDIN>;$pai=3.1415926 ;if($r>=0){$C=2*$pai*$r;}If($r<0){$C=0;}Print “$C\n”;写一个程序,用户能输入2 个数字(不在同一行) 。
输出为这两个数的积。
#!/usr/bim/perl $a=<STDIN>;$b=<STDIN>;$c=$a*$b;Print”$c”;写一个程序,用户能输入1个字符串和一个数字(n)(不在同一行)。
输出为,n行这个字符串,1次1行(提示,使用“ x”操作符)。
例如,如果用户输入的是“fred”和“3”,则输出为:3 行,每一行均为fred。
如果输入为“fred”和“299792', 则输出为299792 行,每一行均为fred。
#!/usr.bin/perl$string=<STDIN>;$int=<STDIN>; $output=$string x $int print $output;3.9 练习写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。
如果是从键盘输入的,那在Unix系统中应当使用CTRL+D 表明end-of-file ,在Win dows 系统中使用CTRL+Z.写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。