Update value in same view using CASE expression

I am creating a view where i have daily data of different items and each item has a state. I created a column that shows what the column was yesterday using

LAG(state) OVER(PARTITION BY item_id ORDER BY currentdate) AS yesterday_state 

Now I want to count how long the state has been the same value and im doing so with this:

COUNT (CASE WHEN state = yesterday_state THEN state ELSE NULL END) OVER(PARTITION BY item_id ORDER BY currentdate AS state_age 

This is working properly but I need to find a way to set the value back to 0 when state != yesterday_state

This all is happening inside of SELECT statement as I’m creating a view. How could I go around doing this so that the state_age sets to 0 when state is not the same value as yesterday_state

Regular Expression Kleene Star application – using 0s and 1s

Have a question from an online course I am trying to take for a certificate. Came across a few questions like this and need help working through the problem and the rationale to get the solution. Any help appreciated.

Consider the regular expressions R1 = (01)*(0*1*)(10) and R2 = (101)(01). Which of following is true?

10 in R1 and 101 R2

10 in R1 and 1000 R2

010 in R1 and 100 R2

010 not in R1 and 100 not in R2

McNaughton-Yamada Algotihm (1960) Regular Expression to DFA without passing through NFA

I have a homework problem here. It asks me to use the McNaughton-Yamada algorithm to first convert a regular expression to a DFA, and then minimize this using a partition argument. I can do the latter. My problem is that I cannot access any actual reference on the algorithm. Their original paper is behind a paywall at IEEE that my university does not have access to.

The algorithm went something like this: 1. For each symbol in the expression, given them a subscript from left to right increasing by one for each instance of that symbol. For example, the expression, aa* would receive a_1 a_2^*.

  1. We proceed to construct a diagram based on the possible lengths of words.

If done appropriately, this produces a DFA. I think the labeling in (1) is to help label the states.

Feel free to come up with your own example if you decide to give an answer. I won’t provide any problem here because there is no guarantee that it isn’t actually my homework exercise.

Lambda Expression Reduction

I am unable to solve the following lambda expression using both normal order (Call-by-name) and applicative order (Call-by-value) reduction. I keep getting different answers for both. This is the lambda expression that has to be reduced using both techniques:

(λfx.f (f x)) (λfx.f (f x)) f x

Plot integral expression

Trying to plot in Mathematica with an integral as the iterator

Plot[x, {Integrate[1/Sqrt[0.31*x + 0.68*x^4 + 0.01*x^2], x], 0, 10^7}] 

But get the error that it the integral can’t be used as an iterator. On the other hand if I try

Plot[Integrate[1/Sqrt[0.31*x + 0.68*x^4 + 0.01*x^2], x], {x, 0, 10^7}] 

I get the ‘invalid integration variable or limit’ error.

Full adder carry expression

I’m learning about logic circuits and I’ve come across full adder. In the book they derived its two carry out expressions –

Cout = x&&y || x&&z || y&&z and Cout = x&&y || (x’&&y || x&&y’)&&z

I’ve tried to get the second equation from the first one but couldn’t. Any one knows how to do that?

Convert CFG to CNF Arithmatic Expression

Convert CFG to CNF

The Grammar

E→E+T E→T T→T*F T→F F→(E) F→x 

Step 1 Assign variables to terminals

A→ + B→ * C→( D→ ) F→x 

Step 2

Remove epsilon which in this grammar is not available 

Step 3

Remove useless symbols which there are none 

Step 4 Remove unit rule

S→E E→T T→F So we have E→T*F|E+T T→(E)|x F→(E)|x 

Step 5 Add a start symbol

    S→T*F|E+T E→T*F|E+T T→(E)|x F→(E)|x A→ + B→ * C→( D→ ) F→x 

Step 6 Convert in the form A→BC and A→a Until now we have

S→T*F|E+T E→T*F|E+T T→(E)|x F→(E)|x A→ + B→ * C→( D→ ) F→x 

Using terminal variables we get

S=EAT|TBF T→CED|x E→EAT|TBF F→CED|x A→ + B→ * C→( D→ ) Let  A_1=AT B_1=BF E_1=ED 

So final CNF is

S=EA_1 |TB_1 T→CE_1 |x E→EA_1 |TB_1 F→CE_1  | x A→ + B→ * C→( D→ ) F→x A_1=AT B_1=BF E_1=ED 

But after converting I cannot deduce this string from gammar


THanks Rahman

Converting DFA to Regular Expression Using State Removal

I’m trying to convert the following NFA to a regular expression.

I’ve attached my work below and end up with the expression $ aa^*bb^*$ . As far as I can tell, this doesn’t seem correct but I’ve been working at it for quite a while. Can anyone tell me where I went wrong? And if it happens to be correct, can you tell me why?

Thanks a lot in advance.


Upon further work, I came up with the regular expression: $ aa^*b(b\cup aaa^*b)^*$ . This seems like the correct response.

How to do an arbitrary expression growth in Mathematica?

I want to write a function that can be used to grow an expression from a seed or another existing expression based on part specification.


So if I start from a seed say "START" like follows:

expr = growExpression["START",{{1,2,3}->"B",{2,1}->"A"},Null] 


Note I want it to accept a parameter which can be used to decide unspecified but necessary growth to support the rest of the structure. Next, if I now take this expression as a seed and do further growth as follows:

expr = growExpression[expr,{{1}->"C",{1,2,2,4}->"D",{1,2,1}->"A",{2,1}->"E"},f] 


Note even if non-leaves are replaced the expression doesn’t shrink, i.e. {1}->"C" is basically equivalent to {1,0}->"C".

Is there a builtin function that can help to achieve this behavior without side effects?

In an NFA that recognizes L(ab) where ab is a regular expression, what is the point of the empty string transition?

I’m reading Introduction to Computational Theory by Michael Sipser. He provides the NFA below as one that recognizes the language of the regular expression $ ab$ .

What is the purpose of the middle two states and the $ \varepsilon$ transition between them? Could this NFA be designed without them and still work as intended?


Here’s another example of an NFA that recognizes the language described by $ aba$ . Again, I’m confused by the addition of the empty string transitions and the extra states. enter image description here