Challenges

# Truthify an array

+2
−0

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


Why does this post require moderator attention?
Why should this post be closed?

Any number of dimensions is kind of annoying and severely limits some languages such as java - is there a reason for it? Quintec‭ 16 days ago

@Quintec I allowed dimensions as a separate input if needed for that reason. Would it make more sense to have 2D arrays only? Razetime‭ 16 days ago

Maybe, IMO any number of dimensions is just an extra I/O challenge that doesn't really have anything to do with the challenge idea (but that's just me perhaps) Quintec‭ 16 days ago

Ok, I've changed it to take 2D arrays only. Razetime‭ 16 days ago

+5
−0

# JavaScript (Node.js), 74 70 bytes

Works for any number of dimensions.

f=(a,z=[])=>a.map?.((b,c)=>z.push(...f(b).map(a=>[c,...a])))?z:a?[z]:z


Try it online! (TIO uses an old version of Node.js that does not support the ?. operator, but you can try the 74 bytes solution)

Why does this post require moderator attention?

+1
−0

# C (gcc), 100 bytes

i,j;f(x,y,p)int*p;{puts("[");for(;i<x;i++)for(j=0;j<y;j++)*p++&&printf("[%d,%d],",i,j);puts("\b]");}


Try it online!

Note that TIO console doesn't handle backspace properly.

Why does this post require moderator attention?