Looping through a set of values in google sheets - google-sheets

I have 2 sets of data. One is tank names
Tank Name
A1
A2
B1
B2
and the next is ON/OFF Data
ON/OFF
0
0
1
1
0
1
0
1
1
0
1
0
1
Now the result I am looking is, when the ON/OFF is 1 then the first tank is to be mentioned: when it's 0, no tank to be mentioned. Once all the tanks are mentioned,then it should again start from the first tank ie A1..like this
Result expected
0
0
1 A1
1 A2
0
1 B1
0
1 B2
1 A1
0
1 A2
0
1 B1
You can check the google sheet here : https://docs.google.com/spreadsheets/d/1SP2SfA-bzzhHgfrvpyUIkeQfUykata0oHxyD-x69yxE/edit?usp=sharing
Hope to get some help to get this solved.
Thanks

You can use this formula entered in (say) D2 and pulled down:
=if(B2=1,index(A$2:A,mod(sum(B$2:B2)-1,4)+1),"")
or if you prefer, can go for an array formula entered in E2 instead:
=ArrayFormula(if(B2:B=1,vlookup(mod(sumif(row(B2:B),"<="&row(B2:B),B2:B)-1,4)+2,{row(2:5),A2:A5},2,false),""))

Related

how to generate a "serie" of numbers based on a value:

I am stuck with a little problem.
i would like to auto-generate a Serie of numbers(is this the right description?):
for example:
The input/value in A1 is 5
the OUTPUT in B1 should now be 1
in
in cell B2 - 2,
B3 - 3,
B4 - 4,
B5 - 5,
B6 - 1, (STARTING at one again)
B7 - 2
. . . and so on until end of range
if I then change the value in A1 to 7 for example it should now "count" from 1 to 7 and repeat until the end of the range again.
any hints available would be much appreciated
=ARRAYFORMULA(MOD(SEQUENCE(ROWS(B2:B),1,0),A1)+1)
this is creating an array of numbers from 0 to however many rows there are on the sheet, then taking the modulus of each number using the value of A1 as a divisor.
MOD() means "what's the remainder after dividing by [n]?" where N in your example case is 6 or 7 or whatever.

McCulloch-Pitts neuron NAND

A MP neuron of NAND can be constructed using the truth table below:
P Q P(and not)Q
1 1 0
1 0 1
0 1 0
0 0 0
The neuron that shows this:
Inputs:
P +2
Q -1
If the threshold is 2
This will give an output of Y=1
My professor seemed confused and didn't clarify why this isn't correct when it is (to the best of my knowledge). Did he make a mistake or have i got this wrong?
A solution would be great.
Side note: I have sketched out this neuron but cannot draw on this page (new to SO).
First of all NAND is not "and not" but "not and", the logical table is
P Q NAND(P,Q)
1 1 0
1 0 1
0 1 1
0 0 1
second of all, there is nothing hard about NAND nor your gate. The "only" problematic one is XOR (and nXOR).
P Q XOR(P,Q)
1 1 0
1 0 1
0 1 1
0 0 0
So:
single perceptron can easily represent both NAND(p,q) = NOT(AND(p,q)) as well as AND(p, NOT(q)) (which you call NAND).
the impossible to represent gate is XOR and its negation.

Quick way to determine the number of k-length paths from A to B in a dense complete graph

Given a complete dense graph (over 250.000 nodes) , what is the quickest way to determine the number of k-length paths from node A to B ?
I understand this is an old post, but I had the exact same question and could not find the answer.
I like to think of this problem as a "permutation without repetition", as the order of the nodes visited matters (permutation) and we aren't backtracking (no repetitions). The number of permutations without repetition is: n!/(n-r)!
For a complete graph with N nodes, there are N - 2 remaining nodes to choose from when creating a path between a given A and B. To create a path of length K, K-1 nodes must be chosen from the remaining nodes after A and B are excluded. Therefore, in this context, n = N - 2, and r = k - 1.
Plugging into the above formula yields:
(N-2)!/(N-K-1)!
Example: for N = 5, with nodes 0,1,2,3,4 the following paths are possible from 0 to 1:
0 1
0 2 1
0 2 3 1
0 2 3 4 1
0 2 4 1
0 2 4 3 1
0 3 1
0 3 2 1
0 3 2 4 1
0 3 4 1
0 3 4 2 1
0 4 1
0 4 2 1
0 4 2 3 1
0 4 3 1
0 4 3 2 1
This yields 1 path of length 1, 3 paths of length 2, 6 paths of length 3, and 6 paths of length 4.
This appears to work for any N>=2 and K<=N-1.
You can use basically dynamic programming: For each node Y and path length k, you can compute the number of paths from A to Y of length k if you know the number of paths from A to X of path length k-1 for all nodes X. Total complexity is O(KV), where K is the total path length you are trying to compute for and V is the number of vertices.

Using COUNTIFS on 3 different columns and then need to SUM a 4th column?

I have written this formula below. I do not know the correct part of this formula that will add the numbers I have in Column AB2:AB552. As it is, this formula is counting the number of cells in that range that has numbers in it, but I need it to total those numbers as my final result. Any help would be great.
=COUNTIFS(Cases!B2:B552,"1",Cases!G2:G552,"c*",Cases!X2:X552,"No",**Cases!AB2:AB552,">0"**)
Assuming you don't actually need the intermediate counts, the sumifs function should give you the final result:
=SUMIFS(Cases!AB2:AB552,Cases!B2:B552,1,Cases!G2:G552,"c",Cases!X2:X552,"No",Cases!AB2:AB552,">0")
Testing this with some limited data:
Row B G X AB
2 2 a No 10
3 1 c No 24
4 2 c No 4
5 1 c No 0
6 1 a Yes 9
7 2 c No 12
8 2 c No 6
9 2 b No 0
10 1 b No 0
11 1 a No 10
12 2 c No 6
13 1 c No 20
14 1 c No 4
15 1 b Yes 22
16 1 b Yes 22
the formula above returned 48, the sum of AB3, AB13, and AB14, which were the only rows matching all 4 criteria

Pandas stack unstack pivot hierarchical index - reshape dataframe

I have massaged a dataframe so it looks like this:
123
456
789
0AB
CDE
FGH
...
,,,
I would like to transform it, so it looks like this:
123789CDE...
4560ABFGH,,,
The pattern is this:
123 789 CDE ...
456 0AB FGH ,,,
That is, I take two rows and concatenate the next two rows, etc, so I get a wide dataframe.
But my real dataframe is not three columns, it is maybe 50 columns, and maybe 100,000 rows, so my dataframe is 100,000 x 50 big. I want to take 100 rows, and concatenate the next 100 rows, etc so I get a wide dataframe with dimension 100 x (50 * 100,000/100) = 100 x 50,000.
Can Pandas do this? My aim is to do some calculations on each of these 100 rows. Or is hierarchical indexing better?
shell [33]>>> df
[33]>>>
0
0 123
1 456
2 789
3 0AB
4 CDE
5 FGH
6 ...
7 ,,,
shell [34]>>> pd.DataFrame(df.values.reshape(4, 2)).sum()
[34]>>>
0 123789CDE...
1 4560ABFGH,,,
dtype: object
Another approach is using groupby.
shell [35]>>> df['group'] = 0
shell [36]>>> df[1::2]['group'] = 1
shell [37]>>> grouped = df.groupby('group')
shell [38]>>> grouped.sum()
[38]>>>
0
group
0 123789CDE...
1 4560ABFGH,,,
Maybe worth studying not to create a new frame and instead work directly on the groups? Certainly for multiple columns and huge numnber of rows.
shell [39]>>> for key, group in grouped:
print key
print group
....:
0
0 group
0 123 0
2 789 0
4 CDE 0
6 ... 0
1
0 group
1 456 1
3 0AB 1
5 FGH 1
7 ,,, 1

Resources