Starting from:

$25

COMP9044 - Week 10 -  Weekly Test Questions - Solved

Don't Repeat Yourself
Write a program remove_repeats.pl which takes 0 or more arguments and prints some of those arguments.

The first occurrence and only the first occurrence of any argument should be printed.

The arguments should be printed on a single line. A single space should separate each argument.

Your program can NOT assume the arguments with be in any particular order.

Make your program behave exactly as indicated by the examples below.

It must produce exactly the same output as below, except you may print an extra space at the end of the line if you wish.

Your program must be Perl.

For example:

$ remove_repeats.pl  

$ remove_repeats.pl bird bird 

$ remove_repeats.pl bird cow fish bird cow fish 

$ remove_repeats.pl echo echo echo echo 

$ remove_repeats.pl bird cow fish bird cow fish bird bird cow fish 

$ remove_repeats.pl how much wood would a woodchuck chuck how much wood would a woodchuck chuck $ remove_repeats.pl a a a a b a a b 

$ remove_repeats.pl a b c d c b a a b c d

$ remove_repeats.pl d c b d c a a d d c b a
When you think your program is working you can autotest to run some simple automated tests:

$ 2041 autotest remove_repeats 
When you are finished working on this exercise you must submit your work by running give:

$ give cs2041 test10_remove_repeats remove_repeats.pl 
 

 

Write a program text_round.plthat copies its standard input to standard output but maps all numbers to their nearest whole number equivalent. For example, 0.667 would be mapped to 1, 99.5 would be mapped to 100, 16.35 would be mapped to 16, and so on. All other text in the input should be transferred to the output unchanged.

A number is defined as a string containing some digit characters with an optional decimal point ('.') followed by zero or more additional digit characters.

For example 0, 100, 3.14159, 1000.0, 0.999 and 12345. are all valid numbers.

For example, given this input:

I spent $15.50 for 3.3kg of apples yesterday. 

Pi is approximately 3.141592653589793 2000 is a leap year, 2001 is not. 
your program should produce this output:

I spent $16 for 3kg of apples yesterday. 

Pi is approximately 3

2000 is a leap year, 2001 is not. 
For example:

$ cat text_round_input.txt 

I spent $15.50 for 3.3kg of apples yesterday. 

Pi is approximately 3.141592653589793 2000 is a leap year, 2001 is not. 

$ text_round.pl <text_round_input.txt 

I spent $16 for 3kg of apples yesterday. 

Pi is approximately 3

2000 is a leap year, 2001 is not. 
When you think your program is working you can autotest to run some simple automated tests:

$ 2041 autotest text_round 
When you are finished working on this exercise you must submit your work by running give:

$ give cs2041 test10_text_round text_round.pl 
 

 

Write a program reference.pl that reads lines of text from its standard input and prints them to its standard output. Except for lines which contain with a '#' character followed by a positive integer.

Lines of the form #n (where n is an integer value), should be replaced this by the n'th line of input.

This transformation only applies to lines which start with a # character, followed by the digits of a positive integer and then the newline character. No other characters appear on such lines.

You may assume:

Lines are numbered starting from 1,

There are no more than 100 lines in the input,

No line is more than 80 characters long, All n values are valid input line numbers, No n values refer to other #n lines.

For example:

$ cat reference_input.txt line A line B line C #7 line D #2 line E 

$ reference.pl <reference_input.txt line A line B line C line E line D line B line E 
When you think your program is working you can autotest to run some simple automated tests:

$ 2041 autotest reference 
When you are finished working on this exercise you must submit your work by running give:

$ give cs2041 test10_reference reference.pl 
 


More products