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

Cumulative Counts

+12
−0

Challenge

Given an array of numbers return the cumulative count of each item.

This is the number of times an item has occurred so far.

Examples

[1,1,2,2,2,1,1,1,3,3] -> [1,2,1,2,3,3,4,5,1,2]
[3,7,5,4,9,2,3,2,6,6] -> [1,1,1,1,1,1,2,2,1,2]

Brownie points for beating my 7 byte APL answer.

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?

0 comment threads

21 answers

+1
−0

Lean 4, 111 bytes

def c(l:List Nat):List Nat:=((λx=>((l.reverse).drop x).count (l.reverse)[x]!)<$>(List.range l.length)).reverse

Try it online!

Given how verbose Lean syntax is, and how you can't really assign variables to keywords in a way that it saves bytes the vast majority of the time, if at all, I doubt there's a way to save any more bytes from here. I think 111 might actually be the limit.

And the only reason that I have to reverse the list is because while Lean does have a function to drop elements, it only does so from the left, which we really don't want here.


Hexdump since this contains unprintable characters:

00000000 64 65 66 20 64 28 6c 3a 4c 69 73 74 20 4e 61 74  |def d(l:List Nat|
00000010 29 3a 4c 69 73 74 20 4e 61 74 3a 3d 28 28 ce bb  |):List Nat:=((..|
00000020 78 3d 3e 28 28 6c 2e 72 65 76 65 72 73 65 29 2e  |x=>((l.reverse).|
00000030 64 72 6f 70 20 78 29 2e 63 6f 75 6e 74 20 28 6c  |drop x).count (l|
00000040 2e 72 65 76 65 72 73 65 29 5b 78 5d 21 29 3c 24  |.reverse)[x]!)<$|
00000050 3e 28 4c 69 73 74 2e 72 61 6e 67 65 20 6c 2e 6c  |>(List.range l.l|
00000060 65 6e 67 74 68 29 29 2e 72 65 76 65 72 73 65     |ength)).reverse|
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 »