Class Prep - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Class Prep

Description:

upper(str) to convert all letters to UPPER CASE. lower(str) to convert to ... the characters on the alphabet to remain within the set of printable letters. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 26
Provided by: davidh67
Category:
Tags: class | prep

less

Transcript and Presenter's Notes

Title: Class Prep


1
Class Prep
  • Bring to Class
  • In-Class Exercise
  • Paper for Classroom Printer
  • Copy sample programs from course folder
  • Run MATLAB

2
Alternate HW 4d Solution
  • I wanted you to write this program using a for
    loop inside the while loop, but notice that the
    problem can also be done using the more efficient
    "implicit" MATLAB loop mechanism
  • HW 4d by Dave Hannay
  • while true
  • count input('Number of random points? ')
  • if count lt 0
  • break
  • end if
  • pts rand(count, 2) two columns (x,y)
  • hits sum(sqrt(pts(,1).2 pts(,2).2) lt
    1)
  • myPI 4 hits / count
  • end while

3
Get Sample Programs
  • Copy 109Week05 sub-folder from 109 course folder
    to H\csc109
  • If not already there
  • Run MATLAB and switch to your sub-folder as your
    Current Directory

4
Week 05-c(not in textbook)
  • strrep
  • Regular Expressions

5
Hands-On DEMO String Manipulation strrep(not in
textbook)
  • S strrep(S1, S2, S3) replaces all occurrences
    of the string S2 in S1 with the string S3. The
    new string is returned.
  • gtgt start 'This is a good example'
  • gtgt new1 strrep(start, 'good', 'great')
  • new1 This is a great example
  • gtgt new2 strrep(start, ' ', '')
  • new2 Thisisagoodexample

6
Character Casing
  • upper(str) to convert all letters to UPPER CASE
  • lower(str) to convert to lower case
  • gtgt shout upper('Hi there')
  • shout
  • HI THERE

7
Hands-On DEMO Regular Expression Match
regexp('str','expr') (not in textbook)
  • Any Single Character . (that's a period sitting
    there)
  • Use '..ain' in an expression to match a sequence
    of five characters ending in 'ain'. (Note that .
    matches white-space characters, like space, as
    well)
  • str 'The rain in Spain falls mainly on the
    plain.'
  • regexp(str, '..ain')
  • ans 4 13 24 39
  • Matches ' rain', 'Spain', ' main', and 'plain'.
  • Returning Strings Rather than Indices   
  • Here is the same example, this time specifying
    the command qualifier 'match'. In this case,
    regexp returns the text of the matching strings
    rather than the starting index
  • regexp(str, '..ain', 'match')
  • ans ' rain' 'Spain' ' main' 'plain'

8
Character Classes (I'll have a handout with this
info for the test)
  • Character classes represent either a specific set
    of characters (e.g., uppercase) or a certain type
    of character (e.g., non-white-space).

9
Selected Characters Range of Characters
  • Selected Characters c1c2c3 Use c1c2c3 in an
    expression to match selected characters r, p, or
    m followed by 'ain'. Specify two qualifiers this
    time, 'match' and 'start', along with an output
    argument for each, mat and idx. This returns the
    matching strings and the starting indices of
    those strings
  • mat idx regexp(str, 'mrpain', 'match',
    'start')
  • mat
  • 'rain' 'pain' 'main'
  • idx
  • 5 14 25
  • Range of Characters c1 - c2 Use c1-c2 in an
    expression to find words that begin with a letter
    in the range of A through Z
  • mat idx regexp(str, 'A-Z\w', 'match',
    'start')
  • mat
  • 'The' 'Spain'
  • idx
  • 1 13

10
Multiple Occurrences
  • Zero or More exprUse to match strings having
    any number of the previous expression, including
    none at all.
  • str 'LOOKS LIKE IT SHOULD WORK SO'
  • expr 'LO'
  • regexp(str, expr, 'match')
  • ans
  • 'LOO' 'L' 'L'
  • One or More exprWith you must have at least
    one occurrence of the expression. This looks for
    one or more O's followed by any letter
  • expr 'OA-Z'
  • regexp(str, expr, 'match')
  • ans
  • 'OOK 'OU' 'OR'

11
Word and White-Space Characters
  • \w, \sUse \w and \s in an expression to find
    words that end with the letter n followed by a
    white-space character.
  • mat ix regexp(str, '\wn\s', 'match',
    'start')
  • mat
  • 'rain ' 'in ' 'Spain ' 'on '
  • ix
  • 5 10 13 32

12
Numeric Digits
  • \dUse \d to find numeric digits in the following
    string
  • numstr 'Easy as 1, 2, 3'
  • mat idx regexp(numstr, '\d','start',
    'match')
  • mat
  • '1' '2' '3'
  • idx
  • 9 12 15

13
Logical Grouping Operators(again they'll be on
the handout for test)
  • Logical operators do not match any specific
    characters. They are used to specify the context
    for matching an accompanying regular expression.

14
Hands-On DEMO Start End of Words
  • Start and End of Word MatchUse \ltexpr\gt to match
    any words containing only a's or b's, ignore
    character casing (note i at end of regexpi)
  • a_b 'ab aaa B Aaaa bbb aAabBbcCc cdab'
  • mat idx regexpi(a_b, '\ltab\gt', 'match',
    'start') matches sub-words starting with a or
    ending with b
  • mat idx regexpi(a_b, '\lt(ab)\gt', 'match',
    'start') matches words containing ONLY a's or
    ONLY b's

15
What if the Pattern is Nowhere to be Found?
  • gtgt regexp('Original String','look for')
  • ans

16
regexprep extends strrep
  • gtgt str 'I came, I saw, I conquered!'
  • gtgt regexprep(upper(str), 'A-Z','')
  • ans
  • ICAMEISAWICONQUERED

17
Arrays of Strings
  • Character string arrays can be constructed by
    either of the following
  • As a vertical vector of strings, all of which
    must be the same length
  • By using a special version of the char() cast
    function that accepts a variable number of
    strings with different lengths, pads them with
    blanks to make all rows the same length, and
    stores them in an array of characters

18
Hands-On DEMO String Arrays
  • Strings in each row that are not the same length
  • a 'Now''is''the''time'
  • ERROR!
  • Same length
  • dbl 'ww''xx''yy''zz'
  • OK
  • Fancy new version of char function
  • words char('Now','is','the','time')
  • to check for trailing blanks
  • words dbl

19
Bio-Engineering Example DNA
  • See MatlabDNA folder inside 109Week05 folder
  • DNA_System.m
  • GenerateDNA.m
  • CountCG.m
  • FindOpenReadingFrame.m

20
Hands-On DEMO Count of Occurrences
  • ShakespeareSonnet18 by Dave Hannay
  • Sonnet18 ...
  • 'Shall I compare thee to a Summer''s day?'
    ...
  • 'Thou art more lovely and more temperate'
    ...
  • . . .
  • 'So long lives this, and this gives life to
    thee.'
  • searchWord input('Word to find? ', 's')
  • count CountWord(Sonnet18, searchWord) try
    thou
  • disp(upper(searchWord) ' occurs ' int2str(count)
    ' times in Sonnet 18')
  • function TheCount CountWord(text, word)
  • pattern '\lt' word '\gt'
  • foundAt regexpi(text, pattern)
  • TheCount length(foundAt)

21
In-Class Exercise 5c mini-GOOGLE
  • Search for Prefix of a string
  • Write a function findPrefix that takes in two
    strings. If the second string is contained within
    the first string, return ALL WORDS matching
    prefix. If not, return empty vector.
  • HINT Other than the function heading and
    comments, you can do this in a single line of
    code!
  • Use provided main program cs109ex5c.m
  • Put your name on cs109ex5c.m and findPrefix.m

22
END
23
Secret Messages--Text Approach
  • A scheme that makes use of the MATLAB random
    number generator to create a random character
    shift unique to each occurrence of characters in
    a message, so that letter frequency cannot be
    used to determine the encryption technique.
  • At the destination, as long as the random number
    generator is seeded with an agreed value, the
    message can be reconstructed by shifting back the
    message characters.
  • A few minor modifications wrap the characters on
    the alphabet to remain within the set of
    printable letters.

24
The Solution (see Listing_06_1.m)
  • encryption section
  • seed the random generator to a known state
  • rand('state', 123456) 123456 is arbitrary
  • lo_ch 33 where did these numbers come
    from???
  • hi_ch 126
  • range hi_ch1 - lo_ch
  • rn floor( range rand(1, length(txt) ) )
  • change (txtgtlo_ch) (txtlthi_ch)
  • enc txt
  • enc(change) enc(change) rn(change)
  • enc(enc gt hi_ch) enc(enc gt hi_ch) - range
  • disp('encrypted text')
  • encrypt char(enc)

25
The Solution - decryption
good decryption seed the random generator to
the same state rand('state', 123456) rn floor(
range rand(1, length(txt) ) ) change
(encryptgtlo_ch) (encrypt lt hi_ch) dec
encrypt dec(change) dec(change) - rn(change)
range dec(dec gt hi_ch) dec(dec gt hi_ch) -
range disp('good decrypt') decrypt char(dec)
Write a Comment
User Comments (0)
About PowerShow.com