Week 02 Tutorial Ans PDF

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 PDF
Total Downloads 104
Total Views 155

Summary

Download Week 02 Tutorial Ans PDF


Description

COMP[29]04116s2(http://www.cse.unsw.edu.au/~cs2041/16s2/)

FiltersandRegexps SoftwareConstruction(http://www.cse.unsw.edu.au/~cs2041/16s2/)

1.Whatisyourtutor'sname,email,howlonghavetheybeenatUNSW,whataretheystudying,whatis1interestingthing aboutthem?

Answeredintute.

2.Whatareyourclassmates'snames,whataretheyeachstudying,whatis1interestingthingabouteachofthem?

Answeredintute.

3.Aretheremarksforattendinglectures,tutorialsorlabs?

Noattendencemarksforlectures,tutorialsorlabs.

4.Whatisanoperatingsystem?Whatoperatingsystemsarerunninginyourtuteroom?WhatoperatingsystemdoCSElab computersrun?

Anoperatingsystemisapieceofsoftwarethatmanagesthehardwareofacomputerandprovidesaninterfacetothe programsthatrunonthecomputer.OperatingsystemsonphonesinyourtutroommightincludeLinux(Android),IOS (iPhone),Windows.CSE'slabcomputersandserversrunLinux.

5.

a.WritearegexptomatchCpreprocessorcommandsinaCprogram. b.WritearegexptomatchallthelinesinaCprogramexceptpreprocessorcommands c.WritearegexptofindlineinaCprogramwithtrailingwhitespaceoneorwhitespaceattheendofline d.WritearegexptomatchthenamesBarry,Harry,LarryandParry e.Writearegexptomatchastringcontainingtheword hello followedlaterbytheword world f.Writeregexptomatchtheword calendar andallmisspellingswith'a'replaced'e'orviceversa g.Writeregexptomatchalistofpositiveintegersseparatedbycommas,e.g. 2,4,8,16,32 h.WriteregexptomatchaCstringwhoselastcharacterisnewline

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.GivefivereasonswhythisattempttosearchafileforHTMLparagraphandbreaktagsmayfail.

$grep|/tmp/index.html  Giveegrepcommandsthatwillwork.

Thecharacters''and'|'arepartoftheshell'ssyntax(metacharacters)andtheshellwillinterpretthemrather thanpassingthemtogrep.Thiscanbeavoidedwithsingleordoublequotesorbackslash,e.g:

$egrep'|'/tmp/index.html  $egrep"|"/tmp/index.html  $egrep\\|/tmp/index.html  Forhistoricalreasons'grep'doesn'timplementalternation('|').Youneedtouse'egrep'or('grepE')insteadtogetthe fullpowerofregularexpressions. Thesuppliedregularexpressionwon'tmatchtheHTMLtagsiftheyareinuppercase(AZ),e.g:.Thematchcan becaseinsensitivebychangingtheregularexpressionorusinggrep'siflag

$egrep'|'/tmp/index.html  $egrepi'|'/tmp/index.html 

Thesuppliedregularexpressionalsowon'tmatchHTMLtagscontainingspaces,e.g:.Thiscanberemediedby changingtheregularexpressionorusinggrep'swflag.

$egrepi''/tmp/index.html  $egrepiw'|'/tmp/index.html  TheHTMLtagmaycontainattributes,e.g:.Againcanberemediedbychangingtheregular expressionorusinggrep'swflag.

$egrepi']*>'/tmp/index.html  Thiswillstillmatch.AbettersolutionproposedbyMichaelManansalais:

$egrepi']*)*>'/tmp/index.html  TheHTMLtagmaycontainanewline.Thisismoredifficulttohandlewithanessentiallylinebasedtoollikegrep.

7.Foreachoftheregularexpressionbelowindicatehowmanydifferentstringsthepatternmatchesandgivesomeexampleof thestringsitmatches.Ifpossibletheseexampleshouldincludetheshorteststringandthelongeststring.

Perl Pe*r*l Fullstop. [19][09][09][09] I(love|hate)programmingin(Perl|Python)and(Java|C) Regexp

NMatches

ShortestMatch

LongestMatch

OtherExamples

Perl

1

Perl

Perl

Perl

Arbitrary

Pl

Pe*r*l Fullstop. [19][09][09][09] I(love|hate)programmingin (Perl|Python)and(Java|C)

sameasnumberof charactersincharacterset 9000

constantlength

constantlength

constantlength

Ilove 8

PelPrlPerl

constantlength

programmingin PerlandC

FullstopaFull stopbFullstopc 100010011002

Iloveprogrammingin PythonandJava

8.Thisregularexpression[09]*.[09]*isintendedtomatchfloatingpointnumberssuchas'42.5'.Isitappropriate?

No.Theregularexpression[09]*.[09]*matchesstringswhicharenotfloatingpointnumbers.Itwillmatchzeroor moredigits,anycharacter,followedbyzeroormoredigits.Italsowillmatchnumberssuchas01.12 Betterwouldbe[19][09]*\.[09]+

9.Whatdoesthecommand egrep -v . printandwhy? Giveanequivalentegrepcommandwithnooptions,inotherwordswithoutthevandwithadifferentpattern.

Thepattern'.'matchesanycharacter. Theoptionvcausesegreptoprintlineswhichdon'tmatchthepattern Sothecommand egrep -v . printsalltheemptylinesinitsinput. Thecommand egrep '^$' wouldalsodothis.

10.Writeanegrepcommandwhichwillprintanylinesinafile ips.txt containinganIPaddressesintherange129.94.172.1to 129.94.172.25

$egrep'129\.94\.172\.([19]|1[09]|2[05])$'ips.txt  Or,moregenerally

$egrep'\b129\.94\.172\.([19]|1[09]|2[05])\b'ips.txt 

11.Foreachofthescenariosbelow describethestringsbeingmatchedusinganEnglishsentence giveaPOSIXregularexpressiontomatchthisclassofstrings Intheexamples,theexpectedmatchesarehighlightedinbold. a.encryptedpasswordfields(includingthesurroundingcolons)inaUnixpasswordfileentry,e.g. root:ZHolHAHZw8As2:0:0:root:/root:/bin/bash jas:nJz3ru5a/44Ko:100:100:John Shepherd:/home/jas:/bin/bash

:[^:]+:

Sinceencryptedpasswordscancontainjustaboutanycharacter(exceptcolon)youcouldstructurethepattern as"findacolon,followedbyasequenceofnoncolons,terminatedbyacolon".Notethatthispatternactually matchesallofthefieldsinthelineexceptthefirstandlast,butifweassumethatweonlylookforthefirstmatch oneachline,itwilldo.

b.positiverealnumbersatthestartofaline(usingnormalfixedpointnotationforreals,notthekindofscientific notationyoufindinprogramminglanguages),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]*)?

Thispatternassumesthatrealnumberswillconsistofasequenceofdigits(theintegerpart)optionallyfollowed byadecimalpointwiththefractiondigitsafterthedecimalpoint.Notetheuseofthe ^ symboltoanchorthe patterntothestartoftheline,the + toensurethatthereisatleastonedigitintheintegerpart,the \ to escapethespecialmeaningof . ,andthe ? tomakethefractionalpartoptional.

c.Namesasrepresentedinthisfilecontainingdetailsoftute/labenrolments: 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|

[^|,]+, [^|]+

Topickoutthecontentwithoutthedelimiters,thefirstpartofthenameisanystringwithoutacommaorbar, thenthecommaandspace,andtheneverythinguptothenextdelimiter.Bothpartsofthenamearenonempty, hence + isusedratherthan * .

d.Namesasabove,butwithoutthetrailingspaces(difficult).Hint:whataregivennamescomposedof,andhowmanyof thesethingscantherebe?

[^|,]+,( [^| ]+)+

Wecouldn'tjustsay [^| ]+ ,becausethatwoulddisallowspacesinsidethegivennames.Foraspacetobe accepted,ithastobefollowedbyanonspace(usuallyaletter).Hencethegivennameportionisoneormore sequencesofW,whereWisaspacefollowedbynonspacesandnonbars. Whenaregularexpressionstartstolooklikestupidsmileyicons,youknowit'scomplex.

12.Considerthefollowingcolumnated(spacedelimited)datafilecontainingmarksinformationforasinglesubject:

2111321 2166258 2168678 2186565 2190546 2210109 2223455 2266365 ...

37 67 84 77 78 50 95 55

FL CR DN DN DN PS HD PS

Assumethatthestudentnumberoccursatthebeginningoftheline,thatthefileissortedonstudentnumber,andthat nobodyscores100. a.Givecallstothe sort filtertodisplaythedata: i.inorderonstudentnumber

$sort...


Similar Free PDFs