Problem Set: Perl II

Perl_II
Perl II Problem Set:
 
1. Create a script ("add.pl") that takes two numbers from the command line and adds them.
   % add.pl 2 3
 
   5
 
2. Modify the "add.pl" script from the previous problem set so that it checks that both arguments are defined (hint, use function defined.)
 
   % add.pl 2 3
 
   5
 
   % add.pl 2
 
   Please provide two numbers.    
 
3. Modify the script again so that it checks that both arguments are positive numbers. Zero is allowed, but -1 is not:
 
  % add.pl 2 -3
 
  Please provide two positive numbers.    
 
4.   Write a script ("order.pl") to compare two strings given on the command line arguments and print "right order" if they are in alphabetic order, and "wrong order" if they are not:
 
 % order.pl Fred Lucy
 
 right order
 
 % order.pl Lucy Fred
 
 wrong order
 
5.  Write a script ("reorder.pl") to compare two strings given on the command line and print them out in correct alphabetic order:
 
 % reorder.pl Fred Lucy
 
 Fred Lucy
 
 % reorder.pl Lucy Fred
 
  Fred Lucy
 
6. Write a script named "same.pl" to read two strings from the terminal. Compare them in a case-sensitive manner and print "same" if they are the same, "different" if they are different:
 
 % same.pl
 
 Enter string 1: lucy
 
 Enter string 2: Lucy
 
 different
 
7. Modify this script to compare the strings in a case-INsensitive manner (hint, use the "lc" or "uc" functions to change string to upper or lowercase.
 
8.  Write a script named "percent.pl" to calculate percentages, where the percentage is $i/($i+$j) * 100. Make sure that the script does not crash when given two numbers that add up to zero:
 
% percent.pl 50 150
 
25%
 
% percent.pl 50 -50
 
You are trying to trick me! at line 4.
 
9. Modify this script to use the printf() function to produce nicely formatted floating point numbers (hint:  try "man sprintf" and "man printf" or look it up online to learn about this wonderful function).
 
% percent.pl 50 150
 
25.00 %
 
10.  Write a program named "pali.pl" to detect palindromes. It must be able to handle changes in case.
 
% pali.pl "Madam in Eden Im Adam"
 
yes!
 
% pali.pl gatcctag
 
yes!
 
% pali.pl "cold spring harbor laboratory"
 
                  no!
 
11. Modify the program to work even if there is extraneous punctuation:
 
 % pali.pl "A man, a plan, a canal... Panama"
 
  yes!
 
        (Hint: Look up the s/// pattern matching and substitution function in the Perl reference guide. We will cover this formally in a few days, so you can save this one for later if you like.)
 
12.  Create a file of numbers call numbers.txt with the following content:
 
22
45
1
2
31
32
72
24
 
Extra Credit (Topics to be covered in next lecture):
 
        Here is pseudo-code for a program which uses numbers.txt as input:
 
                  create file myresult.txt and open it for writing output
 
                  open numbers.txt for reading
 
                  while (each line of the file numbers.txt) {
 
                    if (the number is even) {
 
                              if (the number is less than 24) {
 
                                          print the line to STDOUT
 
                              }
 
                    }
                    else {
 
                              compute the factorial of the number
 
                              print the factorial to the file myresult.txt (one per line)
 
                    }
 
                  }
 
        a. What will be printed to STDOUT?
        b. What will be the contents of myresult.txt?
        c. Convert the pseudocode above into a real program.

Comments are closed.