[Tutor] trying to understand pattern matching code

Joel Goldstick joel.goldstick at gmail.com
Fri Apr 25 14:16:57 CEST 2014


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/c5aff22e/attachment.html>


More information about the Tutor mailing list