conversion of pushdown automata - theory

what will be the pushdown automata for the language
L = { wxw^r | w is in (0+1)* and x is either 0 or 1
I also want to know the transitions if we pass string 001000100
please help I am stick because the length of x is 1

Related

SPSS: using IF function with REPEAT when each case has multiple linked instances

I have a dataset as such:
Case #|DateA |Drug.1|Drug.2|Drug.3|DateB.1 |DateB.2 |DateB.3 |IV.1|IV.2|IV.3
------|------|------|------|------|--------|---------|--------|----|----|----
1 |DateA1| X | Y | X |DateB1.1|DateB1.2 |DateB1.3| 1 | 0 | 1
2 |DateA2| X | Y | X |DateB2.1|DateB2.2 |DateB2.3| 1 | 0 | 1
3 |DateA3| Y | Z | X |DateB3.1|DateB3.2 |DateB3.3| 0 | 0 | 1
4 |DateA4| Z | Z | Z |DateB4.1|DateB4.2 |DateB4.3| 0 | 0 | 0
For each case, there are linked variables i.e. Drug.1 is linked with DateB.1 and IV.1 (Indicator Variable.1); Drug.2 is linked with DateB.2 and IV.2, etc.
The variable IV.1 only = 1 if Drug.1 is the case that I want to analyze (in this example, I want to analyze each receipt of Drug "X"), and so on for the other IV variables. Otherwise, IV = 0 if the drug for that scenario is not "X".
I want to calculate the difference between DateA and DateB for each instance where Drug "X" is received.
e.g. In the example above I want to calculate a new variable:
DateDiffA1_B1.1 = DateA1 - DateB1.1
DateDiffA1_B2.1 = DateA1 - DateB2.1
DateDiffA1_B1.3 = DateA1 - DateB1.3
DateDiffA1_B2.3 = DateA1 - DateB2.3
DateDiffA1_B3.3 = DateA1 - DateB3.3
I'm not sure if this new variable would need to be linked to each instance of Drug "X" as for the other variables, or if it could be a single variable that COUNTS all the instances for each case.
The end goal is to COUNT how many times each case had a date difference of <= 2 weeks when they received Drug "X". If they did not receive Drug "X", I do not want to COUNT the date difference.
I will eventually want to compare those who did receive Drug "X" with a date difference <= 2 weeks to those who did not, so having another indicator variable to help separate out these specific patients would be beneficial.
I am unsure about the best way to go about this; I suspect it will require a combination of IF and REPEAT functions using the IV variable, but I am relatively new with SPSS and syntax and am not sure how this should be coded to avoid errors.
Thanks for your help!
EDIT: It seems like I may need to use IV as a vector variable to loop through the linked variables in each case. I've tried the syntax below to no avail:
DATASET ACTIVATE DataSet1.
vector IV = IV.1 to IV.3.
loop #i = .1 to .3.
do repeat DateB = DateB.1 to DateB.3
/ DrugDateDiff = DateDiff.1 to DateDiff.3.
if IV(#i) = 1
/ DrugDateDiff = datediff(DateA, DateB, "days").
end repeat.
end loop.
execute.
Actually there is no need to add the vector and the loop, all you need can be done within one DO REPEAT:
compute N2W=0.
do repeat DateB = DateB.1 to DateB.3 /IV=IV.1 to IV.3 .
if IV=1 and datediff(DateA, DateB, "days")<=14 N2W = N2W + 1.
end repeat.
execute.
This syntax will first put a zero in the count variable N2W. Then it will loop through all the dates, and only if the matching IV is 1, the syntax will compare them to dateA, and add 1 to the count if the difference is <=2 weeks.
if you prefer to keep the count variable as missing when none of the IV are 1, instead of compute N2W=0. start the syntax with:
If any(1, IV.1 to IV.3) N2W=0.

Bayesian Network Probability For Child Node

Given the following Bayesian network determine the probabilities.
On the network shown in Figure 1, suppose that:
P("alternator broken"=true) = 0.02
P("no charging"=true | "alternator broken"=true) = 0.95
P("no charging"=true | "alternator broken"=false) = 0.01.
What is P("no charging"=false)? How is it derived?
How would you go about determining "no charging" without having information about "fanbelt broken"?
Would the following be true:
P("no charging"=false) =
P("alternator broken"=true) * P("no charging"=true | "alternator broken"=true) + P("alternator broken"= false) * P("no charging"=true | "alternator broken"= false)
It's not possible
To calculate P("no charging") for the given BN, you are missing the prior for fanbelt broken. And also the CPT for no charging is underspecified, because no charging depends on fanbelt broken.
But you might want to
The best you can do with the information you have is simply ignore fanbelt broken. If the values for P( "charging" | "alternator broken") are obtained by taking the correct expectation over fanbelt broken, then the result is correct. If the latter is true this means that fanbelt broken is already eliminated (summed out), and it's influence is incorporated into the CPT for ´charging`.

C: While Loop With Logical OR

I am writing a simple text game in C where the player combats a very simple AI. Inside the main() function, there is a loop that should continue as long as the player's HP or the enemy's HP is higher than 0. See the example below.
while(grunt->hp > 0 || player->hp > 0) {
stats(player);
printf("Grunt HP: %d\n", grunt->hp);
plyMove(player, grunt);
aiMove(player, grunt);
}
if(player->hp > 0) {
printf("You won the battle!\n"); }
else {
printf("You've lost the battle!\n"); }
return 0;
Now to my understanding, this while loop should do it's thing as long as both expressions are true. If just one of them becomes false, the loop should end. However, if the enemy's HP is lower than zero, it will not break out. But if the player's HP is lower than zero, it breaks.
Every iteration of the loop is printing these values, so I can keep track of them.
Am I simply not understanding the logic of this? If you need to see any additional code, please let me know.
You're using a logical OR, which is true if at least one parameter is true. In other words, the truth table is this:
In | In | Out
T | T | T
T | F | T
F | T | T
F | F | F
It sounds like you want a logical AND, which is true if both parameters are true. This way, the fight continues as long as both the player and enemy have over 0 health. The operator for logical AND in C (and many other languages) is &&.
In case you're interested, here's the truth table for AND:
In | In | Out
T | T | T
T | F | F
F | T | F
F | F | F
While both the grunt's hp and the player's hp (true) are above 0 you want the loop to continue so you want:
while(grunt->hp > 0 && player->hp > 0)
If either the grunt's hp or the player's hp goes below 0 the condition above will be false and you will exit the loop.
If in your head you were thinking "I want to end the game when the player's HP or the enemy's HP is below or equal to zero", the right while condition would be:
while(!(grunt->hp <= 0 || player->hp <= 0))
With the logical inverse operator !
Recall: !(A || B) == !A && !B so other guys answer is strictly equivalent.
Sometimes an abstraction can make things easier to grasp.
Consider if you had:
#define Alive(who) ((who)->hp > 0)
then you could write:
while (Alive(grunt) && Alive(player)) {
…
}

Explain 1-liner party of a US President from IOCCC 2013

I found this code on ioccc and I have trouble even beginning to understand how it works!
void main(int riguing, char** acters) {
puts(1[acters-~!(*(int*)1[acters]%4796%275%riguing)]);
}
An explanation of how this is valid code and how it actually works would be fantastic!
First, in C (and C++) k[pointer] and pointer[k] mean exactly the same thing, which is *(k + pointer) and *(pointer + k), respectively. Code obfuscators often seem to like to use the first version because many people find it unusual. But it's reasonably obvious that pointer + k and k + pointer are the same computation.
The other little diversion in the snippet is the use of
pointer-~!(something)
which is exactly the same as
pointer + (something == 0 ? 2 : 1)
How this works:
The ! operator turns any true (non-zero) value into 0 and the false (0) value into a boolean true (1):
!something: something == 0 ? 1 : 0
The ~ operator is bitwise inverse, so it turns 0 into the number consisting of all 1 bits, which is -1 and 1 into the number consisting of all 1 bits except the last bit, which is -2. See Wikipedia article on two's complement.
~!something: something == 0 ? -2 : -1
Subtracting that from something is the same as adding the negative (a - -b == a + b)
a-~!something: something == 0 ? a + 2 : a + 1
Finally
1[a-~!something]: something == 0 ? a[3] : a[2]
So it selects either the second or third command line argument based on whether some computation is zero or not.
So now we need to decipher "some computation". We start with the type-punning operator *(T *)(pointer), in this case *(int*)(char*), reads out whatever the pointer points to as though it were a T. So in this case, it reads the first sizeof(int) characters from 1[acters] -- that is, from the first command-line argument (argv[1]) -- as though they were the internal representation of an integer. That will code every president as an integer based on the first four characters of their surname.
While there have been several repeated presidential surnames in US history, it's not a problem as long as the two presidents with the same name shared a political party.
One such pair, the father and son John Adams, Jr. (a Federalist), and John Quincy Adams (elected to the senate as a Federalist and to the presidency as a Democratic-Republican), are eliminated as being prior to the first valid president (Franklin Pierce), as is the elder Harrison (William Henry, a Whig) whose grandson Benjamin was elected as a Republican. The father and son George H.W. and George W. Bush are both Republicans. And the two Johnsons, Andrew and Lyndon Baines (as far as I know, not related to each other), were both Democrats.
So that leaves only the two Roosevelts, Theodore (Republican) and Franklin Delano (Democrat). The great-great-great-great-grandfathers of the two Roosevelt presidents were the brothers Johannes and Jacobus, sons of Nicholas Roosevelt (or Nicholas van Rosenvelt) (1658-1742) and grandsons of the Dutch immigrant Claes Maartenszen Van Rosenvelt, making them fifth cousins. However, the presidents were more closely related to each other through Eleanor Roosevelt, Theodore's niece and FDR's wife. In order for the IOCCC entry to work, it's necessary to represent the younger Roosevelt as "fdr", as he was commonly known.
So that only leaves (integer)%4796%275%riguing, or (integer)%4796%275%4, since riguing (aka argc) is 4. That's a simple hash function, which I imagine was discovered by trial and error using the list of presidential surnames and their affiliations.

Theory of Computation: Design a 2-stack PDA for this language?

Consider a language L2 = { ak bk ck | k >= 0 }.
(k should be superscript)
Design a 2-stack PDA for the language L2.
Could anyone give me some guidance on how to go about doing this?
Try solving the case with a 1-stack PDA and the language { aⁿ bⁿ | n >= 0 } - that should expose how to solve { aⁿ bⁿ cⁿ| n >= 0 } using an additional stack.

Resources