COMP2041 20T2 - Week 01 Laboratory Exercises PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7
At a glance
Powered by AI
The key takeaways are understanding regular expressions, using UNIX filters, and completing various exercises related to egrep-ing files and dictionaries.

The objectives of the laboratory exercises are understanding regular expressions and the use of UNIX filters.

Before the lab, students should re-read the relevant lecture slides and examples. After completing an exercise, students can use the autotest to check their answers and submit their work using the give command.

04/06/2020 COMP2041 20T2 — Week 01 Laboratory Exercises

Week 01 Laboratory Exercises


Objectives
Understanding regular expressions
Understanding use of UNIX filters

Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.

Getting Started
Create a new directory for this lab called lab01, change to this directory, and fetch the provided code for this week by running these
commands:
$ mkdir lab01
$ cd lab01
$ 2041 fetch lab01

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

:
egrep-ing a Dictionary with egrep
There is a template file named dictionary_answers.txt which you must use to enter the answers for this exercise.
Download dictionary_answers.txt, or copy it to your CSE account using the following command:
$ cp -n /web/cs2041/20T2/activities/dictionary/dictionary_answers.txt .

The autotest scripts depend on the format of dictionary_answers.txt so just add your answers don't otherwise change the file. In
other words edit dictionary_answers.txt:
gedit dictionary_answers.txt &

On most Unix systems you will find one or more dictionaries containing many thousands of words typically in the directories
/usr/share/dict/.

We've created a dictionary named dictionary.txt for this lab exercise.


Download dictionary.txt, or copy it to your CSE account using the following command:
$ cp -n /web/cs2041/20T2/activities/dictionary/dictionary.txt .

$ ls -l

total 4
lrwxrwxrwx 1 cs2041 cs2041 17 May 26 10:36 dictionary.txt -> ../dictionary.txt
-rw-r--r-- 1 cs2041 cs2041 1072 May 26 10:36 dictionary_answers.txt

. Write an egrep command that prints the words in dictionary.txt which contain in characters "lmn" consecutively.
Hint: it should print:
https://cgi.cse.unsw.edu.au/~cs2041/20T2/lab/01/questions 1/7
04/06/2020 COMP2041 20T2 — Week 01 Laboratory Exercises

Selmner
Selmner's
almner
almners
calmness
calmness's
calmnesses

The COMP2041 class account contains a script named autotest that automatically runs 1 or more tests on your lab exercises.
Once you have entered you answer for q1 you can check it like this:
2041 autotest dictionary q1
Test q1 (egrep '^Q1 answer' dictionary_answers.txt|tail -1|sed 's/.*answer[: ]*//'|sh) - passed
1 tests passed 0 tests failed

Passing the autotest doesn't guarantee your answer is correct, of course, but it may find a mistake you've missed so run
autotest for each of the following questions when you've entered the answer in dictionary_answers.txt.
. Write a shell pipeline that prints the words that contain "zz", but do not end in apostrophe-s ('s)?
Hint: it should print:
Abruzzi
Arezzini
Arezzo
Barozzi
Belshazzar
Brazzaville
Buzz
. Write an egrep command that prints the words that contain four consecutive vowels?
Hint: it should print these words:
Aiea
Aiea's
Araguaia
Araguaia's
Douai
Douai's
Graeae
. Write an egrep command that prints words which contain all 5 vowels "aeiou" in that order?
The words may contain more than 5 vowels but they must contain "aeiou" in that order.
Hint: it should print these words:
abstemious
abstemiously
abstemiousness
abstemiousness's
abstemiousnesses
abstentious
adenocarcinomatous
. Write an egrep command that prints which contain the vowels "aeiou" in that order and no other vowels.
Hint: it should print these words:
abstemious
abstemiously
abstentious
arsenious
caesious
facetious
facetiously

When you think your program is working, you can use autotest to run some simple automated tests:
$ 2041 autotest dictionary

Autotest Results
76% of 311 students who have autotested dictionary_answers.txt so far, passed all autotest tests.
98% passed test Ẃᾉ
95% passed test Ẃᾊ
https://cgi.cse.unsw.edu.au/~cs2041/20T2/lab/01/questions 2/7
04/06/2020 COMP2041 20T2 — Week 01 Laboratory Exercises

96% passed test Ẃᾋ


95% passed test Ẃᾌ
93% passed test Ẃᾎ
When you are finished working on this exercise, you must submit your work by running give:
$ give cs2041 lab01_dictionary dictionary_answers.txt

before Tuesday 09 June 11 59 to obtain the marks for this lab exercise.

:
egrep-ing Federal Parliament
There is a template file named parliament_answers.txt which you must use to enter the answers for this exercise.
Download parliament_answers.txt, or copy it to your CSE account using the following command:
$ cp -n /web/cs2041/20T2/activities/parliament/parliament_answers.txt .

The autotest scripts depend on the format of parliament_answers.txt so just add your answers don't otherwise change the file.
In this exercise you will analyze a file named parliament.txt containing a list of the members of the Australian House of
Representatives (MPs).
Download parliament.txt, or copy it to your CSE account using the following command:
$ cp -n /web/cs2041/20T2/activities/parliament/parliament.txt .

. Write an egrep command that will print all the lines in the file where the electorate begins with W.
Hint: it should print these lines:
Hon Scott Buchholz: Member for Wright, Queensland
Hon Tony Burke: Member for Watson, New South Wales
Mr Nick Champion: Member for Wakefield, South Australia
Mr Stephen Jones: Member for Whitlam, New South Wales
Mr Peter Khalil: Member for Wills, Victoria
Mr Llew O'Brien: Member for Wide Bay, Queensland
Dr Kerryn Phelps AM: Member for Wentworth, New South Wales
Ms Anne Stanley: Member for Werriwa, New South Wales
Ms Zali Steggall OAM: Member for Warringah, New South Wales
Hon Dan Tehan: Member for Wannon, Victoria

. Write an egrep command that will list all the lines in the file where the MP's first name is Andrew.
Hint: it should print these words:
Hon Andrew Gee: Member for Calare, New South Wales
Mr Andrew Giles: Member for Scullin, Victoria
Mr Andrew Hastie: Member for Canning, Western Australia
Mr Andrew Laming: Member for Bowman, Queensland
Hon Dr Andrew Leigh: Member for Fenner, Australian Capital Territory
Mr Andrew Wallace: Member for Fisher, Queensland
Mr Andrew Wilkie: Member for Denison, Tasmania

. Write an egrep command that will print all the lines in the file where the MP's surname (last name) ends in the letters 'll'.
Hint: it should print these words:
Mr Julian Hill: Member for Bruce, Victoria
Mr Brian Mitchell: Member for Lyons, Tasmania
Mr Rob Mitchell: Member for McEwen, Victoria
Ms Zali Steggall OAM: Member for Warringah, New South Wales

. Write an egrep command that will print all the lines in the file where the MP's name and the electorate ends in the letter 'y'.
Hint: it should print these lines:

https://cgi.cse.unsw.edu.au/~cs2041/20T2/lab/01/questions 3/7
04/06/2020 COMP2041 20T2 — Week 01 Laboratory Exercises

Mr Rowan Ramsey: Member for Grey, South Australia

. Write an egrep command that will print all the lines in the file where the MP's name or the electorate ends in the letter 'y'.
Hint: it should print these lines:
Dr Anne Aly: Member for Cowan, Western Australia
Hon Linda Burney: Member for Barton, New South Wales
Mr Pat Conroy: Member for Shortland, New South Wales
Mr Chris Crewther: Member for Dunkley, Victoria
Mr Milton Dick: Member for Oxley, Queensland
Hon Damian Drum: Member for Murray, Victoria
Ms Nicolle Flint: Member for Boothby, South Australia
Hon Ed Husic: Member for Chifley, New South Wales
Hon Bob Katter: Member for Kennedy, Queensland
Ms Ged Kearney: Member for Batman, Victoria
Mr Craig Kelly: Member for Hughes, New South Wales
Hon Dr Mike Kelly AM: Member for Eden-Monaro, New South Wales
Hon Michelle Landry: Member for Capricornia, Queensland
Hon Sussan Ley: Member for Farrer, New South Wales
Mrs Melissa McIntosh: Member for Lindsay, New South Wales
Hon Ben Morton: Member for Tangney, Western Australia
Mr Llew O'Brien: Member for Wide Bay, Queensland
Hon Tanya Plibersek: Member for Sydney, New South Wales
Mr Rowan Ramsey: Member for Grey, South Australia
Ms Michelle Rowland: Member for Greenway, New South Wales

. Write an egrep command to print all the lines in the file where there is any word in the MP's name or the electorate name that
ends in ng.
Hint: it should print these lines:
Mr John Alexander OAM: Member for Bennelong, New South Wales
Hon Josh Frydenberg: Member for Kooyong, Victoria
Mr Luke Gosling OAM: Member for Solomon, Northern Territory
Mr Andrew Hastie: Member for Canning, Western Australia
Hon Catherine King: Member for Ballarat, Victoria
Ms Madeleine King: Member for Brand, Western Australia
Mr Andrew Laming: Member for Bowman, Queensland
Hon Bill Shorten: Member for Maribyrnong, Victoria
Mr Terry Young: Member for Longman, Queensland

. Write an egrep command that will print all the lines in the file where the MP's surname (last name) both begins and ends with a
vowel.
Hint: it should print these lines:
Hon Anthony Albanese: Member for Grayndler, New South Wales
Ms Cathy O'Toole: Member for Herbert, Queensland

. Most electorate have names that are a single word, e.g. Warringah, Lyons & Grayndler. A few electorates have multiple word
names, for example, Kingsford Smith. Write an egrep command that will print all the lines in the file where the electorate name
contains multiple words (separated by spaces or hyphens).
Hint: it should print these lines:
Hon Mark Butler: Member for Port Adelaide, South Australia
Hon Barnaby Joyce: Member for New England, New South Wales
Hon Dr Mike Kelly AM: Member for Eden-Monaro, New South Wales
Mr Llew O'Brien: Member for Wide Bay, Queensland
Hon Matt Thistlethwaite: Member for Kingsford Smith, New South Wales
Hon Jason Wood: Member for La Trobe, Victoria
Mr Trent Zimmerman: Member for North Sydney, New South Wales

. Write a shell pipeline which prints the 8 Australians states & territory in order of the number of MPs they have. It should print
only the number of MPs, followed by the name of the states/territories. It should print them from fewest to most MPs.
Hint: check out the Unix filters cut, sort, uniq in the lecture notes.
Hint: it should print these lines:

https://cgi.cse.unsw.edu.au/~cs2041/20T2/lab/01/questions 4/7
04/06/2020 COMP2041 20T2 — Week 01 Laboratory Exercises

1 Australian Capital Territory


2 Northern Territory
5 Tasmania
9 South Australia
15 Western Australia
27 Queensland
33 Victoria
45 New South Wales

. Challenge: The most common first name for an MP is Andrew. Write a shell pipeline which prints the 2nd most common MP first
name. It should print this first name and only this first name.
Hint: check out the Unix filters cut, sort, sed, head, tail & uniq in the lecture notes.
Hint: it should print this line:
Tony

When you think your program is working, you can use autotest to run some simple automated tests:
$ 2041 autotest parliament

Autotest Results
51% of 205 students who have autotested parliament_answers.txt so far, passed all autotest tests.
98% passed test Ẃᾉ
66% passed test Ẃᾉᾇ
95% passed test Ẃᾊ
94% passed test ẂᾋẂᾌ
88% passed test Ẃᾎ
91% passed test Ẃᾏ
90% passed test Ẃᾑ Ẃᾒ
81% passed test Ẃᾓ
When you are finished working on this exercise, you must submit your work by running give:
$ give cs2041 lab01_parliament parliament_answers.txt

before Tuesday 09 June 11 59 to obtain the marks for this lab exercise.

:
Exploring Regular Expressions
There is a template file named ab_answers.txt which you must use to enter the answers for this exercise.
Download ab_answers.txt, or copy it to your CSE account using the following command:
$ cp -n /web/cs2041/20T2/activities/ab/ab_answers.txt .

Use egrep to test your answers to these questions.


Try to solve these questions using the standard regular expression language described in lectures.
. Write a egrep command that prints the lines in a file named input.txt containing containing at least one A and at least one B.
For example:
Matching Not Matching
Andrew's favourite Band is not George is Brillant
ABBA Andrew
BA B
AB A
https://cgi.cse.unsw.edu.au/~cs2041/20T2/lab/01/questions 5/7
04/06/2020 COMP2041 20T2 — Week 01 Laboratory Exercises

So to test with egrep you might do this:


cat >input.txt <<eof
Andrew's favourite Band is not
George is Brillant
ABBA
Andrew
AB
BA
A
B
eof
egrep 'REGEXP' input.txt
Andrew's favourite Band is not
ABBA
AB
BA

. Write a egrep command that prints the lines in a file named input.txt containing only the characters A and B such that all
pairs of adjacent A's occur before any pairs of adjacent B's. In other words if there is pair of B's on the line , there can not be a
pair of A's afterwards.
Matching Not Matching
ABAABAABAABBBBABB BBAA
ABBA ABBAA
ABAAAAAAAAAABBA ABBABABABABAA
ABABABABA ABBBAAA
A BBABABABABABABAA
. Write a egrep command that prints the lines in a file named input.txt containing only the characters A and B such that the
number of A's is divisible by 4.
Matching Not Matching
AAAA AAAAA
BABABABAB ABABBBBBBBBBBBBBBBAAA
AAAABBBBAAAA AAAABBABBAAAA
BBBAABBBBBAABBBAAAA BBBAABBABBBAABBBAAAA
. Write a egrep command that prints the lines in a file named input.txt containing only the characters A and B such that there
are exactly Ṏ A's followed by exactly Ṏ B's and no other characters.
Matching Not Matching
AAABBB AAABB
AB BA
AABB AABBB
AAAABBBB AAAABBBBA
When you think your program is working, you can use autotest to run some simple automated tests:
$ 2041 autotest ab

Autotest Results
https://cgi.cse.unsw.edu.au/~cs2041/20T2/lab/01/questions 6/7
04/06/2020 COMP2041 20T2 — Week 01 Laboratory Exercises

32% of 68 students who have autotested ab_answers.txt so far, passed all autotest tests.
99% passed test Ẃᾉ
77% passed test Ẃᾊ
63% passed test Ẃᾋ
39% passed test Ẃᾌ
When you are finished working on this exercise, you must submit your work by running give:
$ give cs2041 lab01_ab ab_answers.txt

before Tuesday 09 June 11 59 to obtain the marks for this lab exercise.

Submission
When you are finished each exercises make sure you submit your work by running give.
You can run give multiple times. Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Tuesday 09 June 11 59 59 to submit your work.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
You check the files you have submitted here.
Automarking will be run by the lecturer several days after the submission deadline, using test cases different to those autotest runs
for you. (Hint: do your own testing as well as runningautotest.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web
interface.
Lab Marks
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this
command on a CSE machine:
$ 2041 classrun -sturec

COMP(2041|9044) 20T2: Software Construction is brought to you by


the School of Computer Science and Engineering
at the University of New South Wales, Sydney.
For all enquiries, please email the class account at cs2041@cse.unsw.edu.au
CRICOS Provider 00098G

https://cgi.cse.unsw.edu.au/~cs2041/20T2/lab/01/questions 7/7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy