If you are a course participant, please send the solutions to these exercises to erikt(at)science.uva.nl before or on Friday February 29, 2008. Do not forget to test your programs. Include the test results in your weekly reports.
There are six exercises. Each exercise is worth 1.5 points. Counting starts at 1.0. The maximum attainable score for these exercises is 10.0.
All the programs you construct for these exercises must contain the line use strict at the top.
Write program that reads two numbers from the keyboard and then shows the product of these numbers. Example run:
$ perl -w 7.1.pl Please enter the first number: 6 Please enter the second number: 7 The product of 6 and 7 is 42 $
Write a program that reads a list of positive numbers (one per line) and stops when a zero (0) is entered. The program should print the largest and smallest number of the sequence, if these exist. If the two numbers do not exists (that is, when the list is empty) then nothing should be printed. The zero (0) should not be considered as a part of the number list. Example runs:
$ perl -w 7.2.pl Please enter a number: 12 Please enter a number: 5 Please enter a number: 31 Please enter a number: 0 The largest number was 31 The smallest number was 5 $ perl -w 7.2.pl Please enter a number: 0 $
Write program that reads two numbers and an operation from the keyboard, and then shows the result of applying the operation to these numbers. The operations are add (+), subtract (-), multiply (*) and divide (/). The program should contain at least four subroutines: one for every operation. Each of the subroutines should take two numbers as parameters and return the result as as number. Example runs:
$ perl -w 7.3.pl Please enter the first number: 6 Please enter the second number: 7 Please enter the operation: - The difference between 6 and 7 is -1 $ perl -w 7.3.pl Please enter the first number: 12 Please enter the second number: 5 Please enter the operation: + The sum of 12 and 5 is 17 $
Write a program that reads strings consisting of a's and b's and successively performs five tests on these strings:
Use regular expression for performing the tests. The results of the tests should be shown by the program. The program should continue processing strings until the word "stop" is entered as string. Example run:
$ perl -w 7.4.pl Please enter a string: abaa Result of test 1: no Result of test 2: no Result of test 3: no Result of test 4: yes Result of test 5: yes Please enter a string: bbb Result of test 1: yes Result of test 2: yes Result of test 3: no Result of test 4: no Result of test 5: no Please enter a string: stop $
Write a program that reads a paragraph of text and shows the relative frequency of the characters in the text (a percentage for each character). Examine only the characters a-z and ignore other characters like numbers, punctuation signs and white space. Convert all upper case characters to lower case ($string = lc($string)) so that they can also be used in the counts. The program should stop reading text when an empty line is found. Apply the program to a paragraph of English text and to a paragraph in another language, and check if you can find interesting differences. Example run:
$ perl -w 7.5.pl Please enter a text below. Finish with an empty line: This is a test. t 27.3% s 27.3% i 18.2% h 9.1% e 9.1% a 9.1% $
Repeat exercise 7.5 but this time take the input text from a file which is specified in the parameter list of the program. This program should not only process the first paragraph but the complete file as one block of text. The required output format is the same as in the previous exercise. Example run:
$ perl -w 7.5.pl test.txt t 27.3% s 27.3% i 18.2% h 9.1% e 9.1% a 9.1% $
where the input file test.txt contains the line This is a test.