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

Shape of an array

+6
−0

Given a non-ragged array (an array where all sub-arrays at a particular level have the same length) of non-negative integers, answer its shape, that is, the length along every dimension. You may assume that the given array has at least one empty dimension, and that only the trailing dimension can have length 0.

Test cases

[][0]

[0][1]

[[]][1,0]

[[],[]][2,0]

[[4,4],[0,3]][2,2]

[[[4],[6]]][1,2,1]

[[[[7],[0]]]][1,1,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)

6 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.

+0
−0

J, 1 byte

$

This is shape of. Pretty low-hanging fruit tbh.

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 2, 37 bytes

l=input()
while 1:print len(l);l=l[0]

Try it online!

This makes heavy use of programs being allowed to terminate with error, which I assume is allowed by default because it is on SE. We repeatedly print the length, and then replace the list with its first entry. This will fail when we're at a leaf, which is when the list is empty or is actually a number.

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

Raku, 32 bytes

{map +*,($_,*[0]...^9 ge*.gist)}

Try it online!

It's difficult to tell the difference between one element arrays and just plain numbers, since Raku translates between the two. However, gist returns the list formatted with brackets surrounding it, so we are able to use that to our advantage using string comparison.

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

JavaScript (Node.js), 36 bytes

f=a=>a?.map?[a.length,...f(a[0])]:[]

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

JavaScript (Node.js), 51 bytes

n=[];f=(a)=>a&&a.map?(n.push(a.length),f(a[0]),n):n

Run f on your input, e.g. f([[],[]]).


Explanation:

n=[]; // make a global array to hold the results
f=(a)=> // make the function
	a&&a.map? // is a defined, and an array?
		(n.push(a.length), // add the length to the results
		f(a[0]),           // do it again on the first item
		n)                 // ...and return n
	// ...if not, return n
	:n
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

JavaScript (Node.js), 39 bytes

Returns dimensions from innermost to outermost

f=a=>a?.pop?f(a[0]).concat(a.length):[]

JavaScript (Node.js), 41 bytes

Returns dimensions from outermost to innermost

f=a=>a?.pop?[a.length].concat(f(a[0])):[]
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 »