How to flip a range horizontally? - google-sheets

I have this google sheets input table
How to flip the range A2:E range horizontally with Arrayformula because the range A1:E is growing.
Input Output
c a t t a c
b i r d d r i b
h o r s e e s r o h
t i g e r r e g i t

The old way
=TRANSPOSE(SORT(TRANSPOSE(A2:E),SEQUENCE(ROWS(
TRANSPOSE(A2:E))),0))
The new way
=LAMBDA(range,
TRANSPOSE(SORT(range,SEQUENCE(ROWS(range)),0)))(TRANSPOSE(A2:E))
Used formulas help
TRANSPOSE - SORT - SEQUENCE - ROWS - LAMBDA

Related

Check if cell is contained in group of cells to add its value to another cell

I have a Google Sheets document that looks like this:
A B C D
1
2 X X 3
3 Z Y 2
4 W Z 6
5
How can I do so that if the element in column A is one of the elements of column B, its corresponding value in column C is added to column D? In this case, column D must have this values:
D2 = 3, because X is in column B
D3 = 2, because Z is in column B
D4 = 0, because W is not in column B
Many thanks in advane!
use:
=INDEX(IF(A2:A="",,IFNA(VLOOKUP(B2:B,
FILTER(B2:C, COUNTIF(B2:B, A2:A)), 2, 0))*1))
You could use:
=INDEX(IF(A2:A="",,COUNTIF(B2:B,A2:A)*C2:C),,)

LaTeX, sloped boxes

I'd like to make a Find-A-Word for the library wall.
The solution (also for the wall) requires a box, enclosing the word.
Horizontal/vertical boxes are no problem. Sloped (slanted?) boxes are what's the problem.\I envisage a command like
\makebox (length, breadth, angle, co-ordinates of left-lower corner)
It may be that this has been done before.
Has anyone any suggestions?
William.
Using tikz and the tikzmarks library:
\documentclass{article}
\usepackage[hmargin=4cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\ttfamily
\noindent
\tikzmark{a:start}w d e w e r n \tikzmark{b:start}b v c w o i q\tikzmark{b:stop} v o i q t u h n r g j q v r o e q i o n j v k w
o q p i n t o j v k m o q e i n g k m f o r q e p i n k f m p i g n o j k m
f \tikzmark{c:start}v e p o q i o n j r g k m l e v q n b g j k v m e q n b o j g k v l m e q
n b j g f k l e m v q n j g k e m l v q n g j r f e l v q n j g f l k q g j
n\tikzmark{a:stop} v q e\tikzmark{c:stop} n p m k w g j k n e k e m l v q n g j r f e l v q n j g f l k q g j
\begin{tikzpicture}[remember picture, overlay]
\draw[red] ([shift={(0,1.5ex)}]pic cs:a:start) rectangle (pic cs:a:stop);
\draw[red] ([shift={(0,1.5ex)}]pic cs:b:start) rectangle (pic cs:b:stop);
\draw[red,rotate=45] ([shift={(1.5ex,1.5ex)}]pic cs:c:start) rectangle ([shift={(-0.5ex,0ex)}]pic cs:c:stop);
\end{tikzpicture}
\end{document}

Ruby - How to remove only 1 whitespace from string

I try to remove 1 whitespace from this string:
m y r e a l n a m e i s d o n a l d d u c k
Expected result:
my real name is donald duck
My code are:
def solve_cipher(input)
input.split('').map { |c| (c.ord - 3).chr }.join(' ') # <- Here I tried everything
end
puts solve_cipher('p| uhdo qdph lv grqdog gxfn')
# => m y r e a l n a m e i s d o n a l d d u c k
I tried everything to solve my problem, example:
input.....join(' ').squeeze(" ").strip # => m y r e a l n a m e...
or
input.....join.gsub(' ','') # => myrealnameisdonaldduck
or
input.....join(' ').lstrip # => m y r e a l n a m e...
and so on...
Well, you could split the string into words first, then split each word into characters. Using the same method you used in your code, it could look like this.
def solve_cipher(input) input.split(' ').map{ |w| w.split('').map { |c| (c.ord - 3).chr}.join('')}.join(' ') end
When joining the characters in the same word, we put no space between them; when joining the words together we put one space between them.
As stated in the question, you are using Rails, so you can also try squish method:
def solve_cipher( input )
input.split(' ').map(&:squish).join(' ')
end
str = "m y r e a l n a m e i s d o n a l d d u c k"
str.gsub(/\s(?!\s)/,'')
#=> "my real name is donald duck"
The regex matches a whitespace character not followed by another whitespace character and replaces the matched characters with empty strings. (?!\s) is a negative lookahead that matches a whitespace.
If more than two spaces may be present between words, first replace three or more spaces with two spaces, as follows.
str = "m y r e a l n a m e i s d o n a l d d u c k"
str.gsub(/\s{3,}/, " ").gsub(/\s(?!\s)/,'')
#=> "my real name is donald duck"
I know that it is not a fancy way of doing it but you could just try to create a new string and have a function traversal(input) with a counter initiated at 0, that would return this new string.
It would go through your input (which is here your string) and if the counter is 0 and it sees a space it just ignores it, increments a counter and go to the next character of the string.
If the counter is different of 0 and it sees a space it just concatenates it to the new string.
And if the counter is different of 0 and it sees something different of a space, it concatenates it to the new string and counter equals 0 again.
The trick is to use a capture group
"m y r e a l n a m e i s d o n a l d d u c k".gsub(/(\s)(.)/, '\2')
=> "my real name is donald duck

semantic web rule use "all"

Assume that I have the following statements:
A p B, A p C, B p C ( p is a symmetric property, i.e. B p A, C p A and C p B)
A v 2, B v 1, C v 1,
I want to use a rule to do something like:
?a p all(?b)
if ?b v 1
than ?a q 'Yes'
that means that you can infer (A q 'Yes'), but B can't since B p A and A v 2(although B p C and C v 1).
[rule: (?a eg:p ?b), (?b eg:v 1) -> (?a eg:q 'Yes')]
I've used the above rule in Jena, but I got A,B,C eg:q 'Yes', which is wrong.
Any help will be greatly appreciated.
Update (originally posted as an answer)
the meaning of (?a p all(?b)) is that I like to get a set which all ?mem in this set fulfill the (?a p ?mem). And all member must fulfill (?mem v 1) to infer (?a q 'Yes').
For example,
A p B and A p C,so I get a set which contains (B, C).since B and C v 1,so A q 'Yes.
B p A and B p C,so I get a set(A, C),but A v 2,so can't infer that B q 'Yes'.
Problem Solved
Thanks to Joshua Taylor.
Firstly, these two rules can't use at the same time.The rule2 should be used after rule1.
And, the rule2 should be [rule2: (?s ?p ?o) noValue(?s, connectedToNonOne) -> (?s q 'Yes')].
but I got A,B,C eg:q 'Yes', which is wrong.
The rule you have actually written in Jena says
For any two individuals X and Y, if (X p Y) and (Y v 1) then (X q 'Yes').
From the rule you've written, this is correct, by:
(A p C), (C v 1) &rightarrow; (A q 'Yes')
(B p C), (C v 1) &rightarrow; (B q 'Yes')
(C p B), (B v 1) &rightarrow; (C q 'Yes')
What you're actually trying to say is:
For any individual X, if for every individual Y, (X p Y) implies (Y v 1), then (X q 'Yes').
In first order logic, your original rule could be written as:
∀ x,y ([p(x,y) &wedge; v(y,1)] &rightarrow; q(x,'yes')
What you're actually trying to capture would be:
∀x[(∀y[p(x,y) &rightarrow; v(y,1)]) &rightarrow; q(x,'yes')]
That's harder to capture in Jena rules, because to check whether (∀y[p(x,y) &rightarrow; v(y,1)]) holds or not, all Jena can do is check whether there are currently any counterexamples. If one were added later, you might have incorrect inferences.
Using the builtins available in the rule reasoner, you could do something with noValue and notEqual along the lines of:
#-- If an individual is disqualified by being
#-- connected to a something that is connected
#-- to something that is not equal to 1, then
#-- add a connectedToNonOne triple.
[rule1:
(?x p ?y), (?y v ?z), notEqual(?z,1)
->
(?x connectedToNonOne true)]
#-- Mark everything that is *not* disqualified
#-- with `q 'Yes'`.
[rule2:
noValue(?x, connectedToNonOne)
->
(?x q 'Yes')

Follow sets Top-Down parsing

I have a question for the Follow sets of the following rules:
L -> CL'
L' -> epsilon
| ; L
C -> id:=G
|if GC
|begin L end
I have computed that the Follow(L) is in the Follow(L'). Also Follow(L') is in the Follow(L) so they both will contain: {end, $}. However, as L' is Nullable will the Follow(L) contain also the Follow(C)?
I have computed that the Follow(C) = First(L') and also Follow(C) subset Follow(L) = { ; $ end}.
In the answer the Follow(L) and Follow(L') contain only {end, $}, but shouldn't it contain ; as well from the Follow(C) as L' can be null?
Thanks
However, as L' is Nullable will the Follow(L) contain also the Follow(C)?
The opposite. Follow(C) will contain Follow(L). Think of the following sentence:
...Lx...
where X is some terminal and thus is in Follow(L). This could be expanded to:
...CL'x...
and further to:
...Cx...
So what follows L, can also follow C. The opposite is not necessarily true.
To calculate follows, think of a graph, where the nodes are (NT, n) which means non-terminal NT with the length of tokens as follow (in LL(1), n is either 1 or 0). The graph for yours would look like this:
_______
|/_ \
(L, 1)----->(L', 1) _(C, 1)
| \__________|____________/| |
| | |
| | |
| _______ | |
V |/_ \ V V
(L, 0)----->(L', 0) _(C, 0)
\_______________________/|
Where (X, n)--->(Y, m) means the follows of length n of X, depend on follows of length m of Y (of course, m <= n). That is to calculate (X, n), first you should calculate (Y, m), and then you should look at every rule that contains X on the right hand side and Y on the left hand side e.g.:
Y -> ... X REST
take what REST expands to with length n - m for every m in [0, n) and then concat every result with every follow from the (Y, m) set. You can calculate what REST expands to while calculating the firsts of REST, simply by holding a flag saying whether REST completely expands to that first, or partially. Furthermore, add firsts of REST with length n as follows of X too. For example:
S -> A a b c
A -> B C d
C -> epsilon | e | f g h i
Then to find follows of B with length 3 (which are e d a, d a b and f g h), we look at the rule:
A -> B C d
and we take the sentence C d, and look at what it can produce:
"C d" with length 0 (complete):
"C d" with length 1 (complete):
d
"C d" with length 2 (complete):
e d
"C d" with length 3 (complete or not):
f g h
Now we take these and merge with follow(A, m):
follow(A, 0):
epsilon
follow(A, 1):
a
follow(A, 2):
a b
follow(A, 3):
a b c
"C d" with length 0 (complete) concat follow(A, 3):
"C d" with length 1 (complete) concat follow(A, 2):
d a b
"C d" with length 2 (complete) concat follow(A, 1):
e d a
"C d" with length 3 (complete or not) concat follow(A, 0) (Note: follow(X, 0) is always epsilon):
f g h
Which is the set we were looking for. So in short, the algorithm becomes:
Create the graph of follow dependencies
Find the connected components and create a DAG out of it.
Traverse the DAG from the end (from the nodes that don't have any dependency) and calculate the follows with the algorithm above, having calculated firsts beforehand.
It's worth noting that the above algorithm is for any LL(K). For LL(1), the situation is much simpler.

Resources