| Bytes | Lang | Time | Link |
|---|---|---|---|
| 046 | Juby | 250511T173605Z | Jordan |
| 004 | Thunno 2 | 230809T154355Z | The Thon |
| 029 | JavaScript Node.js | 220102T001918Z | Dom Hast |
| 066 | Prolog SWI | 220102T060656Z | 0 |
| 007 | Pyth | 221003T184251Z | hakr14 |
| 005 | Nibbles | 220105T195635Z | Darren S |
| 023 | Wolfram Language Mathematica | 220102T042839Z | att |
| 034 | PowerShell Core | 220126T000811Z | Julian |
| 005 | Vyxal | 220101T192059Z | emanresu |
| 037 | Ruby | 220104T225634Z | daniero |
| 006 | 05AB1E | 220105T181400Z | Kevin Cr |
| 049 | C gcc | 220105T064617Z | att |
| 035 | Pari/GP | 220103T142713Z | alephalp |
| 061 | Juby | 220102T071056Z | Razetime |
| 064 | Java | 220103T014459Z | Unmitiga |
| 015 | x8664 machine code | 220102T142933Z | m90 |
| 040 | Python + Pandas | 220102T043255Z | U13-Forw |
| 046 | Python 3.8 | 220101T191900Z | DLosc |
| 047 | C clang | 220102T014438Z | Noodle9 |
| 023 | Add++ | 220101T235625Z | lyxal |
| 012 | Perl 5 + p | 220102T000154Z | Dom Hast |
| 010 | APL Dyalog Unicode | 220101T195909Z | user |
| 005 | Japt | 220101T225720Z | AZTECCO |
| 006 | Husk | 220101T225601Z | Dominic |
| 003 | BQN | 220101T191642Z | ovs |
| 038 | Zsh | 220101T222817Z | pxeger |
| 031 | R | 220101T215007Z | Giuseppe |
| 044 | Factor | 220101T220850Z | chunes |
| 037 | Perl 5 | 220101T212104Z | Kjetil S |
| 068 | Retina 0.8.2 | 220101T213133Z | Neil |
| 011 | Haskell + hgl | 220101T212616Z | Wheat Wi |
| 010 | Charcoal | 220101T211303Z | Neil |
| 009 | J | 220101T203032Z | Jonah |
| 5654 | C tcc | 220101T200239Z | wizzwizz |
| 6160 | Python 3 | 220101T192317Z | wizzwizz |
| 005 | Jelly | 220101T200511Z | Jonathan |
| 033 | R | 220101T194142Z | pajonk |
| 043 | Scala | 220101T194006Z | user |
| 022 | APL+WIN | 220101T193452Z | Graham |
| 005 | Jelly | 220101T191157Z | caird co |
| 007 | MATL | 220101T191913Z | Luis Men |
| 051 | JavaScript ES6 | 220101T192131Z | rydwolf |
Thunno 2, 4 bytes
ġıŻ+
Explanation
ġıŻ+ # Implicit input
ġ # Group consecutive items together
ı # Map over each group:
Ż # Push [0..length)
+ # Add this to the group
# Implicit output
JavaScript (Node.js), 29 bytes
Saved 6 bytes thanks to @tsh and 2 more bytes thanks to @Shaggy!
a=>a.map(i=n=>i[n]=i[n]+1||n)
Explanation
maps over the input (a) (storing the mapping function as i) and returns the value of setting index n in i to the previous value + 1, or the input (n), which will increment the input each following time it's encountered.
Prolog (SWI), 75 66 bytes
B+A,[C]-->[A],{C is A+B},B+1+A;[X],{C=X},1+X.
_+_-->[].
a-->0+x,!.
This program can be run by calling the grammar a//0 to parse the input list. The remainder after parsing will be unified with the output list.
Pyth, 7 bytes
+VsUM.g
Explanation:
+VsUM.g | Full code
+VsUM.gkQQ | with implicit variables
-----------+----------------------------------
.gkQ | Group identical runs in the input
UM | Convert each run to [0..length)
s | Flatten
+V Q | Vector add to input
Nibbles, 5 bytes
!$,~~+-@?_
That's 10 nibbles which are encoded in 0.5 bytes each. Nibbles isn't on TIO yet.
Translation:
! zip
$ the input list
,~ 1..
~ (by a function, as opposed to 1 op zip) \x y->
+
-
@ y
? index
_ the input list
implicit x
implicit y
Usage: pass the input in via the command line i.e.
nibbles filename.nbl "[1,1,1,1,10,10]"
Command line args used instead of stdin because input int list would still be 1 nibble in its second use (DeBruijn indicies incremented by 2 from the zip).
This is basically the same as DLosc's solution in Python (adding the difference of the index from its position)
Wolfram Language (Mathematica), 28 23 bytes
g[#]++&/@((g@#=#)&/@#)&
(g@#=#)&/@# initialize g[x]=x for each x in input
g[#]++&/@( ) post-increment g[x] for each x in input
Vyxal, 5 bytes
Ġvẏf+
-1 thanks to lyxal
Ġ # Group runs of identical chars
vẏf # 0...n each and flatten
+ # Add to input
Port of caird coinheringaahing's answer.
Ruby, 37 bytes
Requires Ruby version 2.7 or newer.
->a{a.tally.flat_map{[*_1.._1+_2-1]}}
A quick rundown:
->a{X} is an anonymous lambda function taking a parameter a and returning X.
tally returns a hash with each unique number in the input list and the number of times that number appeared in the list.
[*1..4] returns [1,2,3,4].
flat_map returns a flattened list from the result of the given {code block} being run on each number/count pair returned by tally, where _1 is a placeholder for the number and _2 is the count. So for example with an array with 5 2s, [*_1.._1+_2-1] returns [2,3,4,5,6].
05AB1E, 6 bytes
ÅγL<+˜
Try it online or verify all test cases.
€kā<α+
Try it online or verify all test cases.
γεεN+,
Outputs each item newline separated.
Try it online or verify all test cases.
I have the feeling 5 bytes should be possible, but haven't been able to find it yet. Here are three different 6-bytes approached, though.
Explanation:
Åγ # Run-length encode the (implicit) input-list, pushing the lists of items and
# counts separated to the stack
L # Transform each count to an inner list in the range [1,count]
< # Decrease each by 1 to make the ranges [0,count)
+ # Add the run-length items to each inner list
˜ # Flatten the list of lists
# (after which this list is output implicitly as result)
€ # Map each item in the (implicit) input-list to:
k # The 0-based index of its first occurrence
ā # Push a list in the range [1,length] (without popping)
< # Decrease each by 1 to make the range [0,length)
α # Get the absolute difference of each with the indices
+ # And add each to the (implicit) input-items
# (after which the list is output implicitly as result)
γ # Split the (implicit) input-list into parts of equal adjacent items
ε # Map each inner list to:
ε # Map each integer to:
N+ # Add the 0-based (inner) map-index
, # Pop and output it with trailing newline
Pari/GP, 35 bytes
a->b=x;[if(b-t,c=0;b=t,b+c++)|t<-a]
Pari/GP, 36 bytes
a->p=0;[t+polcoeff(p+=x^t,t)-1|t<-a]
Based on my PARI/GP answer to another challenge.
J-uby, 66 61 bytes
:group_by+:next|:values|:*&(:zip%(:size|:*)|:*&:sum)|:flatten
Surely there is a way to improve this. Yep, + is very useful.
x86-64 machine code, 15 bytes
3b 06 74 02 31 c9 ad 29 4e fc ff cf e0 f2 c3
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes the length of the array in RDI and the address of the array of 32-bit integers in RSI. The starting point is after the first 4 bytes.
Assembly:
.global f
repeat:
cmp eax, [rsi] # Compare last value and current value
je skip # Jump if they are equal
f:
xor ecx, ecx # (If not equal, or at the start) ECX = 0
skip:
lodsd # Put the current value in EAX, and advance the pointer
sub [rsi-4], ecx # Subtract ECX from the (same) value
dec edi # Count down from the length
loopnz repeat # Jump back if not finished, also reducing ECX by 1
ret # Return
Here is another solution I found, at 16 bytes, taking the arguments in the opposite order:
.global f
f:
mov eax, edx
repeat:
cmpxchg [rdi], edx
cmovne edx, eax
inc edx
scasd
dec esi
jnz repeat
ret
Python + Pandas, 40 bytes
lambda L:S(L)+S(L).groupby(L).cumcount()
The code doesn't run on TIO, since it's pandas...
But anyway, here is the code.
Python 3.8, 48 46 bytes
2-byte savings jointly contributed by Jonathan Allan and dingledooper
lambda L,j=1:[n-(j:=j-1)-L.index(n)for n in L]
Explanation
The original 48-byte solution is easier to explain:
lambda L:[n+i-L.index(n)for i,n in enumerate(L)]
Consider each number \$n\$ in the list together with its index \$i\$. Suppose that we are looking at the \$k^{th}\$ occurrence (0-indexed) of the number \$n\$. We want to add \$k\$ to the number. Since identical numbers are adjacent, the index of the \$0^{th}\$ occurrence of \$n\$ will be at index \$i-k\$. This is the result of L.index(n), meaning \$k\$ is i-L.index(n). Add that value to \$n\$ for each element of the list, and we're done.
To get the 46-byte solution, we track the index using a variable instead of enumerate, updating it in the list comprehension using Python 3.8's "walrus operator" :=. We could track the actual index \$i\$:
lambda L,i=-1:[n+(i:=i+1)-L.index(n)for n in L]
but it saves a byte to track \$j \equiv -i\$ instead, because we can initialize it to 1 instead of -1.
C (clang), 47 bytes
b;f(*a,n){for(b=0;n--;b+=*a-b-*++a?-b:1)*a+=b;}
Inputs a pointer to an array of integers and its length (because pointers in C carry no length info).
Incrementally increments identical elements in place.
Add++, 26 23 bytes
L,dBG€bL€RbFz£+dbL1Xz£_
Explained
L,dBG€bL€RbFz£+dbL1Xz£_
L, # create a lambda that:
d # pushes two copies of the input
BG # ... groups the second on consecutive items
€bL # ... gets the length of each of those groups
€R # ... creates the range [1...n] for each of those lengths (add++ doesn't have a range [0...n) built-in for some reason dang it caird.)
bF # ... flattens that list
z # and zips it with the original input. This creates a list of items and how much to increment by
£+ # now, reduce each item by addition
dbL # and push the length of the list, keeping a copy on the stack of the original list
1X # push a list of length(^) 1s
z£_ # and subtract that from each item in the other list (this is just to account for the fact that there's no [0...n) built-in)
Husk, 6 bytes
ṁGo→Kg
g # group equal elements
ṁ # then map the following onto each group
# (concatenating the results):
G # scan from left
# (arg1=result so far, arg2=next element)
o # combination of 2 functions:
K # constant function (= arg1)
→ # incremented
BQN, 3 bytesSBCS
Perfect challenge for BQN's occurence count builtin.
+⟜⊒
The modifier ⟜ composes two functions. If there is a single argument (as is the case here), the right function is called on that argument, and then the left function is called with that result and the original argument:
(f⟜g x) ≡ (x f g x)
The builtin ⊒ takes a vector and returns for each element how many times the same value appeared before:
(⊒ 1‿1‿1‿10‿10‿100) ≡ 0‿1‿2‿0‿1‿0
The result of ⊒ is then added element-wise to original input by +:
(1‿1‿1‿10‿10‿100 + 0‿1‿2‿0‿1‿0) ≡ 1‿2‿3‿10‿11‿100
Ports of other more interesting answers:
Zsh, 38 bytes
p=.5 for x;echo $[(c=p-x?0:c+1)+(p=x)]
I think there might a solution abusing mv's backup functionality like this answer, but I can't work out how.
R, 31 bytes
function(a)a+seq(!a)-match(a,a)
Port of Jonah's J answer. Uses 1-based indices but it makes no difference.
Test harness taken from pajonk's answer.
Factor, 44 bytes
[ dup '[ over + swap _ index - ] map-index ]
Port of @Dlosc's Python answer.
dup '[ ... _ ... ] slots a copy of the input into the quotation at the _. map-index is like map except it also places the index on the stack in addition to the element.
There were a lot of other approaches and attempts; the last three are just longer versions of the above.
[ histogram >alist [ first2 over + 1 <range> ] map-concat ]
[ [ ] group-by values [ dup length iota v+ ] map-flat ]
[ histogram [ over + 1 <range> ] f assoc>map concat ]
[ dup '[ _ overd index -rot + - abs ] map-index ]
[| l | l [ | n i | n i + l index - ] map-index ]
[| l | l [ over + swap l index - ] map-index ]
Retina 0.8.2, 68 bytes
r(`(?<=(^|,)(\3,)*)((-)?\d+)
#$#2$*1$4$3$*
+`1-1
-
#(-)?(1*)-?
$1$.2
Try it online! Link includes test cases. Explanation:
r(`
Use right-to-left matching for the whole script, as that allows the matches to be specified slightly more golfily.
(?<=(^|,)(\3,)*)((-)?\d+)
Match each integer, and count how many times it's been repeated.
#$#2$*1$4$3$*
Replace each integer with a marker #, the repetition count in unary, and the integer in unary. Note that if the integer is positive this already adds the count to the integer.
+`1-1
-
If the integer was negative then find the difference between it and the count.
#(-)?(1*)-?
$1$.2
Convert the integer back to decimal, ignoring a trailing - sign, which indicates that the count was at least as large as the absolute value of the integer and therefore the result is no longer negative.
28 bytes in Retina 1 if only non-negative integers need to be supported:
r`(?<=(\2,)*)(\d+)
$.(*_$#1*
Try it online! Link includes test cases. Explanation:
r`(?<=(\2,)*)(\d+)
From right to left, match each integer, and count how many times it's been repeated.
$.(*_$#1*
Add the number of repetitions to the integer.
Haskell + hgl, 11 bytes
gr+>zW(+)nn
Explanation
Most of this is pretty simple
gris "group"; it groups a list into a list of equal segments.zWis "zip with"; it takes a function and uses it to combine two lists pairwise.(+)is "plus"; it adds things.nnis "the natural numbers"; an infinite list of all non-negative numbers in order.zW(+)nncombines the last 3 into a single thing which takes a list and adds every element with its index.
The last thing here that links gr with zW(+)nn. The +> operator. This operator has a name it's called "Kleisli composition". Regular composition takes functions a -> b and b -> c and produces a third function a -> c, Kleisli composition does very similar. It takes Kleisli morphisms, which are just a particular kind of function, that maps into a monad. So a -> m b where m is a monad would be an example of a function from a to m b, but is a Kleisli morphism from a to b.
So Kleisli composition takes a function a -> m b and a function b -> m c and produces a function a -> m c where m is some monad.
This is in fact sort of the fundamental essence of a monad, that Kleisli composition forms a category. So how are we using it here?
Well we have gr :: List a -> List (List a) and zW(+)nn :: List a -> List a and the thing we want is List a -> List a. A naive way to do it would be to group the whole thing map across each element and the concat everything back up:
jn<m(zW(+)nn)<gr
However an experienced Haskell golfer will notice that map and then concat is exactly the monad behavior of the List. So we could potentially get a bind to do the concat and the map in one go.
And now if we look at the types we can see they fit the shape of Kleisli morphisms. So we can actually, compose, map and concat all at once using Kleisli composition.
Charcoal, 10 bytes
IEθ⁺ι№…θκι
Try it online! Link is to verbose version of code. Does not require the array to be sorted or grouped. Explanation:
θ Input array
E Map over elements
ι Current element
⁺ Plus
№ Count of
ι Current element in
…θκ Prefix of input
I Cast to string
Implicitly print
Alternative approach, also 10 bytes, but requires the array to be grouped:
IEθ⁺ι⁻κ⌕θι
Try it online! Link is to verbose version of code. Explanation:
θ Input array
E Map over elements
ι Current element
⁺ Plus
κ Current index
⁻ Minus
⌕ First index of
ι Current element in
θ Input array
I Cast to string
Implicitly print
J, 9 bytes
+i.@#-i.~
Basically a port of DLosc's python answer, which fits naturally in the array paradigm.
how
Consider 1, 1, 1, 1, 10, 10, 20, 20, 20, 30, 40, 40, 40, 40
i.~Index of each element's first appearance:0 0 0 0 4 4 6 6 6 9 10 10 10 10i.@#-Subtract that from0 1 2 ... n:0 1 2 3 0 1 0 1 2 0 0 1 2 3+Add that to the original input:1 2 3 4 10 11 20 21 22 30 40 41 42 43
C (tcc), 56 54 bytes
c;f(int*p,int*q){for(c=0;p-q;*p+=++c)c*=!(*p-*++p-c);}
- -2 bytes thanks to pxeger
This is a rather straightforward port of my suboptimal Python answer. Takes a pointer to the beginning and end of the array.
Explanation
c; /* int c; (thanks pxeger) */
f(
int *p, /* pointer to the first integer */
int *q /* pointer to the last integer */
) {
for (
c=0; /* initialise the increment */
p-q; /* reached the end? (if so, stop) */
/* note: these run out of order */
*p+=++c)
c*=!(*p-*++p-c);
/*
equivalent to:
int original_p0 = p[0] - c; // undo increment
if (original_p0 != p[1]) {
c = 0; // different, so reset counter
}
p[1] += c;
c += 1;
p += 1; // move to next p
*/
}
Python 3, 61 60 bytes
I wrote this answer to make sure my algorithm was sound, before I implemented it in C.
def f(a,b=.5,c=0):
for d in a:c*=not d-b;yield d+c;c+=1;b=d
Explanation
def f(
a, # input list
b=.5, # previous value
c=0 # consecutive counter
):
for d in a:
c *= not d-b; # if d and b are different, c = 0
yield d + c;
c += 1;
b = d # next loop, this'll be the previous value
Jelly, 5 bytes
+ċṪ$Ƥ
A monadic Link accepting a sorted list of integers that yields a list of integers.
How?
+ċṪ$Ƥ - Link: list of integers, A
Ƥ - for prefixes of A:
$ - last two links as a monad, f(prefix):
Ṫ - pop off the tail from the prefix
ċ - count occurrences of that in the remaining prefix
+ - A add that (vectorises)
Scala, 43 bytes
s=>s.indices.map(i=>s(i)+i-s.indexOf(s(i)))
A port of DLosc's great answer. Go upvote that!
My original answer, 48 bytes
s=>s.distinct.flatMap(x=>x to x-1+s.count(x.==))
This one gets distinct elements, then for every element x, it makes a range from x to x + n, where n is the number of occurrences of x.
APL+WIN, 22 bytes
Prompts for input vector
v+¯1++⌿p×+\p←<⍀v∘.=v←⎕
Jelly, 5 bytes
ŒɠḶF+
How it works
ŒɠḶF+ - Main link. Takes a list L on the left
Œɠ - Group run lengths
Ḷ - Zero based range
F - Flatten
+ - Add elementwise to L
For example, take L = [1, 1, 2, 2, 3, 3]:
Œɠ:[2, 2, 2]Ḷ:[[0, 1], [0, 1], [0, 1]]F:[0, 1, 0, 1, 0, 1]+:[0, 1, 0, 1, 0, 1] + [1, 1, 2, 2, 3, 3] = [1, 2, 2, 3, 3, 4]
MATL, 7 bytes
t&=XRs+
Try it online! Or verify all test cases
How it works
Consider input [1, 1, 10, 10, 100, 100, 100, 100] as an example.
t % Implicit input. Duplicate
% STACK: [1 1 10 10 100 100 100 100], [1 1 10 10 100 100 100 100]
&= % Matrix of equality comparisons
% STACK: [1 1 10 10 100 100 100 100], [1 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0
0 0 1 1 0 0 0 0
0 0 1 1 0 0 0 0
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1]
XR % Upper triangular part, without the diagonal
% STACK: [1 1 10 10 100 100 100 100], [0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1
0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1]
s % Sum of each column
% STACK: [1 1 10 10 100 100 100 100], [0 1 0 1 0 1 2 3]
+ % Add, element-wise. Implicit display
% STACK: [1 2 10 11 100 101 102 103]
JavaScript (ES6), 51 bytes
x=>x.map((y,i)=>y+x.reduce((r,z,k)=>r+=z==y&k<i,0))