[Tutor] trying to understand pattern matching code

rahmad akbar matbioinfo at gmail.com
Fri Apr 25 15:33:54 CEST 2014


Thanks joel

Could you elaborate more on bitwise, and why do we need to double bit. I
experimented with some input

ShiftAnd('pattern', 'textcontainingpatternandpatern')

print out the (masks, accept_state), looks like bellow, the rest make sense
couse the numbers are doubled, but the 't' goes 4+8 = 12. why is this?

({'a': 2, 'e': 16, 'n': 64, 'p': 1, 'r': 32, 't': 12}, 64)


and  i could not make sense of this line yet :  D = (( D << 1) + 1) & masks
[ c ]


to Dave,

i do i do the text mode? i had no idea this was on html
Am 25.04.2014 14:16 schrieb "Joel Goldstick" <joel.goldstick at gmail.com>:

>
> On Apr 25, 2014 7:14 AM, "rahmad akbar" <matbioinfo at gmail.com> wrote:
> >
> > hey guys,
> > i am trying to understand this code pasted bellow,
> > 1. what is line means? mask[c] |= bit
> This bitwise or.  It sets the rightmost bit in masks[c]
> > 2. then the line bit *=2, this increment the bit to 2 for each characters
> This doubles bit.
> > 3. what is this line means? accept_state = bit //2
> Integer division
> > 4. lastly, what is this scary line means? D = (( D << 1) + 1) & masks [
> c
>
> The << is bitwise shift left
> > def ShiftAnd (P , T ):
> >   m = len ( P )
> >   masks = dict () # empty dictionary
> >   bit = 1
> >   for c in P :
> >     if c not in masks : masks [ c ] = 0
> >     masks [ c ] |= bit
> >     bit *= 2
> >   accept_state = bit // 2
> >   D = 0 # bit - mask of active states
> >   i = 0
> >   for c in T :
> >     D = (( D << 1) + 1) & masks [ c ]
> >   if ( D & accept_state ) != 0:
> >     yield i
> >   i += 1
> >
> >
> You should sprinkle some print statements in and run with various arguments
> >
> >
> >
> > --
> > many thanks
> > mat
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140425/fb36b67e/attachment-0001.html>


More information about the Tutor mailing list