Lazy generation of all binary sequences of a given length in Scala - scala

I want to generate all binary sequences of a given length, however I want it done lazily, so as not to overload memory.
As an example of what i want, I provide a Python code, that does exactly what I want:
def nextBinary(i, seq):
b = seq[:i] + [1] + seq[i + 1:]
yield b
if i + 1 < len(seq):
for nextSeq in nextBinary(i + 1, seq):
yield nextSeq
for nextSeq in nextBinary(i + 1, b):
yield nextSeq
def genBinary(length):
start = [0 for i in range(length)]
yield start
for seq in nextBinary(0, start):
yield seq
Now the genBinary returns a generator object which I can use like this:
for seq in genBinary(2):
print(seq)
# [0, 0]
# [1, 0]
# [0, 1]
# [1, 1]
for seq in genBinary(3):
print(seq)
# [0, 0, 0]
# [1, 0, 0]
# [0, 1, 0]
# [0, 0, 1]
# [0, 1, 1]
# [1, 1, 0]
# [1, 0, 1]
# [1, 1, 1]
How would I code something equivalent in Scala? I suspect it might be somehow doable with Streams or maybe continuations.

Here's one way you could do it with Streams:
val bss: Stream[Vector[List[Boolean]]] =
Vector(List.empty) #:: bss.map(bs => bs.map(true :: _) ++ bs.map(false :: _))
bss(3).mkString("\n")
//res1: String =
//List(true, true, true)
//List(true, true, false)
//List(true, false, true)
//List(true, false, false)
//List(false, true, true)
//List(false, true, false)
//List(false, false, true)
//List(false, false, false)
bss(4).mkString("\n")
//res2: String =
//List(true, true, true, true)
//List(true, true, true, false)
//List(true, true, false, true)
//List(true, true, false, false)
//List(true, false, true, true)
//List(true, false, true, false)
//List(true, false, false, true)
//List(true, false, false, false)
//List(false, true, true, true)
//List(false, true, true, false)
//List(false, true, false, true)
//List(false, true, false, false)
//List(false, false, true, true)
//List(false, false, true, false)
//List(false, false, false, true)
//List(false, false, false, false)

Related

How to access the values only in flutter map

"turf_amenities": {
"turf_washroom": true,
"turf_water": true,
"turf_dressing": false,
"turf_parking": true,
"turf_gallery": true,
"turf_cafeteria": true
},
You can get the bool list like
final bools = data["turf_amenities"]?.values.toList();
print(bools);
final data = {
"turf_amenities": {
"turf_washroom": true,
"turf_water": true,
"turf_dressing": false,
"turf_parking": true,
"turf_gallery": true,
"turf_cafeteria": true
},
};
final values = data.values.toList();
print(
values); //[{turf_washroom: true, turf_water: true, turf_dressing: false, turf_parking: true, turf_gallery: true, turf_cafeteria: true}]
final bools = data["turf_amenities"]?.values.toList();
print(bools); //[true, true, false, true, true, true]

How to match two boolean tables and count match values and unmatch values

table_1
"mcqtest_id": 1,
"option_one_correct": true,
"option_two_correct": true,
"option_three_correct": true,
"option_four_correct": true,
"option_five_correct": false,
"option_six_correct": false,
"option_seven_correct": false,
"option_eight_correct": false,
"question_id": 1
table_2
"mcqtest_id": 1,
"option_one_correct": false,
"option_two_correct": true,
"option_three_correct": false,
"option_four_correct": true,
"option_five_correct": false,
"option_six_correct": false,
"option_seven_correct": false,
"option_eight_correct": false,
"question_id": 2
I want to find the match between this two boolean tables and if match count match values and else count unmatch values with postgresql query.
help me to understand and write this query

Find index of all true inside massive

I've an array of booleans there are 10 trues and falses and I want to know the index of all trues
var trueAndFalses = [false, false, true, false, false, false, true, false, false, false]
I tried firstIndex but it only returns the firstIndex ( yes kinda ironic ). now I wonder is there any built in functions to find all the indexes of true
print(trueAndFalses.firstIndex(of: true))
any solution will be appericated <3
You can try to find all index by visiting all elements and checking if its true like below:
var trueAndFalses = [false, false, true, false, false, false, true, false, false, false]
for var i in (0..<trueAndFalses.count){
if (trueAndFalses[i] == true)
{
print("true fount at index ",i)
}
}
Output:
true fount at index 2
true fount at index 6
Other way:
let indices = trueAndFalses.enumerated().compactMap { $1 == true ? $0 : nil }
print(indices)
Output:
[2, 6]

Scipy: Union of sparse boolean matrices

In Scipy, what is the most efficient way to get the union A+B+C of multiple boolean sparse (csr) matrices A,B,C (with the same shape)?
Union means among others:
sparsity changes
overlaps are possible
Just add them:
import scipy.sparse as sparse
x = sparse.csr_matrix([[True, True, False], [False, False, False], [True, False, False]] , dtype=bool)
y = sparse.csr_matrix([[False, True, False], [False, True, False], [False, True, False]], dtype=bool)
print((x + y).todense())
>>[[ True True False]
[False True False]
[ True True False]]
EDIT
If you want to access the indices directly, you can use coo format (which allows retrieving row and col index), stack the indices and use np.unique (disclaimer: I haven't checked for efficiency comparison):
import scipy.sparse as sparse
c2=sparse.eye(5, k=1, dtype=bool, format='coo')
c1=sparse.eye(5, dtype=bool, format='coo')
c3 = c1.copy()
c3.row, c3.col = np.unique(np.hstack((np.vstack((c1.col, c1.row)),np.vstack((c2.col, c2.row)))), axis=1)
c3.data = np.ones(c3.row.size, dtype=bool)
c3.todense()
>> matrix([[ True, False, False, False, False],
[ True, True, False, False, False],
[False, True, True, False, False],
[False, False, True, True, False],
[False, False, False, True, True]])

how to schedule a pgagent job through scripts/commandLine

I want to run jobs through PgAgent.
I am able to do that by creating a job in PgAgentJob through PgAdmin UI, as described here https://www.pgadmin.org/docs/pgadmin4/dev/pgagent_jobs.html.
But I want to use a sql script that can create a PgAgent job as we do in Oracle. Please suggest how I can achieve this.
To create a job in pgAgent use something like the following INSERT STATEMENTS (for a Routine Maintenance job) :
INSERT INTO pgagent.pga_job (jobid, jobjclid, jobname, jobdesc, jobenabled, jobhostagent)
SELECT jcl.jclid, 'MyJob', '', true, ''
FROM pgagent.pga_jobclass jcl WHERE jclname='Routine Maintenance';
To add a step to this job, which executes a SQL command ('delete from test where user_name=''test'';), use the following command:
INSERT INTO pgagent.pga_jobstep (jstjobid, jstname, jstdesc, jstenabled, jstkind, jstonerror, jstcode, jstdbname, jstconnstr)
SELECT (SELECT jobid
FROM pgagent.pga_job
WHERE jobname = 'MyJob'), 'MyStep', '', true, 's', 'f', 'delete from test where user_name=''test'';', 'postgres', '';
To create a schedule for this job (every day at 08:45), use the following command:
INSERT INTO pgagent.pga_schedule (jscjobid, jscname, jscdesc, jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths, jscenabled, jscstart, jscend)
VALUES((SELECT jobid
FROM pgagent.pga_job
WHERE jobname = 'MyJob'), 'MySchedule', '', '{f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,t,f,f,f,f,f,f,f,f,f,f,f,f,f,f}',
'{f,f,f,f,f,f,f,f,t,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f}', '{t,t,t,t,t,t,t}', '{t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t}',
'{t,t,t,t,t,t,t,t,t,t,t,t}', true, '2018-07-16 00:00:00', NULL);
Here a graphical representation of this schedule:
Here you can see a complete summary of these commands inside an anonymous block (generated by pgAdmin):
DO $$
DECLARE
jid integer;
scid integer;
BEGIN
-- Creating a new job
INSERT INTO pgagent.pga_job(
jobjclid, jobname, jobdesc, jobhostagent, jobenabled
) VALUES (
1::integer, 'MyJob'::text, ''::text, ''::text, true
) RETURNING jobid INTO jid;
-- Steps
-- Inserting a step (jobid: NULL)
INSERT INTO pgagent.pga_jobstep (
jstjobid, jstname, jstenabled, jstkind,
jstconnstr, jstdbname, jstonerror,
jstcode, jstdesc
) VALUES (
jid, 'MyStep'::text, true, 's'::character(1),
''::text, 'postgres'::name, 'f'::character(1),
'delete from test where user_name=''test'';'::text, ''::text
) ;
-- Schedules
-- Inserting a schedule
INSERT INTO pgagent.pga_schedule(
jscjobid, jscname, jscdesc, jscenabled,
jscstart, jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths
) VALUES (
jid, 'MySchedule'::text, ''::text, true,
'2018-07-16 00:00:00+02'::timestamp with time zone,
-- Minutes
ARRAY[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false]::boolean[],
-- Hours
ARRAY[false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]::boolean[],
-- Week days
ARRAY[true, true, true, true, true, true, true]::boolean[],
-- Month days
ARRAY[true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true]::boolean[],
-- Months
ARRAY[true, true, true, true, true, true, true, true, true, true, true, true]::boolean[]
) RETURNING jscid INTO scid;
END
$$;