How to solve Turing Machine example for anbncn / n>=1? - turing-machines

Can anyone tell me whether my answer is right or not for this example of Turing Machine??
Q. Construct Turing Machine for L={ anbncn / n>=1 }
Ans: Transition function for string aabbcc
^(q1,a)=(X,q2,R)
^(q2,a)=(a,q2,R)
^(q2,b)=(Y,q3,R)
^(q3,b)=(b,q3,R)
^(q3,c)=(Z,q4,L)
^(q4,b)=(b,q4,L)
^(q4,Y)=(Y,q4,L)
^(q4,a)=(a,q4,L)
^(q4,X)=(X,q1,R)
^(q1,a)=(X,q2,R)
^(q2,Y)=(Y,q2,R)
^(q2,b)=(Y,q3,R)
^(q3,Z)=(Z,q3,R)
^(q3,c)=(Z,q4,L)
^(q4,Z)=(Z,q4,L)
^(q4,Y)=(Y,q4,L)
^(q4,Y)=(Y,q4,L)
^(q4,X)=(X,q1,R)
^(q1,Y)=(Y,q2,R)
^(q2,Y)=(Y,q2,R)
^(q2,Z)=(Z,q2,R)
^(q2,Z)=(Z,q2,R)
^(q2,B)=(B,q5,N)
Can anyone tell me whether its correct or not??
Or should I have to make changes in it??

It seems correct
The idea is:
Turn the first a to X, then go right to find the first b, turn it to Y, go right to find the first c, turn it to Z, get back to the first a again.
The process goes on until the first letter under the head is Y instead of a. It means that all a's have been matched with b's and c's.
The head then needs to go right, passes Y's and Z's till it reaches a blank.

Related

Length error while creating a dfn function in APL to check for anagrams

I am a beginner in APL and am writing a dfm function to check whether two strings are an anagram of one another. The method I thought of was:
{⍵[⍋⍵] ≡ ⍺[⍋⍺]}
However, it returns 0 for 'ALBERT EINSTEIN' and 'TEN ELITE BRAINS' that are anagrams. I tried to check why:
a ← 'ALBERT EINSTEIN'
b ← 'TEN ELITE BRAINS'
a[⍋a] ≡ b[⍋b]
0
a[⍋a]
ABEEEIILNNRSTT
b[⍋b]
ABEEEIILNNRSTT
a[⍋a] = b[⍋b]
LENGTH ERROR
a[⍋a]=b[⍋b]
Is it because the second rearrangement seems to have a leading space? Why does that happen? Can anyone suggest a way I can make this program work?
In your specific case, you would need to filter out the spaces from the vectors you're comparing. This is because your vectors will have different lengths after grading up, since one of them has an extra space.
One solution could be:
'albert einstein'{(a b)←(⍺~' ')(⍵~' ')⋄a[⍋a]≡b[⍋b]}'ten elite brains'
The first part of the code, (a b)←(⍺~' ')(⍵~' '), assigns ⍺ and ⍵, without spaces (~' '), to a and b respectively. Then, you can just compare the vectors the same way you're doing now.
You can check the output of the code above on Try it online!

Alexa adding extra character to slot value

​I am trying to get user response with option of a b c d or e . I have configured a slot with these possible values and reading the slot in my nodejs. when a user responds with the option a, c,d,e are returned ok in intent.slots.Answer.value (although option "a" is returned as lower case in slot value and C,D and E returns in upper case) but the bigger issue is option "b" is returned with an extra dot (.) appended like this "b." While I can parse it out, I hate to patch it before I know what is causing it. I have done console.log of slot values before any manipulation and it is as per above description. has anyone experienced this?
The slot values you provide are getting used when the language model get's build. But there is no guarantee, that those values will be recognized by alexa and forwarded to your intent. It could by anything alexa understands. I guess alexa is considering a, c, d and e as words, but in the case of b, she understands it's a single letter (which get's returned as "B."). Why? Welcome to the mysteries of black box Alexa :)
I would clean the value like this:
value.toLowerCase().replace(/\./, '')
Building on unnu's answer... I would just use whole word options like One, Two, Three...

Live variable analysis , is my explanation correct?

As stackexchange have not more tags about compiler tags , so I'm posting here , this question .
A variable x is said to be live at a statement Si in a program if the following three conditions hold simultaneously :
1. There exists a statement Sj that uses x
2. There is a path from Si to Sj in the flow
graph corresponding to the program
3. The path has no intervening assignment to x
including at Si and Sj
The variables which are live both at the statement in basic block 2 and at the statement in basic block 3 of the above control flow graph are
p, s, u
r, s, u
r, u
q, v
I try to explain :
As the wikipedia says "Stated simply: a variable is live if it holds a value that may be needed in the future."
As per the definition given in question, a variable is live if it is used in future before any new assignment.
Block 2 has ‘r’ and ‘v’ both as live variables. as they are used in block 4 before any new value assinged to them. Note that variable ‘u’ is not live in block 2 as ‘u’ is assigned a new value in block 1 before it is used in block 3. Variables ‘p’, ‘s’ and ‘q’ are also not live in block 2 due to same reason.
Block 3 has only ‘r’ as live variable as every other variable is assigned a new value before use.
Another explanation given as :
Only r.
p, s and u are assigned to in 1 and there is no intermediate use of them before that. Hence p, s and u are not live in both 2 and 3.
q is assigned to in 4 and hence is not live in both 2 and 3.
v is live at 3 but not at 2.
Only r is live at both 2 and 3.
But official GATE key said both r and u.
I see two things that are probably at least part of your confusion.
First, in the list of conditions for live variables, there is no restriction stating that Si != Sj, so this makes the definitions a little imprecise in my mind.
The second is your assertion "Note that variable ‘u’ is not live in block 2 as ‘u’ is assigned a new value in block 1 before it is used in block 3."
The way I would look at this would be this:
Upon entry into block 2, before the statement v = r + u is
executed (imagine a no-op empty statement inserted as the entry to
each block, and another as the exit from the block), then both r
and u must be live, because there exists an upcoming statement
that uses both, and there is a path in the code from the entry to
that statement that contains no intermediate assignments to either.
Rather than imagining these extra empty statements, using the
original semantics of the definitions, then we're just talking about
the case where Si == Sj, because both refer to the v = r + u
statement - there is trivially a path from a statement to itself
containing no additional assignments in that sense. I find it easier
to think about it with the extra empty entry and exit statements,
though.
After the execution of v = r + u, however, at the imaginary
block exit empty statement, it is now safe to consider u as not
live (alternatively, dead), because nothing in block 4 uses it, and
it's reassigned in block 1 before it's ever used again in either
block 2 or 3.
So, part of the confusion seems to be thinking of whether a variable is live in a particular block, which doesn't really fit with the definitions - you need to think about whether a variable is live at the point of a particular statement. You could make the case that the variable u is both alive and dead in block 2, depending on whether you look at it before execution of the lone statement, or after...

Is it possible to link IPython widgets less naively?

I'll start with a simple example of what I'm trying to do:
Say, I have two IntSlider-widgets. I would like to have one represent x and the other x^2 (unidirectional link). Maybe I would even want the first slider to display sqrt(x^2), if I were to play with the second slider (bidirectional link).
This would,very naively, translate in something like this:
l1 = traitlets.link((widg1, 'value'),(widg2, 5* 'value'))
Which of course it doesn't, because the second tuple argument is supposed to be a string, so really I am passing 'valuevaluevaluevaluevalue'.
Anyway, would this be possible and if yes, has it already been implemented?
link only passes on the same value. If you want to transform the value, you need a callback:
def widg1_changed(name, new_value):
widg2.value = 5 * new_value
widg1.on_trait_change(widg1_changed, 'value')
I don't know of a good way to do this bidirectionally at present.

Deleting node from singly linked list

If your goal is to write a function to delete a node in a singly linked list, and you are only given access to the node you want to delete (not given head), Why doesn't my solution work?
My solution:
public void deleteNode(Node n) {
n = n.next;
}
Book's solution:
public void deleteNode(Node n) {
n.data = n.next.data;
n.next = n.next.next;
}
It seems to me that simply assigning n to n.next takes care of assigning the data and pointer in one line.
This is an easy mistake to make before you get a good mental model of how the manipulation of linked lists work. The deleteNode is given a reference to the node you want to delete. It stores this reference in the variable n which is set up only for the use of the deleteNode code. In your solution, deleteNode changes the value of n to point to the next node in the list, but this only affects n which is discarded once execution reached the end of deleteNode. The linked list still exists exactly as it did before deleteNode was executed. Put another way, the node that precedes the node you want to delete still points to the same node that it did when you entered deleteNode, and the node that was supposed to be deleted still points to the next in the line. Changing n had no effect.
The book's solution, however, does change things up a bit. After it runs, the node that is supposed to be deleted now contains the data and next pointer from what used to be the next node down. This affectively "shifts" the contents of the next node up one.
All nodes are somewhere in the memory, and each node knows where to find the next node
(independent of variables in the code, ie. no "n" or something like that).
Additionally, there can be one or more variables per node which are "pointing"
to that node in memory, The problem in your method is, you´re only
changing the target of your vriable n, but not the nodes itself.
If you have 4 nodes with values A,B,C,D and want to delete B, your variable n points to B.
After the assignment in your method, n would point to C,
but the 4 nodes itself would be unchanged.
The books solution does change the value and next of B itself...

Resources