Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Regular Expreso 2by oiskuu (Hermit) |
on Apr 27, 2016 at 17:29 UTC ( [id://1161674]=note: print w/replies, xml ) | Need Help?? |
Multiplying by three is the same as shift + add. Essentially you have a binary adder with carry, or rather two carries: the input bit is also a carry. So you have an FSM with three states for the three possible carry values 0, 1, 2. 00111 + 00111 -------- 10101 Let's start with a recursive regex with groups for each of the states. From here, we can inline the group 2 in g1 and g3: Now it should be obvious that the group 3 i.e. carry=2 state is entered with a 11 10 and left with 00 01. Rewriting the regex with repeats instead of recursion:
ps. I'm assuming that the input string must have 2*n digits.
In Section
Meditations
|
|