Starting from:

$25

COMP9044 - Week 05 - Weekly Test Questions - Solved

Sort Words Line by Line
Write a Perl program sort_words.pl that reads lines of text from its standard input and prints them to its standard output with the words on each line rearranged to be in sorted (alphabetic) order.

You can assume that a word is any sequence of non-whitespace characters.

You should print the words separated by a single space character.

For example:

$ ./sort_words.pl 

I shall be telling this with a sigh

Somewhere     ages and ages hence 

Two roads diverged in a   wood and I I took   the one   less traveled by

And that has made all the difference 

 Ctrl-D                           

I a be shall sigh telling this with Somewhere ages ages and hence 

I Two a and diverged in roads wood 

I by less one the took traveled 

And all difference has made that the 
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.
pl 
 

 

Write a Perl program replace_digits.pl that take a single argument a filename.

It should replace all digits (0-9 characters) in the files with the character '#'.

Your program should not should produce any output.

Your program should only change the file. For example

$ cat file.txt 

I can think of 100's, no 1000,000s of other things I'd rather be doing than these 3 questions. 

$ ./replace_digits.pl file.txt 

$ cat file.txt 

I can think of ###'s, no ####,###s of other things I'd rather be doing than these # questions. 
Hint: don't try read and writing the file at the same time - it won't work - read all all the file, then write 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.

You can assume the file contains ASCII and is less than 1 megabyte.

No error checking is necessary.

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

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

$ give cs2041 test05_replace_digits replace_digits.pl 
 

 

Write a Perl program, identical_files.pl which takes 2 or more filenames are argument.

Your program should then check if each file has exactly the same contents ad the other files given as arguments.

If all files are identical (the same), it should print exactly the message in the example below.

Otherwise it should print a message indicating the first file which is different to a previous file on the command line. Again print exactly the message as in the example below.

For example if create some files for testing:

 

Then this is how identical_files.pl should behave:

$ ./identical_files.pl five.txt 5.txt 

All files are identical 

$ ./identical_files.pl fortytwo.txt 42.txt 42a.txt 

All files are identical 

$ ./identical_files.pl 5.txt 42.txt 42a.txt five.text 

42.txt is not identical 

$ ./identical_files.pl 

Usage: ./identical_files.pl <files>
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.

You can assume files contain ASCII and are less than 1 megabyte.


More products