Comments on Truthify an array
Parent
Truthify an array
Jelly has an atom called untruth, which when given indices, creates an array with 1s at those places: [2,4] → [0,1,0,1]
.
You are required to perform the inverse of this. Given a 2D boolean array, find the indices of the true values in it.
Challenge
You will be given a single 2D boolean array $M$. You may take its dimensions as a separate argument if needed.
The output must consist of all the indices of the true values in $M$.
Output may be 0-indexed or 1-indexed.
It is guaranteed that the input will only consist of two different values.
Test Cases
All test cases are 0-indexed.
I: [[1,0,1,1,1,0]]
O: [[0,0],[0,2],[0,3],[0,4]]
I: [[1,0,1],[1,0,1],[0,1,0]]
O: [[0,0],[0,2],[1,0],[1,2],[2,1]]
[JavaScript (Node.js)], 74 70 …
4y ago
[APL (Dyalog Extended)], 1 byt …
4y ago
[Jelly], 2 bytes ŒṪ …
4y ago
JavaScript (V8), 44 bytes …
3y ago
[C (gcc)], 100 bytes …
4y ago
Japt `-m`, 8 7 bytes ð …
3y ago
JavaScript (Node.js), 68 bytes …
3y ago
Scala 3, 68 bytes ```scala . …
3y ago
[Python 3], 75 bytes …
3y ago
[shortC], 78 bytes i,j; …
3y ago
Post
JavaScript (Node.js), 68 bytes
f=(a)=>a.map((x)=>[...x.entries()].filter((e)=>e[1]).map((e)=>e[0]))
Run the function f on your input, e.g. f([[1,0,1],[1,0,1],[0,1,0]])
.
The output looks like this:
[[0, 2], [0, 2], [1]]
with one subarray for each input subarray.
Explanation:
f=(a)=> // creates a function to run the input on
a.map((x)=> // for each outer array:
[...x.entries()] // creates things like this: [index, value]
.filter((e)=>e[1]) // keep ones where the value acts like true
.map((e)=>e[0]) // return the indices for each one
)
1 comment thread