Regular expression (0+1)*1(0+1)*0 DFA - automation

I'm trying to understand the regular expression: (0+1)*1(0+1)*0 Could you provide examples that matches this pattern?

Let me explain :
1 - (0+1) mean any number of 0, then a 1
2 - (0+1)* means the previous line any number of times (can be 0)
3 - (0+1)*1 mean the previous line and a 1
4 - (0+1)*0 means line 2 and a 0
10 works : 0 times (0+1), then a 1, then 0 times (0+1), then a 0.
00000000000100000000000110 works : eleven 0 and a 1, twice (this is (0+1)*). Then, a 1. Then, no (0+1), and the last 0. A few other examples :
10
00001000010000110000100001000010
01010110
0110
I hope you understood (I'm not english, my english is bad, sorry)
EDIT : There are a lot of websites that can help you with regular expressions, whether it is learning or testing regex.

Related

Why do these 2 for looping over sequences differ?

First:
$ raku -e "for 1...6, 7...15 { .say }"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Now:
$ raku -e "for 1...3, 7...15 { .say }"
1
2
3
7
11
15
I would expect this case to print 1,2,3,7,8,... 15.
What's happening here?
I think you might want the raku Range operator .. (two dots) and not the raku Sequence operator ... (three dots).
Here's how you examples look with the Range operator instead:
> raku -e 'for 1..6, 7..15 { .say }'
1..6
7..15
Oh, that's not good ... looks like for is just iterating over the two things 1..6 and 7..15 and stringifying them.
We can use a Slip | to fix that:
> raku -e 'for |(1..6), |(7..15) { .say }'
1
2
... (all the numbers)
14
15
And then:
raku -e 'for |(1..3), |(7..15) { .say }'
1
2
3
7
8
9
10
11
12
13
14
15
With the Sequence operator, you have made something like:
>raku -e 'for 3,7...15 { .say }'
3
7
11
15
That is raku for "make a sequence that starts with 3, then 7, then all the values until you get to the last at 15" ... and since the gap from 3 to 7 is 4, raku will count up in steps of 4. Then you began it with 1..3. ;-)
~p6steve
It's because it is two deductive sequences.
1...3
Is obviously a sequence where you add 1 to each successive value.
1, 2, 3
And since 7 is 4 more than 3, this is a sequence where you add 4 to each successive value.
3, 7 ... 15
3, 7, 11, 15
To get what you want, you could use a flattened Range.
1...3, |(7..15)
Or even a flattened Sequence.
1...3, |(7...15)
TL;DR This answer focuses on addressing what you originally asked (which was about "sequences") and precisely what the code you wrote is doing, rather than providing a solution (using ranges instead).
This is a work in progress dealing with something that seems both poorly documented and hard to fathom (which may explain part though not all of the doc situation). Please bear with me! (And I may just end up deleting this answer.)
1 ... 3, 7 ... 15 ≡ 1 ... (3, 7) ... 15
In the absence of parentheses, operators within an expression are applied according to rules of "precedence" and "associativity".
Infix , has a higher precedence than infix ....¹ The above two lines of code thus produce the same result (1␤2␤3␤7␤11␤15␤):
for 1 ... 3, 7 ... 15 { .say } # Operator evaluation by precedence
for 1 ... (3, 7) ... 15 { .say } # Operator evaluation by parentheses
That said, while the result is what, given a glance at the code, I would expect based on my own "magical" DWIM ("Do What I Mean") thinking, I must say I don't yet know what the precise Raku(do)'s rule(s) are that lead to it DWIMing.
The doc for infix ... says:
If the endpoint is not *, it's smartmatched against each generated element and the sequence is terminated when the smartmatch succeeded.
But that seems overly simple. What if the endpoint of one sequence is another sequence? (As, at least taking a naive view, appears to be the case in your code.)
Also, as #MustafaAydin has noted:
how does your post explain the irregular last step size (of 2) instead of 3? I mean 4, 7 ... 15 alone produces (4, 7, 10, 13). But 1... 4, 7...15 now produces 7, 10, 13, 15 in the tail. Why is 15 included? Maybe i'm missing something idk
I'm at least as confused as Mustafa.
Indeed, I'm confused about several things. How come Raku(do) flattens the two sequences? [D'oh. Because the infix comma is higher precedence than the infix ....] Why doesn't it repeat the 3 in the final combined list? [Perhaps because multiple infix ...s are smart about what to do when there's an expression that's the endpoint of one sequence and the start of another?]
I'm going to go read the old design docs and/or spelunk roast and/or the Rakudo compiler code to see if I can see what's supposedly/actually going on. But not tonight.
Footnotes
¹ There's a table of operators in the current official operator doc. Supposedly this table:
summarizes the precedence levels offered by Raku, listing them in order from high to low precedence.
Unfortunately, at the time of writing this, the central operator table in the Operators page is profoundly wrong #4071.
Until that's fixed, here are "official" and "unofficial" options for determining the precedence of operators:
"official" Use in page search to search the official doc operator page for the operator of interest. Skip to the match in the entries on the left hand side of that same page. As you'll see, infix ,' is one level higher precedence than infix ...`:
Comma operator precedence
infix ,
infix :
List infix precedence
infix Z
infix X
infix ...
"unofficial" Look at the corresponding page of a staging site for an improved doc site. (I don't know how up to date it is, but the central table appears to list operators by precedence order as it claims.)

Extractive Text Summarization: Weighting sentence location in document

I am looking at an extractive text summarization problem. Eventually, I want to generate a list of words (not sentences) that seem to be the most important. One of the ideas that I had was to the words that appear early in the document more heavily.
I have two dataframes. the first is a set of words with their occurrence counts:
words.head()
words occurrences
0 '' 2
1 11-1 1
2 2nd 1
3 april 1
4 b.
And the second is a set of sentences. 0 is the first sentence in the document, 1 is the secont.. etc.
sentences.head()
sentences
0 Site Menu expandHave a correction?...
1 This will be a chance for ...
2 The event will include...
3 Further, this...
4 Contact:Share:
I managed to accomplish my goal like this:
weights = []
for value in words.index.values:
weights.append(((len(sentences) - sentences.index.values) *
sentences['sentences'].str.contains(words['words'][value])).sum())
weights
[0,
5,
5,
0,
12,...]
words['occurrences'] *= weights
words.head()
words occurrences
0 '' 0
1 11-1 5
2 2nd 5
3 april 0
4 b. 12
However, this seems sort of sloppy. I know that I can use list comprehension (I thought it would be easier to read on here without it) - but, other than that, does anyone have thoughts on a more elegant solution to this problem?

Finding Coefficients of LFSR

I am studying cryptography from Cristof Paar's book. There is a question about LFSR's I have trouble with. I just can't understand one point here. Question is this:
We want to perform an attack on another LFSR-based stream cipher. In order
to process letters, each of the 26 uppercase letters and the numbers 0, 1, 2, 3, 4, 5
are represented by a 5-bit vector according to the following mapping:
A -> 0 = 00000
.
.
.
Z -> 25 = 11001
0 -> 26 = 11010
.
.
.
5 -> 31= 11111
(binary)
We happen to know the following facts about the system:
-The degree of the LFSR is m = 6.
-Every message starts with the header WPI
We observe now on the channel the following message (the fourth letter is a zero): j5a0edj2b
What are the feedback coefficients of the LFSR? (This one!)
Solution:
I can't understand the matrix in this solution where did these numbers come?
Using WPI, we have plaintext begins with
P=>(10110)(01111)(01000)
Using j5a0edj2b we have the ciphertext
C=>(01001)(11111)(00000)(11010)(00100)(00011)(01001)............
then by addition of P and C in mod 2, the key stream is
S=>(11111)(10000)(01000)....
we find the matrix from key stream
s0=1,s1=1,s2=1,s3=1,s4=1,s5=1,s6=0,s7=0,s8=0,s9=0,s10=0,s11=1 etc
For the matrix
first line.... (s0,s1,s2,s3,s4,s5)
second line....(s1,s2,s3,s4,s5,s6)
third line.....(s2,s3,s4,s5,s6,s7)
4th (s3,s4,s5,s6,s7,s8)
5th (s4,s5,s6,s7,s8,s9)
last line (s5,s6,s7,s8,s9,s10)
this calulations are given in LFSRs in details

Converting binary to decimal with out using a function

I'm trying to create a binary to decimal converter, and have got stuck on the code. I have researched forums for any help, but they all seam to use functions, which can not be used within a private sub. Please can anyone give me help on a solution to this problem?
I would use the positional notation method:
http://en.wikipedia.org/wiki/Positional_notation
http://www.wikihow.com/Convert-from-Binary-to-Decimal
So basically, without giving you the answer, you want to loop through binary place holders, filling up a variable as you go along. You would use an index to move from the least significant placeholder to the most.
For example : 10011011 in binary is 155 decimal.
So every place holder is a power with a base of two. Then you add the value for each one until your finished, like so:
placeholder 1 is: 2 pow 0 equals 1.
placeholder 2 is: 2 pow 1 equals 2.
placeholder 3 is: 2 pow 2 equals 4.
placeholder 4 is: 2 pow 3 equals 8.
placeholder 5 is: 2 pow 4 equals 16.
placeholder 6 is: 2 pow 5 equals 32.
placeholder 7 is: 2 pow 6 equals 64.
placeholder 8 is: 2 pow 7 equals 128.
Now we only add for the placeholders that have 1s.
128+16+8+2+1 = 155
What you will need:
A loop looping through indexes, and incrementing the exponent value as you go along, only adding the value if the index equals 1 in the binary number.
Hope my explanation makes sense. Good luck.

Determinant Finite Automata (JFLAP)

I have a DFA question (Determinant Finite Automata) . We are using JFLAP to construct the automata. I cannot figure this question out to save my life! Here it is
"DFA to recognize the language of all strings that have an even number of zeros and an odd number of ones."
So the alphabet is {0,1} and only using 0,1. So I need to build an automata that recognizes an even number of zeros and an odd number of ones.

			
				
I don't know whether my understanding is right.
I could give you the description in Grail format that generate an even number of zeros and an odd number of ones.
START 1
1 1 2
2 1 1
1 0 3
3 0 4
4 0 3
FINAL 3