I have a truth table like this:
a b sel O
F F F F
F F T F
F T F F
F T T T
T F F T
T F T F
T T F T
T T T T
so O is the output column, sel is basically a selector. So when sel = F, the O will be the value of a, and if sel= T, the O will be the value of b.
So I was able to come up with an expression (without regarding b input) that correctly matches the output of sel and a, when sel = F:
a $$\lor$$ sel (for example you could check that this expression $$a v sel$$ would produce correctly all the combination of values of a, sel and O, without regarding the value of b)
And similarly for matching output of sel and b, when sel = T:
$$b ^ sel$$ (for example you could check that this expression $$b ^ sel$$ would produce correctly all the combination of values of b, sel and O, without regarding the value of a)
But now I am not sure how to come up with an expression that would correctly put together $$a v sel$$ and $$b ^ sel$$ to have a final expression that matches the truth table above.
(!s && a) || (s && b)
If s is false, the left side takes the value of a, and the right side is false (and thus ignored).
If s is true, the right side takes the value of b, and the left side is false (and thus ignored).
Related
In Graphql, I can create a union such as the following:
union SearchResult = Book | Movie
Is there a way I can do this for plain strings? Something like this:
union AccountRole = "admin" | "consumer"
I am afraid you cannot do that because it is what defined by the specification.
From the union syntax mentioned at specification here , the part that you want to change should follow the Names syntax , which the first character is only allow to be upper case letter, lower case latter or _
(i.e. the characters set as follows)
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m
n o p q r s t u v w x y z _
Given the following tree which is a sort of Backus-Nauf form like notation where
| indicates or (so B is F or G) and [] indicate optional (so H is optional)
Def: A B C
A: D E
B: F | G
C: [H] I
D: a b
E: c d
F: e f
G: g h
H: i j
I: k l
a: a
b: b
c: c
d: d
e: e
f: f
g: g
h: h
i: i
j: j
k: k
l: l
Which can be viewed as
Def
A B C
D E F | G [H] I
a b c d e f g h i j k l
I need to walk the tree extracting the leaf nodes and convert to the following tree which gives the possible routes
Def
a
b
c
d
e
f
i
j
k
l
k
l
g
h
i
j
k
l
k
l
So the possible paths are
abcdefijkl
abcdefkl
abcdghijkl
abcdghkl
I've a repo with a failing C# unit test (that sets up the tree and calls a basic recusive walker) that should hopefully clarify what I'm trying to achieve.
What I can't figure out is how to branch at optional and alternative nodes while maintaining the correct leaves to append subsequent leaves to.
A non-recursive breadth-first search would probably look something along these lines (pseudo code). Kicked off by calling findAllLeafPaths(Def):
var allPathsFound = {}
function findAllLeafPaths(startNode) {
var tokenSequenceQueue = {
createTokenSequenceFrom(startNode)
}
while (tokenSequenceQueue.isNotEmpty()) {
var tokenSequence = tokenSequenceQueue.removeFirst()
var allLeaves = true
for (every token T in tokenSequence) {
if isLeafNode(T)
continue
else if T's rule is "T: Y Z" {
allLeaves = false
tokenSequenceQueue.append(tokenSequence.replace(T, Y + Z))
} else if T's rule is "T: [Y] Z" {
allLeaves = false
tokenSequenceQueue.append(tokenSequence.replace(T, Y + Z))
tokenSequenceQueue.append(tokenSequence.replace(T, Z))
} else if T's rule "T: Y | Z" {
allLeaves = false
tokenSequenceQueue.append(tokenSequence.replace(T, Y))
tokenSequenceQueue.append(tokenSequence.replace(T, Z))
}
}
if (allLeaves) {
allPathsFound.add(tokenSequence)
}
}
}
Here is also a recursive depth-first version. I prefer the first one because the recursion puts your stack at the mercy of the max possible length of result paths:
var allPathsFound = {}
function toLeafNodes(tokenSequence) {
var allLeaves = true
for every token T in tokenSequence {
if isLeafNode(T)
continue
else if T's rule is "T: Y Z" {
allLeaves = false
toLeafNodes(tokenSequence.replace(T, Y + Z)
} else if T's rule is "T: [Y] Z" {
allLeaves = false
toLeafNode(tokenSequence.replace(T, Y + Z)
toLeafNode(tokenSequence.replace(T, Z)
} else if T's rule "T: Y | Z" {
allLeaves = false
toLeafNode(tokenSequence.replace(T, Y)
toLeafNode(tokenSequence.replace(T, Z)
}
}
if (allLeaves) {
allPathsFound.add(tokenSequence)
}
}
[Edit] The non-recursive version currently does one replace at a time and immediately puts the result sequence on the queue. It can be further optimized to do all possible replaces in one pass.
There is another way to build a tree from your definitions. Consider:
Def: A B C
A: D E
B: F | G
C: [H] I
Start with
A
\
B
\
C
Then replace A with D E:
D
\
E
\
B
\
C
Do the same thing with B (replace with F | G), and C (replace with [H] I), and you get:
D
\
E
/ \
F G
/ \ / \
I H I H
\ \
I I
Now, if you do a recursive depth-first traversal of that tree, you get the valid strings:
D E F I
D E F H I
D E G I
D E G H I
And you can replace D with "a b", etc. when you're outputting the strings.
I showed it step-by-step, but you can do it all in a single pass.
I am given 2 DFAs. * denotes final states and -> denotes the initial state, defined over the alphabet {a, b}.
1) ->A with a goes to A. -> A with b goes to *B. *B with a goes to *B. *B with b goes to ->A.
The regular expression for this is clearly:
E = a* b(a* + (a* ba* ba*)*)
And the language that it accepts is L1= {w over {a,b} | w is b preceeded by any number of a's followed by any number of a's or w is b preceeded by any number of a's followed by any number of bb with any number of a's in middle of(middle of bb), end or beginning.}
2) ->* A with b goes to ->* A. ->*A with a goes to *B. B with b goes to -> A. *B with a goes to C. C with a goes to C. C with b goes to C.
Note: A is both final and initial state. B is final state.
Now the regular expression that I get for this is:
E = b* ((ab) * + a(b b* a)*)
Finally the language that this DFA accepts is:
L2 = {w over {a, b} | w is n 1's followed by either k 01's or a followed by m 11^r0' s where n,km,r >= 0}
Now the question is, is there a cleaner way to represent the languages L1 and L2 because it does seem ugly. Thanks in advance.
E = a* b(a* + (a* ba* ba*)*)
= a*ba* + a*b(a* ba* ba*)*
= a*ba* + a*b(a*ba*ba*)*a*
= a*b(a*ba*ba*)*a*
= a*b(a*ba*b)*a*
This is the language of all strings of a and b containing an odd number of bs. This might be most compactly denoted symbolically as {w in {a,b}* | #b(w) = 1 (mod 2)}.
For the second one: the only way to get to state B is to see an a in A, and the only way to get to C from outside C is to see an a in B. C is a dead state and the only way to get to it is to see aa starting in A. That is: if you ever see two as in a row, the string is not in the language; the language is the set of all strings over a and b not containing the substring aa. This might be most compactly denoted symbolically as {(a+b)*aa(a+b)*}^c where ^c means "complement".
I have a generic relation A like this:
DUMP A;
(a, b)
(a, c)
(a, d)
(b, a)
(d, a)
(d, b)
See that there is the pair (a,b) and (b,a); but there isn't a pair for (d,b).
I want to filter those "unpaired" tuples out.
The final result should be something like:
DUMP R;
(a, b)
(a, d)
(b, a)
(d, a)
How can I write this on PIG?
I was able to solve with the following code, but the cross operation is too expensive:
A_cp = FOREACH L GENERATE u1, u2;
X = CROSS A, A_cp;
F = FILTER X BY ($0 == $3 AND $1 == $2);
R = FOREACH F GENERATE $0, $1;
This is the output of my DESCRIBE A ; DUMP A ;:
A: {first: chararray,second: chararray}
(a,b)
(a,c)
(a,d)
(b,a)
(d,a)
(d,b)
This is one way you could solve this:
A = LOAD 'foo.in' AS (first:chararray, second:chararray) ;
-- Can't do a join on its self, so we have to duplicate A
A2 = FOREACH A GENERATE * ;
-- Join the As so that are in (b,a,a,c) etc. pairs.
B = JOIN A BY second, A2 BY first ;
-- We only want pairs where the first char is equal to the last char.
C = FOREACH (FILTER B BY A::first == A2::second)
-- Now we project out just one side of the pair.
GENERATE A::first AS first, A::second AS second ;
Output:
C: {first: chararray,second: chararray}
(b,a)
(d,a)
(a,b)
(a,d)
Update: As WinnieNicklaus points out, this can be shortened to:
B = FOREACH (JOIN A BY (first, second), A2 BY (second, first))
GENERATE A::first AS first, A::second AS second ;
i have a string that random generate by a special characters (B,C,D,F,X,Z),for example to generate a following string list:
B D Z Z Z C D C Z
B D C
B Z Z Z D X
D B Z F
Z B D C C Z
B D C F Z
..........
i also have a pattern list, that is to match the generate string and return a best pattern and extract some string from the string.
string pattern
B D C [D must appear before the C >> DC]
B C F
B D C F
B X [if string have X,must be matched.]
.......
for example,
B D Z Z Z C D C Z,that have B and DC,so that can match by B D C
D B Z C F,that have B and C and F,so that can match by B C F
D B Z D F,that have B and F,so that can match by B F
.......
now,i just think about suffix array.
1.first convert a string to suffix array object.
2.loop each a pattern,that find which suffix array can be matched.
3.compare all matched patterns and get which is a best pattern.
var suffix_array=Convert a string to suffix array.
var list=new List();
for (int i=0;i<pattern length;i++){
if (suffix_array.match(pattern))
list.Add(pattern);
}
var max=list[0];
for (int i=1;i<list.length;i++){
{
if (list[i]>max)
max=list[i];
Write(list[i]);
}
i just think this method is to complex,that need to build a tree for a pattern ,and take it to match suffix array.who have a more idea?
====================update
i get a best solution now,i create a new class,that have a B,C,D,X...'s property that is array type.each property save a position that appear at the string.
now,if the B not appear at the string,we can immediately end this processing.
we can also get all the C and D position,and then compare it whether can sequential appear(DC,DCC,CCC....)
I'm not sure what programming language you are using; have you checked its capabilities with regular expressions ? If you are not familiar with these, you should be, hit Google.
var suffix_array=Convert a string to suffix array.
var best=(worst value - presumably zero - pattern);
for (int i=0;i<pattern list array length;i++){
if (suffix_array.match(pattern[i])){
if(pattern[i]>best){
best=pattern[i];
}
(add pattern[i] to list here if you still want a list of all matches)
}
}
write best;
Roughly, anyway, if I understand what you're looking for that's a slight improvement though I'm sure there may be a better solution.