Title | Week 02 Tutorial Ans |
---|---|
Author | Wang Shan |
Course | Software Construction: Techniques and Tools |
Institution | University of New South Wales |
Pages | 5 |
File Size | 214 KB |
File Type | |
Total Downloads | 104 |
Total Views | 155 |
Download Week 02 Tutorial Ans PDF
COMP[29]04116s2(http://www.cse.unsw.edu.au/~cs2041/16s2/)
FiltersandRegexps SoftwareConstruction(http://www.cse.unsw.edu.au/~cs2041/16s2/)
1.Whatisyourtutor'sname,email,howlonghavetheybeenatUNSW,whataretheystudying,whatis1interestingthing aboutthem?
Answeredintute.
2.Whatareyourclassmates'snames,whataretheyeachstudying,whatis1interestingthingabouteachofthem?
Answeredintute.
3.Aretheremarksforattendinglectures,tutorialsorlabs?
Noattendencemarksforlectures,tutorialsorlabs.
4.Whatisanoperatingsystem?Whatoperatingsystemsarerunninginyourtuteroom?WhatoperatingsystemdoCSElab computersrun?
Anoperatingsystemisapieceofsoftwarethatmanagesthehardwareofacomputerandprovidesaninterfacetothe programsthatrunonthecomputer.OperatingsystemsonphonesinyourtutroommightincludeLinux(Android),IOS (iPhone),Windows.CSE'slabcomputersandserversrunLinux.
5.
a.WritearegexptomatchCpreprocessorcommandsinaCprogram. b.WritearegexptomatchallthelinesinaCprogramexceptpreprocessorcommands c.WritearegexptofindlineinaCprogramwithtrailingwhitespaceoneorwhitespaceattheendofline d.WritearegexptomatchthenamesBarry,Harry,LarryandParry e.Writearegexptomatchastringcontainingtheword hello followedlaterbytheword world f.Writeregexptomatchtheword calendar andallmisspellingswith'a'replaced'e'orviceversa g.Writeregexptomatchalistofpositiveintegersseparatedbycommas,e.g. 2,4,8,16,32 h.WriteregexptomatchaCstringwhoselastcharacterisnewline
a. ^# b. ^[^#] c. \s$ d. [BHLP]arry or Barry|Harry|Larry|Parry e. hello.*world f. c[ae]l[ae]nd[ae]r g. [0-9][0-9]*(,[0-9][0-9]*) or [0-9]+(,[0-9]+)* h. "[^"]*\\n"
6.GivefivereasonswhythisattempttosearchafileforHTMLparagraphandbreaktagsmayfail.
$grep|/tmp/index.html Giveegrepcommandsthatwillwork.
Thecharacters''and'|'arepartoftheshell'ssyntax(metacharacters)andtheshellwillinterpretthemrather thanpassingthemtogrep.Thiscanbeavoidedwithsingleordoublequotesorbackslash,e.g:
$egrep'|'/tmp/index.html $egrep"|"/tmp/index.html $egrep\\|/tmp/index.html Forhistoricalreasons'grep'doesn'timplementalternation('|').Youneedtouse'egrep'or('grepE')insteadtogetthe fullpowerofregularexpressions. Thesuppliedregularexpressionwon'tmatchtheHTMLtagsiftheyareinuppercase(AZ),e.g:.Thematchcan becaseinsensitivebychangingtheregularexpressionorusinggrep'siflag
$egrep'|'/tmp/index.html $egrepi'|'/tmp/index.html
Thesuppliedregularexpressionalsowon'tmatchHTMLtagscontainingspaces,e.g:.Thiscanberemediedby changingtheregularexpressionorusinggrep'swflag.
$egrepi''/tmp/index.html $egrepiw'|'/tmp/index.html TheHTMLtagmaycontainattributes,e.g:.Againcanberemediedbychangingtheregular expressionorusinggrep'swflag.
$egrepi']*>'/tmp/index.html Thiswillstillmatch.AbettersolutionproposedbyMichaelManansalais:
$egrepi']*)*>'/tmp/index.html TheHTMLtagmaycontainanewline.Thisismoredifficulttohandlewithanessentiallylinebasedtoollikegrep.
7.Foreachoftheregularexpressionbelowindicatehowmanydifferentstringsthepatternmatchesandgivesomeexampleof thestringsitmatches.Ifpossibletheseexampleshouldincludetheshorteststringandthelongeststring.
Perl Pe*r*l Fullstop. [19][09][09][09] I(love|hate)programmingin(Perl|Python)and(Java|C) Regexp
NMatches
ShortestMatch
LongestMatch
OtherExamples
Perl
1
Perl
Perl
Perl
Arbitrary
Pl
Pe*r*l Fullstop. [19][09][09][09] I(love|hate)programmingin (Perl|Python)and(Java|C)
sameasnumberof charactersincharacterset 9000
constantlength
constantlength
constantlength
Ilove 8
PelPrlPerl
constantlength
programmingin PerlandC
FullstopaFull stopbFullstopc 100010011002
Iloveprogrammingin PythonandJava
8.Thisregularexpression[09]*.[09]*isintendedtomatchfloatingpointnumberssuchas'42.5'.Isitappropriate?
No.Theregularexpression[09]*.[09]*matchesstringswhicharenotfloatingpointnumbers.Itwillmatchzeroor moredigits,anycharacter,followedbyzeroormoredigits.Italsowillmatchnumberssuchas01.12 Betterwouldbe[19][09]*\.[09]+
9.Whatdoesthecommand egrep -v . printandwhy? Giveanequivalentegrepcommandwithnooptions,inotherwordswithoutthevandwithadifferentpattern.
Thepattern'.'matchesanycharacter. Theoptionvcausesegreptoprintlineswhichdon'tmatchthepattern Sothecommand egrep -v . printsalltheemptylinesinitsinput. Thecommand egrep '^$' wouldalsodothis.
10.Writeanegrepcommandwhichwillprintanylinesinafile ips.txt containinganIPaddressesintherange129.94.172.1to 129.94.172.25
$egrep'129\.94\.172\.([19]|1[09]|2[05])$'ips.txt Or,moregenerally
$egrep'\b129\.94\.172\.([19]|1[09]|2[05])\b'ips.txt
11.Foreachofthescenariosbelow describethestringsbeingmatchedusinganEnglishsentence giveaPOSIXregularexpressiontomatchthisclassofstrings Intheexamples,theexpectedmatchesarehighlightedinbold. a.encryptedpasswordfields(includingthesurroundingcolons)inaUnixpasswordfileentry,e.g. root:ZHolHAHZw8As2:0:0:root:/root:/bin/bash jas:nJz3ru5a/44Ko:100:100:John Shepherd:/home/jas:/bin/bash
:[^:]+:
Sinceencryptedpasswordscancontainjustaboutanycharacter(exceptcolon)youcouldstructurethepattern as"findacolon,followedbyasequenceofnoncolons,terminatedbyacolon".Notethatthispatternactually matchesallofthefieldsinthelineexceptthefirstandlast,butifweassumethatweonlylookforthefirstmatch oneachline,itwilldo.
b.positiverealnumbersatthestartofaline(usingnormalfixedpointnotationforreals,notthekindofscientific notationyoufindinprogramminglanguages),e.g. 3.141 value of Pi 90.57 maximum hits/sec half of the time, life is silly 0.05% is the legal limit 42 - the meaning of life this 1.333 is not at the start
^[0-9]+(\.[0-9]*)?
Thispatternassumesthatrealnumberswillconsistofasequenceofdigits(theintegerpart)optionallyfollowed byadecimalpointwiththefractiondigitsafterthedecimalpoint.Notetheuseofthe ^ symboltoanchorthe patterntothestartoftheline,the + toensurethatthereisatleastonedigitintheintegerpart,the \ to escapethespecialmeaningof . ,andthe ? tomakethefractionalpartoptional.
c.Namesasrepresentedinthisfilecontainingdetailsoftute/labenrolments: 2134389|Wang, Duved Seo Ken 2139656|Undirwaad, Giaffriy Jumis 2154877|Ng, Hinry 2174328|Zhung, Yung 2234136|Hso, Men-Tsun 2254148|Khorme, Saneu 2329667|Mahsin, Zumel 2334348|Trun, Toyin Hong Recky
|fri15-spoons| |tue13-kazoo| |tue17-kazoo| |thu17-spoons| |tue09-harp| |tue09-harp| |tue17-kazoo| |mon11-leaf|
2336212|Sopuvunechyunant, Sopuchue 2344749|Chung, Wue Sun ...
|mon11-leaf| |fri09-harp|
[^|,]+, [^|]+
Topickoutthecontentwithoutthedelimiters,thefirstpartofthenameisanystringwithoutacommaorbar, thenthecommaandspace,andtheneverythinguptothenextdelimiter.Bothpartsofthenamearenonempty, hence + isusedratherthan * .
d.Namesasabove,butwithoutthetrailingspaces(difficult).Hint:whataregivennamescomposedof,andhowmanyof thesethingscantherebe?
[^|,]+,( [^| ]+)+
Wecouldn'tjustsay [^| ]+ ,becausethatwoulddisallowspacesinsidethegivennames.Foraspacetobe accepted,ithastobefollowedbyanonspace(usuallyaletter).Hencethegivennameportionisoneormore sequencesofW,whereWisaspacefollowedbynonspacesandnonbars. Whenaregularexpressionstartstolooklikestupidsmileyicons,youknowit'scomplex.
12.Considerthefollowingcolumnated(spacedelimited)datafilecontainingmarksinformationforasinglesubject:
2111321 2166258 2168678 2186565 2190546 2210109 2223455 2266365 ...
37 67 84 77 78 50 95 55
FL CR DN DN DN PS HD PS
Assumethatthestudentnumberoccursatthebeginningoftheline,thatthefileissortedonstudentnumber,andthat nobodyscores100. a.Givecallstothe sort filtertodisplaythedata: i.inorderonstudentnumber
$sort...