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

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

0 comment threads

23 answers

+1
−0

Uiua, 3 bytes (SBCS)

Builtin +1.

+1⧆

Uiua pad this online

History

0 comment threads

+1
−0

Python 3, 70 bytes

def f(a):
 d={};r=[]
 for x in a:d[x]=d.get(x,0)+1;r+=[d[x]]
 return r

Try it online!

History

0 comment threads

+1
−0

Haskell + hgl, 10 bytes

mpn$ce~<gj

Attempt This Online!

Explanation

  • mpn map across all non-empty prefixes of the input ...
  • ce count the number of elements in each prefix equal to ...
  • gj the last element of that prefix.

Reflection

I'm happy with this. It doesn't take any extra steps, and the glue is cheap.

History

0 comment threads

Sign up to answer this question »