NPDA for L= {w ∈ {a,b}*: number of a's is twice the number of b's} - computation-theory

I have been trying to find a NPDA for this language but I can't come up with anything that will accept all of the words in the language. I did try making its accepting condition an empty stack and using the stack alphabet {$ a b} but maybe I should try something else?

Maybe you are looking the equivalent Grammar for the given Regular Expression.
If so maybe this one with the following productions:
S->AAb | SAAb | ASAb | AASb | AAbS
A->a
Some Tests:
aab : S->AAb->aAb->aab
aaaabb : S-> AASb -> AA(AAb)b -> aaaabb
Could check also with other example and see if fit on all cases.
If fine always should have on Result = (2*count_of_b) for a, count_b
Looking twice, A->a can be removed and have only:
S->aab|Saab|aSab|aaSb|aabS
Test:
aaaabb : aaSb(S_4)->aaaabb(S_1), etc.

The context free grammer for number of a's is twice the number of b's is:
S--> aaSb / €(epsilon)
Push down automata can be done for a^2nb^n.push down automata is nothing but the finite automata with memory (can be stack).
Here, for every two a's push one 'a' into the stack and for the input symbol 'b' pop 'a' from the stack.
At the end, if the stack is empty then the string is accepted.
PDA image for a^2nb^n:
δ(q0,a,z0)=(q0,az0)
δ(q0,a,a)=(q1,a)
δ(q1,a,a)=(q0,aa)
δ(q1,b,a)=(q2,ϵ)
δ(q2,b,a)=(q2,ϵ)
δ(q2,ϵ,z0)=(q3,z0)
where,
Q={q0,q1,q2,q3}
Σ=(a,b)
q0=initial state
q3=final state
From the figure for the odd number of a's push into the stack and for even a's skip.Then for each 'b' pop one 'a' from the stack. At the end, an empty stack represents that the string is accepted.

Related

How is `{⊂⍵}` different from just `⊂`?

I'm reading through Hui and Kromberg's recent "APL Since 1978" and in the discussion of ⌺ (stencil) they give the following example:
{⊂⍵}⌺5⊢'abcde'
abc abcd abcde bcde cde
Why is the {⊂⍵} needed over just ⊂? I'm still pretty new to APL but I would naively think that in general {f⍵} should be equivalent to f when called monadically.
Empirically I can see that's not the case:
⊂⌺5⊢'abcde'
DOMAIN ERROR
⊂⌺5⊢'abcde'
∧
But I don't understand why.
You're absolutely right that {⊂⍵} is equivalent to ⊂ when called monadically, however as per the documentation:
f is invoked dyadically with a vector left argument indicating for each axis the number of fill elements and on what side; positive values mean that the padding precedes the array values, negative values mean that the padding follows the array values.
We can illustrate this by making the function return the enclosure of both arguments together:
{⊂⍺ ⍵}⌺5⊢'abcde'
┌─────────┬─────────┬─────────┬──────────┬──────────┐
│┌─┬─────┐│┌─┬─────┐│┌─┬─────┐│┌──┬─────┐│┌──┬─────┐│
││2│ abc│││1│ abcd│││0│abcde│││¯1│bcde │││¯2│cde ││
│└─┴─────┘│└─┴─────┘│└─┴─────┘│└──┴─────┘│└──┴─────┘│
└─────────┴─────────┴─────────┴──────────┴──────────┘
This left argument is designed to fit the requirements as left argument of ↓ so the added padding can be removed easily:
{⊂⍺↓⍵}⌺5⊢'abcde'
┌───┬────┬─────┬────┬───┐
│abc│abcd│abcde│bcde│cde│
└───┴────┴─────┴────┴───┘
If you want a tacit operand instead of {⊂⍵} then you can use ⊢∘⊂ (which is equivalent to {⍺⊢⊂⍵} and therefore {⊂⍵}) or, in version 18.0, ⊂⍤⊢ (which is equivalent to {⊂⍺⊢⍵} and therefore {⊂⍵}).

How does element membership work in Perl 6?

Consider this example
my #fib = (1,1, * + * … * > 200).rotor(2 => -1);
say #fib[0] ∈ #fib; # prints True
The first statement creates a Sequence of 2-element subsequences via the use of the rotor function. #fib will contain (1,1), (1,2) and so on. Quite obviously, the first element of a sequence is part of a sequence. Or is it?
my #fib = (1,1, * + * … * > 200).rotor(2 => -1);
say #fib[0], #fib[0].^name; # OUTPUT: «(1 1)List␤»
So the first element contains a list whose value is (1 1). OK, let's see
my $maybe-element = (1,1);
say $maybe-element, $maybe-element.^name; # OUTPUT: «(1 1)List␤»
say $maybe-element ∈ #fib; # OUTPUT: «False␤»
Wait, what? Let's see...
my $maybe-element = #fib[0];
say $maybe-element ∈ #fib; # OUTPUT: «True␤»
Hum. So it's not the container. But
say (1,1).List === (1,1).List; # OUTPUT: «False␤»
And
say (1,1).List == (1,1).List; # OUTPUT: «True␤»
So I guess ∈ is using object identity, and not equality. That being the case, how can we check, in sets or sequences of lists, if an independently generated list is included using this operator? Should we use another different strategy?
Maybe a subquestion is why the same literals generate completely different objects, but there's probably a good, and very likely security-related, answer for that.
So I guess ∈ is using object identity, and not equality.
That is correct.
That being the case, how can we check, in sets or sequences of lists, if an independently generated list is included using this operator?
You can use .grep or .first and the equality operator of your choice (presumably you want eqv here), or you can try to find a list-like value type. Off the top of my head, I don't know if one is built into Perl 6.

(GNU) Forth Local Variable Behavior

I was just learning about local variables for word definitions in Forth. I happen to use GNU Forth (gforth). I was looking at the question and answer, Forth local variable assigning variables and was struggling with the behavior of the given answer. When I tried it, I was getting an underflow unless I had four cells on the stack.
Consider this simple example:
: foo { a b } a b + . ;
This word will take the top two stack cells, store them in the local variables a and b, put a and b (in that order) back on the stack, add them, pop and display the result, and emit a carriage return. It works as I would expect, leaving nothing on the stack when it completes:
: foo { a b } a b + . cr ; ok
1 3 foo 4
ok
.s <0> ok
Now I'd like to try a local variable which is not taken from the stack originally:
: foo { a b | c } a b + to c c . cr ;
I would expect this to behave similarly but use the local variable c. This word would take the top two stack cells, store them in the local variables a and b, put a and b (in that order) back on the stack, add them, pop the result and store it in c, push c back onto the stack, then pop and display the top stack cell and emit a carriage return.
This one doesn't work as I would have expected. Here are my results:
: foo { a b | c } a b + to c c . cr ; ok
1 3 foo
:3: Stack underflow
1 3 >>>foo<<<
Backtrace:
$7F2B572EA1F0 >
Hm, ok, why is there an underflow? Let's try an additional cell on the stack:
1 3 5 foo
:4: Stack underflow
1 3 5 >>>foo<<<
Backtrace:
$7F2B572EA1F8 >l
Still an underflow! Let's try another:
1 3 5 7 foo 4
ok
.s <0> ok
No more underflow. The word foo has consumed all of the cells, but the top two don't appear to be used anywhere. The result 4, which is the sum of the first two cells on the stack, is what I would have expected when I originally tried 1 3 foo.
I have been trying to find some good documentation regarding the local variable behavior, but the manual is very terse on this topic. Can someone explain what is happening here?
Per the comment thread to the question, there is a bug in local variable handling in the "current release", version 0.7.3 (7/9/2014) which has been resolved in a later development release. Downloading, building, and using version 0.7.9_20180319 indicated that the problem was resolved. Thanks to Lars Brinkhoff for pointing out the resolution.

How does "Assignment Branch Condition size for index is too high" work?

Rubocop is always report the error:
app/controllers/account_controller.rb:5:3: C: Assignment Branch Condition size for index is too high. [30.95/24]
if params[:role]
#users = #search.result.where(:role => params[:role])
elsif params[:q] && params[:q][:s].include?('count')
#users = #search.result.order(params[:q][:s])
else
#users = #search.result
end
How to fix it? Anyone has good idea?
The ABC size [1][2] is
computed by counting the number of assignments, branches and conditions for a section of code. The counting rules in the original C++ Report article were specifically for the C, C++ and Java languages.
The previous links details what counts for A, B, and C. ABC size is a scalar magnitude, reminiscent of a triangulated relationship:
|ABC| = sqrt((A*A)+(B*B)+(C*C))
Actually, a quick google on the error shows that the first indexed page is the Rubocop docs for the method that renders that message.
Your repo or analysis tool will define a threshold amount when the warning is triggered.
Calculating, if you like self-inflicting....
Your code calcs as
(1+1+1)^2 +
(1+1+1+1+1+1+1+1+1+1+1+1+1)^2 +
(1+1+1+1)^2
=> 194
That's a 'blind' calculation with values I've made up (1s). However, you can see that the error states numbers that probably now make sense as your ABC and the threshold:
[30.95/24]
So cop threshold is 24 and your ABC size is 30.95. This tells us that the rubocop engine assign different numbers for A, B, and C. As well, different kinds or Assignments (or B or C) could have different values, too. E.G. a 'normal' assignment x = y is perhaps scored lower than a chained assignment x = y = z = r.
tl;dr answer
At this point, you probably have a fairly clear idea of how to reduce your ABC size. If not:
a simple way it to take the conditional used for your elsif and place it in a helper method.
since you are assigning an # variable, and largely calling from one as well, your code uses no encapsulation of memory. Thus, you can move both if and elsif block actions into each their own load_search_users_by_role and load_search_users_by_order methods.

Alloy constraint specification

I wrote the following code block in Alloy:
one h: Human | h in s.start => {
s'.currentCall = h.from
}
I want to pick one 'human' from a set of humans (s.start) and set a variable (s'.currentCall) equal to h.from.
However I think this code is saying: There is only one human in s.start, where
s'.currentCall = h.from
is true.
Is my assumption correct? And how should I fix this?
You are absolutely correct, the meaning of the one quantifier is that there is exactly one element in the given domain (set) such that the quantifier body holds true.
Regarding your original goal of picking one element from a set and setting its field value to something: that sounds like an imperative update, and you can't really do that directly in Alloy; Alloy is fully declarative, so you can only assert logical statements about the sets and relations for a bounded universe of discourse.
If you just change one to some and also change the implication to conjunction, and then run the analysis (a simple run command to find a valid instance), the Alloy Analyzer will find a model in which the value s'.currentCall is equal to h.from for some (arbitrary) h from s.start:
pred p[s, s': S] {
some h: s.start | s'.currentCall = h.from
}
run p
I hope this is what you want to achieve.

Resources