Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Truthify an array

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (5 comments)

10 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+5
−0

Jelly, 2 bytes

ŒṪ

Try it online!

1-indexed, but the footer converts to 0-indexed.

Less built-in:

Jelly, 3 bytes

œẹ1

Try it online!

œẹ     All multidimensional indices of
  1    one.

Less built-in:

Jelly, 7 bytes

J,Ɱ"T€Ẏ

Try it online!

 ,Ɱ        Pair each
    T      truthy index
     €     from all rows
J  "       with the indices of the rows.
      Ẏ    Flatten rows.

The absolute least built-in:

Jelly, 8 bytes

Jx),€"JẎ

Try it online!

Outputs each index backwards.

Jx          Repeat each index by the corresponding element
  )         for each row.
   ,€"J     Pair each remaining index with its row index.
       Ẏ    Flatten rows.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+7
−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)

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+5
−0

APL (Dyalog Extended), 1 byte

¯\(ツ)

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+4
−0

JavaScript (V8), 44 bytes

a=>a.map((a,x)=>a.map((n,y)=>n&&print(x,y)))

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

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
	)
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+2
−0

Japt -m, 8 7 bytes

ð òÎmiV

Try it

ð òÎmiV     :Implicit map of each sub-array at 0-based index V
ð           :Indices of truthy (i.e., non-zero) elements
  ò         :Partition between elements where
   Î        :The sign of their difference is truthy
    m       :Map
     iV     :  Prepend V
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Scala 3, 68 bytes

_.zipWithIndex.flatMap(_.zipWithIndex.filter(_._1)map(_._2)map _.->)

Try it in Scastie!

Takes a List[List[Boolean]], returns a List[(Int, Int)]. This abuses underscores a bit (which is why Scala 3 is needed), but I'm proud of it.

Explanation:

_.zipWithIndex //Zip each row with its index
  .flatMap(    //Map every (row, index) tuple to a list of truthy indices,
               //then flatten
  _.zipWithIndex //Zip each element with its column
    .filter(_._1) //Keep the tuples where the element (_1) is true
    map(_._2)     //Keep the second part of the tuple (the column)
    map _.->)     //Make a 2-tuple with the row's index
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Python 3, 75 bytes

lambda a:[[i,k]for i in r(l(a))for k in r(l(a[0]))if a[i][k]]
r=range;l=len

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

shortC, 78 bytes

i,j;f(Ix,Iy,I*p){R"[");O;i<x;i++)Oj=0;j<y;j++)*p++&&R"[%d,%d],",i,j);J"\b]");}

Try it online!

From @Lundin's C (gcc) answer.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »