Formula to find&replace part of a string - google-sheets

Column A contains the string you want to edit.
Column C contains a string to find some word in the string in column A.
Column D contains the string that replaces that string.
Results are returned in column F.
This is the result I want.
a
b
c
d
e
f
result
its test. apple
apple
A
its test. A
its test. banana
banana
B
its test. B
apple & banana
...
...
A & B
banana & banana
B & B
its test. apple
its test. A
its test. banana
its test. B
I used this formula.
=ARRAYFORMULA(VLOOKUP(A2:A, C2:D, 2,TRUE))
and i got this wrong.
a
b
c
d
e
f
result
its test. apple
apple
A
B
its test. banana
banana
B
B
apple & banana
...
...
A
banana & banana
B
its test. apple
B
its test. banana
B
I want to return only some of the strings have changed. If there are several texts to change in one string, I want to change them all.

You may try:
=REDUCE(A:A,C:C,LAMBDA(a,c,IF(c="",a, INDEX (REGEXREPLACE(a,c,OFFSET(c,0,1))))))
It's a basic REGEXREPLACE, but with REDUCE lets you scan the different words to be replaced
With Substitute:
=REDUCE(A:A,C:C,LAMBDA(a,c,IF(c="",a, INDEX (SUBSTITUTE(a,c,OFFSET(c,0,1))))))

Related

How do you write an n-ary tree in postfix notation?

I am trying to understand this paper, Tree template matching in ranked ordered trees by pushdown automata. The first step is having the tree in postfix notation.
How do I take a tree such as this:
foo
bar
abc
def
bar
abc
a
b
a
b
c
d
e
def
abc
baz
bar
abc
a
b
c
abc
def
And write that in postfix notation?
It doesn't make a lot of sense. However, you can either use parentheses:
...(abc a b c)bar abc def)baz)foo
Or specify the number of operands with each operator:
... abc a b c bar4 abc def baz3 foo3
or even:
... abc0 a0 b0 c0 bar4 abc0 def0 baz3 foo3
In the terms of that paper, the tree you are asking about is impossible because you have nodes with the same "symbol" (name) with different numbers of children. The paper, however, is assuming that every symbol in the alphabet has a specified "arity" (the number of children for a node labelled with that symbol). Leaf symbols have arity 0, by the way.
This is (very briefly) mentioned in the Basic Definitions section at the beginning:
A ranked alphabet is a couple 𝒜 = (Σ, φ), where Σ is an alphabet and φ is a mapping . The arity (rank) of a symbol x ∈ Σ is φ(x).
In other words, there is a mathematical function which tells you how many children a labelled node will have, which you can use in the postfix notation to know how many subtrees precede that symbol. (Note also that 𝒜, which includes the arity function, is part of their definition of a PDA.)

How to repeat row range N times in Google Sheets

Basically, I have N rows with one unique value always repeating three times. This is col_1. Then I have a range of values I want repeated as many times there are unique values in col_1. This needs to be dynamic, since col_1 is automatically generated from a list.
col_1 | values
------- ------
a | d
a | e
a | f
b |
b |
b |
c |
c |
c |
So this is what I want to end up with:
col_1 | col_2
----------------
a | d
a | e
a | f
b | d
b | e
b | f
c | d
c | e
c | f
Edit: as a note in comment, my data is completely dynamic so I can't have any assumptions about how many rows there will be. In here I have a list of [a,b,c], multiplied by as many times there are items in Values, so [a,b,c] & [d,e,f] results in 9 rows. If I add "g" to [d,e,f], I then have 12 rows and if I then add "h" to [a,b,c] I would have 16 rows. The dynamic part is the important bit in here.
So I want to answer my own question, because I spend way too long for looking the answer and couldn't find one, so I just came up with one by myself. So here's the answer:
=ArrayFormula(TRANSPOSE(SPLIT(REPT(CONCATENATE(C2:C4&"~"),COUNTA(UNIQUE(A2:A500))),"~")))
You can just copy and change the ranges for it to work, but let me explain how does it work.
First we combine the values we want to repeat into one string with CONCATENATE. The three values are defined in the range of C2:C4.
CONCATENATE(C2:C4&"~") → "d~e~f~"
~ is used here as a delimiter, so there's no any special tricks in here. Next we repeat this string we just made as many times as there are unique values in col_1. For this we use a combination of COUNTA, UNIQUE and REPT.
COUNTA(UNIQUE(A2:A500)) ← Count how many unique occurrences there are in a range ( 3 )
REPT(CONCATENATE(C2:C4&"~"),COUNTA(UNIQUE(A2:A500))
Basically this is converted into:
REPT("d~e~f~",3) → "d~e~f~d~e~f~d~e~f~"
Now we have as many d, e and f as we want. Next we need to turn them into cells. We'll do this with a combination of SPLIT and TRANSPOSE.
TRANSPOSE(SPLIT(REPT(CONCATENATE(C2:C4&"~"),COUNTA(UNIQUE(A2:A500))),"~"))
We split the string from "~" so we'll end up with an array looking like [d,e,f,d,e,f,d,e,f]. We then need to transpose it to turn it into rows instead of columns.
Last part is to wrap everything into an arrayformula, so the formula actually does work.
=ArrayFormula(TRANSPOSE(SPLIT(REPT(CONCATENATE(C2:C4&"~"),COUNTA(UNIQUE(A2:A500))),"~")))
Now the array will look like:
col_1 | col_2
----------------
a | d
a | e
a | f
b | d
b | e
b | f
c | d
c | e
c | f
Now any time you add a new unique value to col_1, three new values are added
There is a new function that we discovered on the Google Product forums due to a user's post. That function is called FLATTEN().
in your scenario, this should work:
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(A2:A&"|"&TRANSPOSE(C2:C4)),"|",0,0),"where Col1<>''"))

Polish to infix notation

Lets say we have an expression in prefix notation or(1) and A B or(2) or(3) C D E (where A, B, C, D, E are boolean values and or numbered for convenience) that we want to convert to an infix notation. In principle I have two ways to evaluate it:
(1) start at or(3) C D, then or(2), then and, then or(1)
(2) start at and A B then check or(3), or(2). Lastly check or(1)
(1) Evaluate starting from right most operator
(2) Evaluate starting from left most operator having all operands as it's direct neighbors.
Both evaluations yield (A and B) or C or D or E.
Which evaluation sequence is correct?
Will these two evaluations ever give different result for the same prefix record?
http://www.cs.man.ac.uk/~pjj/cs212/fix.html recommends the first method.
You will get the same result regarding of the order, so it is up to you.

ASIC design - combinational logic

I had an interview a few days back and this was the question they asked me in one of the rounds:
A mux which gives output a when select line is 1 and b when it is 0. The output is represented as C. This has to be implemented with and and not gates only.
I got it almost but was not sure how the output is received finally in combinational ckt. It is a very simple thing I missed here. I gave the select line to an and gate and input a and the select line and the input b through a not gate to another and gate so I got the output as A and B but how can we have one output alone? either a or b?
C = (A and not(S)) or (B and S)
= not(not(A and not(S)) and not(B and S))
A input 1,
B input 2,
S input select line,
C output,
C = (A & S) | (A & ~S);
Its simple bro!

Match 2 columns, but bring along all associated rows from the second column

What is the easiest way for Example1 to be converted to Example2 (I would be doing this with much longer lists)? Column C and D shall be associated to Col B for the output of Example2. This is not just to make Col B replicate Col A, although that is part of the solution. Thank you in advance!
Example1:
Col A Col B Col C Col D
a e d c
l l o a
e x g t
x a s s
Example2:
Col A Col B Col C Col D
a a s s
l l o a
e e d c
x x g t
It is not totally clear what you want to achieve and what the data qualities are, so a few assumptions:
all items in Col A are also in Col B
items in Col A are unique
Consider the following screenshot. Column A has been copied into column F. The formula in G1 is
=INDEX(B$1:B$4,MATCH($F1,$B$1:$B$4,0))
Copy the formula across to I1 and then copy G1 to I1 down.
If that does not do what you need, please edit your question, add a better data sample and more explanation.

Resources