Starting from:

$25

COMP9044 - Week 06 - Weekly Test Questions - Solved

Set up for the test by creating a new directory called test06, changing to this directory, and fetching the provided code by running these commands:

$ mkdir test06 

$ cd test06 

$ 2041 fetch test06 
Or, if you're not working on CSE, you can download the provided code as a zip file or a tar file.

 

Write a Perl program uniq_echo.pl that prints its command-line argument to standard output, similar to echo command in Shell, except only the first occurrence of any argument should be printed, Repeat occurrences should not be printed.

$ ./uniq_echo.pl echo echo echo echo 

$ ./uniq_echo.pl bird cow bird cow fish bird cow fish bird bird cow fish 

$ ./uniq_echo.pl how much wood would a woodchuck chuck how much wood would a woodchuck chuck $ ./uniq_echo.pl d c b d c a a d d c b a

$ ./uniq_echo.pl 
Your answer must be Perl only. You can not use other languages such as Shell, Python or C.

You may not run external programs, e.g. via system or backquotes.
 

 

Write a Perl program which reads lines from its input until it reads an line that has been entered n times.

Your program should then print "Snap: " followed by the line.

Your program will be given n as a command line argument. Your program should print nothing if the end-of-input is reached before any line is repeated n times.

You can assume the lines are ASCII, you should not assume anything else about the lines.

Your answer must be Perl only. You can not use other languages such as Shell, Python or C.

You may not run external programs, e.g. via system or backquotes.

For example:

$ ./snap_n.pl 2 hi how are you hi 

Snap: hi 

$ ./snap_n.pl 2 hi hi hi hi hi hi hi hi hi hi hi hi 

Snap: hi hi 
$ ./snap_n.pl 4 

Hello World Line 2 

Hello World Line 3 

Hello World Line 4 

Hello World 

Snap: Hello World 
No error checking is necessary.

When you think your program is working you can autotest to run some simple automated tests:

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

$ give cs2041 test06_snap_n snap_n.pl 
 

 

Write a Perl program, sort_file_lines.pl which is given one argument, a file name.

Your program should print the lines of the file in order of length, shortest to longest.

Lines of equal length should be sorted alphabetically.

You can asusme the lines in the file contain ASCII. Yoy should not assume anything else about the lines in the file.

Your answer must be Perl only. You can not use other languages such as Shell, Python or C.

You may not run external programs, e.g. via system or backquotes.

No error checking is necessary.

For example:

$ cat file.txt tiny short line medium line longgggggg line a equal line b equal line c equal line even longggggggggggggggggggggggggggggggggggggggggggerr 

$ sort_file_lines.pl file.txt tiny short line medium line a equal line b equal line c equal line longgggggg line 

even longggggggggggggggggggggggggggggggggggggggggggerr 
When you think your program is working you can autotest to run some simple automated tests:

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

$ give cs2041 test06_sort_file_lines sort_file_lines.pl 
 


More products