Google Sheets ARRAYFORMULA count preceeding rows that meet condition - regex

Let's say I have a spreadsheet that looks something like this:
Name D-List
--------------------- ------
Arnold Schwarzenegger
Bruce Willis
Dolph Lundgren
Dwayne Johnson
Jason Statham
Keanu Reeves
Samuel L. Jackson
Sylvester Stallone
Vin Diesel
For the D-List column, I'd like to count the number of proceeding rows that contains the string "d". If the row doesn't contain a "d", then I want it to return an empty string.
For any given row, I can get this to work with the following pseudo formula:
=IF(REGEXMATCH(A<row>, "d"), COUNTIF(A<row>, "*d*"), "")
Name D-List
--------------------- ------
Arnold Schwarzenegger 1
Bruce Willis
Dolph Lundgren 2
Dwayne Johnson 3
Jason Statham
Keanu Reeves
Samuel L. Jackson
Sylvester Stallone
Vin Diesel 4
I can turn this into an expression that can be duplicated between rows by using INDIRECT and ROW:
=IF(REGEXMATCH(A2, "(?i)d"), COUNTIF(INDIRECT("A2:A" & ROW(A2)), "*D*"), "")
Name D-List
--------------------- ------
Arnold Schwarzenegger 1
Bruce Willis
Dolph Lundgren 2
Dwayne Johnson 3
Jason Statham
Keanu Reeves
Samuel L. Jackson
Sylvester Stallone
Vin Diesel 4
However, if I try to stick it in an ARRAYFORMULA, it doesn't work.
=ARRAYFORMULA(IF(REGEXMATCH(A2:A, "D"), COUNTIF(INDIRECT("A2:A" & ROW(A2:A)), "*D*"), ""))
Name D-List
--------------------- ------
Arnold Schwarzenegger
Bruce Willis
Dolph Lundgren 1
Dwayne Johnson 1
Jason Statham
Keanu Reeves
Samuel L. Jackson
Sylvester Stallone
Vin Diesel 1
What am I missing?

try:
=ARRAYFORMULA(IF(
REGEXMATCH(A2:A, "(?i)d"), COUNTIFS(
REGEXMATCH(A2:A, "(?i)d"),
REGEXMATCH(A2:A, "(?i)d"), ROW(A2:A), "<="&ROW(A2:A)), ))

Related

only sumif checkbox is checked?

How do I only sum the amounts that are checked on sheet2 for each name?
Sheet1
Column A
Tom
Susan
Sheet2
Column A Column B Column C
Tom 100 (un-checked)
Susan 150 (checked)
Susan 75 (un-checked)
Tom 25 (checked)
Susan 50 (checked)
Solved!
=SUMIFS(Sheet2!B1:B,Sheet2!A1:A,Sheet1!A1:A,Sheet2!C1:C,true)
Output:
Tom 25
Susan 200
=SUMIFS(M2:M,A2:A,"Susan",N2:N,true)

Return all possible pairs combinations of values for a single column in SQL

I would like to get all DISTINCT pairs combination to follow table :
Table Name: Dancer
id name
1 Yaniv
2 Dan
3 Eli
4 Guy
5 Sara
6 Naama
7 Suzi
8 Vered
*The results should be like this :
pairs
Yaniv Dan
Yaniv Eli
Yaniv Guy
Yaniv Sara
Yaniv Naama
Yaniv Suzi
Yaniv Vered
Dan Eli
Dan Guy
Dan Sara
Dan Naama
Dan Suzi
Dan Vered
Guy Sara
Guy Naama
Guy Suzi
Guy Vered
Sara Naama
Sara Suzi
Sara Vered
Naama Suzi
Naama Vered
Suzi Vered
I tried CROSS JOIN + WHERE clause to eliminate the identical names as Yaniv Yaniv,Dan Dan...etc
BUT I get also multiple pairs combinations as
Yaniv Dan
Dan Yaniv
How can I filter these multiple pairs..??
this is my SQL code :
Select D2.name + ' ' + D1.name
From Dancer D1
Cross join
Dancer D2
Where D1.name<>D2.name
Hope my question is clear enough.
If your goal is to get all the column of names with all other names in the same column this would be considered a Cartesian join.
Would be something like the following:
Select a.Name +' ' + b.Name as ResultName
from Table as a, Table as b
This works if your Name column contains like you described : John, Yaniv, Dan .. would results in John Yaniv, John Name, Yaniv John, Yaniv Dan. If however your names have multiple parts like Sarah T you will only get the single name with other names. Example: Sarah T John, Sarah T Yaniv, etc. It will not break down Sarah and T into separate results.

Python regex re.sub() is not matching and replacing as expected

The following regex isn't replacing substrings as expected.
I've tried running the code with the following modifications (one at a time, of course) all with no luck:
Utilizing list comprehensions (current)
Using a traditional for loop
Adding the regex result back to the iterator itself
Appending the regex result to a new list
Checked the type of 'name' (it's a string)
Utilized (copied) code format from another regex in my notebook that is currently working
Put the regex into regex101.com to verify that it's functioning (you can see the regex and data I'm using here
Adding/removing the raw string indicators preceding the regex and substitution patterns
names is a list of strings
reg_pattern = r"(?!\\s)(\\W[^\\W,]+)(?!,) and\\s([^ ]+ )([^ ]+)"
sub_pattern = r"\\1 \\3 \\2\\3"
cleaned_names = []
cleaned_names = [re.sub(reg_pattern, sub_pattern, name) for name in names]
The goal can be seen in the link above (particularly in the 'substitution' section at the bottom of that page), but ultimately, I need to append group3 of the regex to the end of group1.
I'm guessing that maybe, you're trying to re.sub the couples names, for which you can likely write some expression similar to:
([A-Z][a-z]+)\s+and\s+(.*)([A-Z]\S*)
if you are not having edge cases, if you do then, you'd probably want to modify the char classes, [A-Z], and add those other chars, in there.
Demo
Test
import re
l = ['George Rosario, Ali Jones, Barbara Boll, and Lindsay McKelvoy', 'Jan and Edgar Adelman', 'Bill Mack and Les Lieberman', 'Dr. Susan Muehle-Bussel, Ray Morales, and Dr. Samuel Barker', 'Dan Barroso and Emily High', 'Cassie and George Sorenson', 'Tom Scott and Mark Smith', 'The scene at IDEAL School & Academy’s 10th\xa0Annual Gala.',
'Les Lieberman, Barri Lieberman, Isabel Kallman, Trish Iervolino, and Ron Iervolino', 'Chuck Grodin', 'Diana Rosario, Ali Sussman, Sarah Boll, Jen Zaleski, Alysse Brennan, and Lindsay Macbeth', 'Kelly and Tom Murro', 'Udo Spreitzenbarth', 'Ron Iervolino, Trish Iervolino, Russ Middleton, and Lisa Middleton', 'Barbara Loughlin, Dr. Gerald Loughlin, and Debbie Gelston', 'Julianne Michelle']
e = r'([A-Z][a-z]+)\s+and\s+(.*)([A-Z]\S*)'
l_out = []
for names in l:
if re.match(e, names):
l_out.append(re.sub(e, r'\1 \3 and \2\3', names))
else:
l_out.append(names)
print(l_out)
Output
['George Rosario, Ali Jones, Barbara Boll, and Lindsay McKelvoy', 'Jan
Adelman and Edgar Adelman', 'Bill Mack and Les Lieberman', 'Dr. Susan
Muehle-Bussel, Ray Morales, and Dr. Samuel Barker', 'Dan Barroso and
Emily High', 'Cassie Sorenson and George Sorenson', 'Tom Scott and
Mark Smith', 'The scene at IDEAL School & Academy’s 10th\xa0Annual
Gala.', 'Les Lieberman, Barri Lieberman, Isabel Kallman, Trish
Iervolino, and Ron Iervolino', 'Chuck Grodin', 'Diana Rosario, Ali
Sussman, Sarah Boll, Jen Zaleski, Alysse Brennan, and Lindsay
Macbeth', 'Kelly Murro and Tom Murro', 'Udo Spreitzenbarth', 'Ron
Iervolino, Trish Iervolino, Russ Middleton, and Lisa Middleton',
'Barbara Loughlin, Dr. Gerald Loughlin, and Debbie Gelston', 'Julianne
Michelle']
Or you can try
import re
l = ['George Rosario, Ali Jones, Barbara Boll, and Lindsay McKelvoy', 'Jan and Edgar Adelman', 'Bill Mack and Les Lieberman', 'Dr. Susan Muehle-Bussel, Ray Morales, and Dr. Samuel Barker', 'Dan Barroso and Emily High', 'Cassie and George Sorenson', 'Tom Scott and Mark Smith', 'The scene at IDEAL School & Academy’s 10th\xa0Annual Gala.',
'Les Lieberman, Barri Lieberman, Isabel Kallman, Trish Iervolino, and Ron Iervolino', 'Chuck Grodin', 'Diana Rosario, Ali Sussman, Sarah Boll, Jen Zaleski, Alysse Brennan, and Lindsay Macbeth', 'Kelly and Tom Murro', 'Udo Spreitzenbarth', 'Ron Iervolino, Trish Iervolino, Russ Middleton, and Lisa Middleton', 'Barbara Loughlin, Dr. Gerald Loughlin, and Debbie Gelston', 'Julianne Michelle']
e = r'([A-Z][a-z]+)\s+and\s+(.*)([A-Z]\S*)'
l_out = []
for names in l:
if re.match(e, names):
l_out.append(re.sub(e, r'\1 \3', names))
l_out.append(re.sub(e, r'\2\3', names))
else:
l_out.append(names)
print(l_out)
Output
['George Rosario, Ali Jones, Barbara Boll, and Lindsay McKelvoy', 'Jan
Adelman', 'Edgar Adelman', 'Bill Mack and Les Lieberman', 'Dr. Susan
Muehle-Bussel, Ray Morales, and Dr. Samuel Barker', 'Dan Barroso and
Emily High', 'Cassie Sorenson', 'George Sorenson', 'Tom Scott and Mark
Smith', 'The scene at IDEAL School & Academy’s 10th\xa0Annual Gala.',
'Les Lieberman, Barri Lieberman, Isabel Kallman, Trish Iervolino, and
Ron Iervolino', 'Chuck Grodin', 'Diana Rosario, Ali Sussman, Sarah
Boll, Jen Zaleski, Alysse Brennan, and Lindsay Macbeth', 'Kelly
Murro', 'Tom Murro', 'Udo Spreitzenbarth', 'Ron Iervolino, Trish
Iervolino, Russ Middleton, and Lisa Middleton', 'Barbara Loughlin, Dr.
Gerald Loughlin, and Debbie Gelston', 'Julianne Michelle']
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.

Printing Out Linked List - C++

I'm trying to print out all of the employees in my linked list but am encountering an issue to where all of but the last employee is being printed out. I have a printRoster() function to where it prints out all of the names of my list correctly which is 3 total, but my print function only seems to print out just 2. (I can post more code if necessary)
Here is my text file:
START_OF_FILE
INSERT_EMPLOYEE
123456
John
Smith
64000
35
INSERT_EMPLOYEE
345678
Mike
Jones
70000
30
INSERT_EMPLOYEE
234567
Dean
Thomas
72000
40
PRINT_ROSTER
PRINT_EMPLOYEE
John
Smith
PRINT_EMPLOYEE
Mike
Jones
PRINT_EMPLOYEE
Dean
Thomas
END_OF_FILE
My output:
John Smith, 123456
Mike Jones, 345678
Dean Thomas, 234567
John Smith, 123456
Salary: 64000
Hours: 35
Mike Jones, 345678
Salary: 70000
Hours: 30
Expected output:
John Smith, 123456
Mike Jones, 345678
Dean Thomas, 234567
John Smith, 123456
Salary: 64000
Hours: 35
Mike Jones, 345678
Salary: 70000
Hours: 30
Dean Thomas, 234567
Salary: 72000
Hours: 40
Problem is in your printEmployee function's while loop while (tempEmployee->next != NULL)
You are checking if next employee is present or not, and if it is present then and only then your loop is executed.
In your case when your loop is at last employee, it checks if next employee is present or not and as it is not present your loop is not executed and the info of last employee is not printed.
you should change your while loop like this
while(tempEmployeee != NULL)

Set variable with new line and tab

Formatting issue that is getting passed to document. Sample companyList below which varies each time script is run
companyList = ["Apple - Seattle Washington (800) 555-5555", "Microsoft - Tampa Florida (800) 555-1234", "Samsung - Tokyo Japan (01) 555 123-1234"]
Right now the line of code to format this text is:
companyInfo = "\n\n".join(companyList)
and companyInfo outputs like this:
Apple - Seattle Washington (800) 555-5555 Microsoft - Tampa Florida (800) 555-1234 Samsung - Tokyo Japan (01) 555 123-1234
How can I rewrite this to format like this (note tabbed one over per new line):
Apple - Seattle Washington (800) 555-5555
Microsoft - Tampa Florida (800) 555-1234
Samsung - Tokyo Japan (01) 555 123-1234
Many thanks in advance
You can do it like this:
companyInfo = "\n\n".join("\t%s" % x for x in companyList)