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

Dashboard
Notifications
Mark all as read
Challenges

Cumulative Counts

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

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comments

8 answers

+3
−0

Jelly, 4 bytes

¹Ƥċ"

Try it online!

   "    For each element of the input,
  ċ     how many times does it occur in
¹Ƥ "    the corresponding prefix of the input?
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+3
−0

APL (Dyalog Unicode), 11 bytes (SBCS)

1 1∘⍉+\∘.=⍨

Try it online!

This was a fun APL exercise. Working on figuring out how to get it down to 7 bytes.

Why does this post require moderator attention?
You might want to add some details to your flag.

4 comments

Btw, since this isn't a train, you might want to add a to the end and make it a tradfn that takes input from stdin. user‭ 27 days ago

and you can preserve the same length by changing 1 1∘⍉ 1 1⍉ Razetime‭ 27 days ago

@user How would I test the code (as in how should I format my TIO fields to use that)? hyper-neutrino‭ 26 days ago

I don't know, there's a template for reading the stuff in the code section, but I usually do something like this and then subtract 2 bytes. user‭ 26 days ago

+3
−0

BQN, 5 bytes

1+⊒

Try it here!

3 characters, but, as there's no SBCS codepage for BQN, it must be counted as UTF-8.

Two of the three characters are just adding one to the built-in that almost solves the challenge too.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−0

Scala 3, 50 bytes

x=>x.zipWithIndex.map((?,i)=>x take i+1 count?.==)

Try it in Scastie!

This is an annoying, stupid approach. The more elegant one using inits didn't work out.

x =>   //The input
x.zipWithIndex //Zip each element with its index
  .map((?,i) =>  //For every element `?` and its index `i`,
    x take i+1   //Take the first i+1 elements
    count        //Count how many of them
    ?.==)        //Are equal to `?` (the element)
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−0

APL (Dyalog Unicode), 11 7 bytes (SBCS)

Razetime and rak1507 came up with 7 byte equivalents of my original dfn (this one's rak1507's). See their solutions below.

+/¨⊢=,\

Try it online!

+/¨⊢=,\
     ,\ ⍝ Prefixes of the list
    =   ⍝ Compare every prefix
   ⊢   ⍝ to the corresponding element in the original list
+/     ⍝ Sum each to get a count of how many elements in each prefix match
Why does this post require moderator attention?
You might want to add some details to your flag.

3 comments

(this as a train is my 7 byte solution) rak1507‭ 27 days ago

1⊥¨⊢=,\ here you go Razetime‭ 27 days ago

or +/¨⊢=,/ or my personal favourite ⊢+.=¨,\ rak1507‭ 27 days ago

+1
−0

Husk, 4 bytes

Sz#ḣ

Try it online!

Explanation

Sz#ḣ
Sz   zip the input
   ḣ with its prefixes
  #  using the count function
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−0

Jelly, 7 bytes

=þ`ÄŒDḢ

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+0
−0

Japt, 14 11 bytes

í@s0YÄ è_¥X

I must be really tired at 12:30am because I can't find Japt's array count function

Try it

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!