Count if "Yes" Google Sheets condition - if-statement

I have a survey and in this one, there is Yes/No questions I'm counting the answers by questions like in the image above, but I want to make a conditional count like as example if someone from the survey answer yes and there is a conditional question related to that like Are you happy? and there is a mix of answers as example 5 yes and 2 nos gives a total of 7 inputs then the second question if you are happy why are you happy? and from the 5 yes we receive 4 answers I want to count the second question only if the first answers was yes, I have this formulas that make that in a split version:
=(COUNTIF(A2:A13,"?*")+COUNT(A2:A13)) # Gives a total of 4
Then if this total is bigger than Zero =IF(H2>0,1,"") with this for each row I have 1 input as example.
But if one of the first questions was Yes I want to count the second question and this is were I get lost.
I would like to mix the both formulas in just one cell. like this:
=IF(Resultofcounts > 0 , 1, "") #Resultofcounts= (COUNTIF(A2:A13,"?*")+COUNT(A2:A13))

unless I miss something:
=ARRAYFORMULA(IF(B3:F7="",,1))
update:
=INDEX({
IF(TRIM(FLATTEN(QUERY(TRANSPOSE(B3:E8),,9^9)))="",,1),
IF(TRIM(FLATTEN(QUERY(TRANSPOSE(F3:G8),,9^9)))="",,1)})

Related

I am trying to get this Arrayformula to work [duplicate]

This question already has answers here:
Perform calculation only if both cells are not blank with arrayformula? [duplicate]
(3 answers)
ArrayFormula and "AND" Formula in Google Sheets
(4 answers)
Closed 4 months ago.
So I am trying to get this Arrayformula to work so I can plot this formula instead of calculating every Y myself.
=arrayformula(Sum(IF(Z3:Z294>Y$1;IFS(AB3:AB294>0;0;Z3:Z294>Y$1;Y3:Y294-(F3:F294*Y$1));0)-(Sum(IF(and(J3:J294>0;Z3:Z294>Y$1);F3:F294;0)))))
It gives me the (correct) return of this part:
Sum(IF(Z3:Z294>Y$1;IFS(AB3:AB294>0;0;Z3:Z294>Y$1;Y3:Y294-(F3:F294*Y$1));0)
But it doesn't subtract the second part:
-(Sum(IF(and(J3:J294>0;Z3:Z294>Y$1);F3:F294;0)))))
I am quite new to extensive Excel/Sheets formulas so I have no idea how to get this to work, It is also quite weird that the second part doesn't add up even seperate from the first part. So this also doesn't work:
=arrayformula(Sum(IF(and(J3:J294>0;Z3:Z294>Y$1);F3:F294;0)))
I hope it makes some sense without any context, thanks in advance!
Have a great rest of your day,
P.S. Please ignore simple mistakes, I don't code that often in Sheets ;)
AND is not supported. instead of
and(J3:J294>0;Z3:Z294>Y$1)
do this:
(J3:J294>0)*(Z3:Z294>Y$1)

Extract 2 non-consecutive strings and always sort them in desired way

I'm finishing my regex project in google sheets and this last question I have for it. Hope you can help.
I have a column, in which one cell may or may not contain multiple strings.
This is an example of it (start position):
Now I want to extract the following values:
Team (A)
Team B
Team (A) Team B
Desired end result image:
I want to check for all 3 possible values, and always sort them with Team (A) as a priority. In case the initial cell (C column) is empty, the result is empty.
I tried this formula:
=ARRAYFORMULA(IFERROR(REGEXEXTRACT(C2,"Team \(A\)|Team B|[Team \(A\) "&" Team B]"),IF(C2="","")))
Problems I have:
Don't know how to give priority :/
Don't know how to give them sorting order :/
All help welcome, thank you.
=ARRAYFORMULA(REGEXREPLACE(IFERROR(
REGEXEXTRACT(C2:C, "(Team \(A\))")&CHAR(10))&
IFERROR(REGEXEXTRACT(C2:C, "(Team B)")), "\n$", ))

Google Sheets Pattern Matching/RegEx for COUNTIF

The documentation for pattern matching for Google Sheets has not been helpful. I've been reading and searching for a while now and can't find this particular issue. Maybe I'm having a hard time finding the correct terms to search for but here is the problem:
I have several numbers (part numbers) that follow this format: ##-####
Categories can be defined by the part numbers, i.e. 50-03## would be one product category, and the remaining 2 digits are specific for a model.
I've been trying to run this:
=countif(E9:E13,"50-03[123][012]*")
(E9:E13 contains the part number formatted as text. If I format it any other way, the values show up screwed up because Google Sheets thinks I'm writing a date or trying to do arithmetic.)
This returns 0 every time, unless I were to change to:
=countif(E9:E13,"50-03*")
So it seems like wildcards work, but pattern matching does not?
As you identified and Wiktor mentioned COUNTIF only supports wildcards.
There are many ways to do what you want though, to name but 2
=ArrayFormula(SUM(--REGEXMATCH(E9:E13, "50-03[123][012]*")))
=COUNTA(FILTER(E9:E13, REGEXMATCH(E9:E13, "50-03[123][012]*")))
This is a really big hammer for a problem like yours, but you can use QUERY to do something like this:
=QUERY(E9:E13, "select count(E) where E matches '50-03[123][012]' label count(E) ''")
The label bit is to prevent QUERY from adding an automatic header to the count() column.
The nice thing about this approach is that you can pull in other columns, too. Say that over in column H, you have a number of orders for each part. Then, you can take two cells and show both the count of parts and the sum of orders:
=QUERY(E9:H13, "select count(E), sum(H) where E matches '50-03[123][012]' label count(E) '', sum(H) ''")
I routinely find this question on $searchEngine and fail to notice that I linked another question with a similar problem and other relevant answers.

SPOJ ALPHA CODE [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I was practicing the dynamic programming problem on SPOJ. But I have no idea how to solve this one.
Can anyone please help ME in solving http://www.spoj.pl/problems/ACODE/ problem on SPOJ
Thanks!
Alice and Bob need to send secret messages to each other and are
discussing ways to encode their messages:
Alice: “Let’s just use a very simple code: We’ll assign ‘A’ the code
word 1, ‘B’ will be 2, and so on down to ‘Z’ being assigned 26.”
Bob: “That’s a stupid code, Alice. Suppose I send you the word ‘BEAN’
encoded as 25114. You could decode that in many different ways!”
Alice: “Sure you could, but what words would you get? Other than
‘BEAN’, you’d get ‘BEAAD’, ‘YAAD’, ‘YAN’, ‘YKD’ and ‘BEKD’. I think
you would be able to figure out the correct decoding. And why would
you send me the word ‘BEAN’ anyway?” Bob: “OK, maybe that’s a bad
example, but I bet you that if you got a string of length 5000 there
would be tons of different decodings and with that many you would find
at least two different ones that would make sense.” Alice: “How many
different decodings?” Bob: “Jillions!”
For some reason, Alice is still unconvinced by Bob’s argument, so she
requires a program that will determine how many decodings there can be
for a given string using her code.
Input
Input will consist of multiple input sets. Each set will consist of a
single line of at most 5000 digits representing a valid encryption
(for example, no line will begin with a 0). There will be no spaces
between the digits. An input line of ‘0’ will terminate the input and
should not be processed.
Output
For each input set, output the number of possible decodings for the
input string. All answers will be within the range of a 64 bit signed
integer.
Example
Input:
25114 1111111111 3333333333 0
Output:
6 89 1
Starting from the left, do the following:
Find how many words the sequence can be interpreted as (call that say x[k]) up to this point using a finite number of the values for previous calculated for points along the sequence.
Move to the next point.
If you still can't get it, you can take a look at the Welcome to Code Jam problem. It somewhat similar and has readily available explanations for it.
If you have a string of numbers as S, then, there are two cases possible :
1) only the first digit corresponds to an alphabet
2) the first two digits correspond to an alphabet. BUT, only if the first two digits don't form a number greater than 26.
Let S be of size n. Let f(Si) be the number of strings formed by last i digits. Note that you have to find f(Sn).
Using the above two rules, you can write a relation as :
If first two digits form a number <= 26 :
f ( Sk ) = f (Sk-1) + f (Sk-2)
If first two digits form a number > 26 :

Regex for binary multiple of 3

I would like to know how can I construct a regex to know if a number in base 2 (binary) is multiple of 3. I had read in this thread Check if a number is divisible by 3 but they dont do it with a regex, and the graph someone drew is wrong(because it doesn't accept even numbers). I have tried with: ((1+)(0*)(1+))(0) but it doesn't works for some values. Hope you can help me.
UPDATE:
Ok, thanks all for your help, now I know how to draw the NFA, here I left the graph and the regular expresion:
In the graph, the states are the number in base 10 mod 3.
For example: to go to state 1 you have to have 1, then you can add 1 or 0, if you add 1, you would have 11(3 in base 10), and this number mod 3 is 0 then you draw the arc to the state 0.
((0*)((11)*)((1((00) *)1) *)(101 *(0|((00) *1 *) *0)1) *(1(000)+1*01)*) *
And the other regex works, but this is shorter.
Thanks a lot :)
I know this is an old question, but an efficient answer is yet to be given and this question pops up first for "binary divisible by 3 regex" on Google.
Based on the DFA proposed by the author, a ridiculously short regex can be generated by simplifying the routes a binary string can take through the DFA.
The simplest one, using only state A, is:
0*
Including state B:
0*(11)*0*
Including state C:
0*(1(01*0)*1)*0*
And include the fact that after going back to state A, the whole process can be started again.
0*((1(01*0)*1)*0*)*
Using some basic regex rules, this simplifies to
(1(01*0)*1|0)*
Have a nice day.
If I may plug my solution for this code golf question! It's a piece of JavaScript that generates regexes (probably inefficiently, but does the job) for divisibility for each base.
This is what it generates for divisibility by 3 in base 2:
/^((((0+)?1)(10*1)*0)(0(10*1)*0|1)*(0(10*1)*(1(0+)?))|(((0+)?1)(10*1)*(1(0+)?)|(0(0+)?)))$/
Edit: comparing to Asmor's, probably very inefficient :)
Edit 2: Also, this is a duplicate of this question.
For some who is learning and searching how to do this:
see this video:
https://www.youtube.com/watch?v=SmT1DXLl3f4&t=138s
write state quations and solve them with Axden's Theorem
The way I did is visible in the image-result is the same as pointed out by user #Kert Ojasoo. I hope i did it corretly because i spent 2 days to solve it...
n+2n = 3n. Thus, 2 adjacent bits set to 1 denote a multiple of 3. If there are an odd number of adjacent 1s, that would not be 3.
So I'd propose this regex:
(0*(11)?)+