Find index of all true inside massive - swift

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]

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

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]])

Property observer not getting hit, but value is changing

I have got a variable which seems to be changing throughout the app. I have confirmed this by when I originally change a value to be true and then print, it shows that it is then false, false, false, true, false, false, false and my property observed is hit so "It changed!" shows in the console.
However, I later print this out again and it has reverted back to false, despite nothing changing the value of it (that I can see) and the property observer is not hit. It does not print "It changed" but in the console, it then prints false, false, false, false, false, false, false.
var isPicked: [Bool] = [false, false, false, false, false, false, false] {
didSet {
print("It changed!")
}
}
Any help would be much appreciated.

Lazy generation of all binary sequences of a given length in 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)