Enumerate each series of identical numbers in-place
Given a list of strictly positive integers, go through each distinct number and replace all occurrences of it with successive indices (zero or one based) of a new series.
Examples
→
/
[42]
→ [0]
/[1]
[7,7,7]
→ [0,1,2]
/[1,2,3]
[10,20,30]
→ [0,0,0]
/[1,1,1]
[5,12,10,12,12,10]
→ [0,0,0,1,2,1]
/[1,1,1,2,3,2]
[2,7,1,8,2,8,1,8,2,8]
→ [0,0,0,0,1,1,1,2,2,3]
/[1,1,1,1,2,2,2,3,3,4]
[3,1,4,1,5,9,2,6,5,3,5,9]
→ [0,0,0,1,0,0,0,0,1,1,2,1]
/[1,1,1,2,1,1,1,1,2,2,3,2]
code-golf number integer sorting substitution
add a comment |
Given a list of strictly positive integers, go through each distinct number and replace all occurrences of it with successive indices (zero or one based) of a new series.
Examples
→
/
[42]
→ [0]
/[1]
[7,7,7]
→ [0,1,2]
/[1,2,3]
[10,20,30]
→ [0,0,0]
/[1,1,1]
[5,12,10,12,12,10]
→ [0,0,0,1,2,1]
/[1,1,1,2,3,2]
[2,7,1,8,2,8,1,8,2,8]
→ [0,0,0,0,1,1,1,2,2,3]
/[1,1,1,1,2,2,2,3,3,4]
[3,1,4,1,5,9,2,6,5,3,5,9]
→ [0,0,0,1,0,0,0,0,1,1,2,1]
/[1,1,1,2,1,1,1,1,2,2,3,2]
code-golf number integer sorting substitution
2
So basically the number of times it has appeared the sequence so far?
– Jo King
yesterday
1
@JoKing Yes, that's another way to state it, but "so far" implies zero-based, and "until and including this" implies one-based. I wanted to keep the choice.
– Adám
yesterday
add a comment |
Given a list of strictly positive integers, go through each distinct number and replace all occurrences of it with successive indices (zero or one based) of a new series.
Examples
→
/
[42]
→ [0]
/[1]
[7,7,7]
→ [0,1,2]
/[1,2,3]
[10,20,30]
→ [0,0,0]
/[1,1,1]
[5,12,10,12,12,10]
→ [0,0,0,1,2,1]
/[1,1,1,2,3,2]
[2,7,1,8,2,8,1,8,2,8]
→ [0,0,0,0,1,1,1,2,2,3]
/[1,1,1,1,2,2,2,3,3,4]
[3,1,4,1,5,9,2,6,5,3,5,9]
→ [0,0,0,1,0,0,0,0,1,1,2,1]
/[1,1,1,2,1,1,1,1,2,2,3,2]
code-golf number integer sorting substitution
Given a list of strictly positive integers, go through each distinct number and replace all occurrences of it with successive indices (zero or one based) of a new series.
Examples
→
/
[42]
→ [0]
/[1]
[7,7,7]
→ [0,1,2]
/[1,2,3]
[10,20,30]
→ [0,0,0]
/[1,1,1]
[5,12,10,12,12,10]
→ [0,0,0,1,2,1]
/[1,1,1,2,3,2]
[2,7,1,8,2,8,1,8,2,8]
→ [0,0,0,0,1,1,1,2,2,3]
/[1,1,1,1,2,2,2,3,3,4]
[3,1,4,1,5,9,2,6,5,3,5,9]
→ [0,0,0,1,0,0,0,0,1,1,2,1]
/[1,1,1,2,1,1,1,1,2,2,3,2]
code-golf number integer sorting substitution
code-golf number integer sorting substitution
asked yesterday
AdámAdám
29k269192
29k269192
2
So basically the number of times it has appeared the sequence so far?
– Jo King
yesterday
1
@JoKing Yes, that's another way to state it, but "so far" implies zero-based, and "until and including this" implies one-based. I wanted to keep the choice.
– Adám
yesterday
add a comment |
2
So basically the number of times it has appeared the sequence so far?
– Jo King
yesterday
1
@JoKing Yes, that's another way to state it, but "so far" implies zero-based, and "until and including this" implies one-based. I wanted to keep the choice.
– Adám
yesterday
2
2
So basically the number of times it has appeared the sequence so far?
– Jo King
yesterday
So basically the number of times it has appeared the sequence so far?
– Jo King
yesterday
1
1
@JoKing Yes, that's another way to state it, but "so far" implies zero-based, and "until and including this" implies one-based. I wanted to keep the choice.
– Adám
yesterday
@JoKing Yes, that's another way to state it, but "so far" implies zero-based, and "until and including this" implies one-based. I wanted to keep the choice.
– Adám
yesterday
add a comment |
27 Answers
27
active
oldest
votes
JavaScript (ES6), 26 bytes
1-indexed.
a=>a.map(o=x=>o[x]=-~o[x])
Try it online!
Commented
a => // a = input array
a.map(o = // assign the callback function of map() to the variable o, so that
// we have an object that can be used to store the counters
x => // for each value x in a:
o[x] = -~o[x] // increment o[x] and yield the result
// the '-~' syntax allows to go from undefined to 1
) // end of map()
1
I have no idea how that works, but it sure looks elegant.
– Adám
yesterday
I've not seen-~
before - that is an absolute gem.
– DaveMongoose
yesterday
Alternatively, it's possible to usea
to store the values, but it's required to-
/~
the index so no byte is saved.
– user202729
yesterday
@DaveMongoose Tips for golfing in JavaScript
– user202729
yesterday
Too bad this isn't C++, otherwise something likea=>a.map(x=>++(a[-x]|=0))
would save a byte.
– user202729
yesterday
|
show 2 more comments
MATL, 4 bytes
&=Rs
This solution is 1-based
Try it out at MATL Online!
Explanation
Uses [1,2,3,2]
as an example
# Implicitly grab the input array of length N
#
# [1,2,3,2]
#
&= # Create an N x N boolean matrix by performing an element-wise comparison
# between the original array and its transpose:
#
# 1 2 3 2
# -------
# 1 | 1 0 0 0
# 2 | 0 1 0 1
# 3 | 0 0 1 0
# 2 | 0 1 0 1
#
R # Take the upper-triangular portion of this matrix (sets below-diagonal to 0)
#
# [1 0 0 0
# 0 1 0 1
# 0 0 1 0
# 0 0 0 1]
#
s # Compute the sum down the columns
#
# [1,1,1,2]
#
# Implicitly display the result
1
ah, I knew there was an old problem that made me think of something similar, it's Unique is Cheap, and the MATL solution there is one character different!
– Giuseppe
yesterday
add a comment |
APL (Dyalog Unicode), 7 bytes
Many, many thanks to H.PWiz, Adám and dzaima for all their help in debugging and correcting this.
+/¨⊢=,
Try it online!
Explanation
The 10-byte non-tacit version will be easier to explain first
{+/¨⍵=,⍵}
{ } A user-defined function, a dfn
,⍵ The list of prefixes of our input list ⍵
(⍵ more generally means the right argument of a dfn)
is 'scan' which both gives us our prefixes
and applies ,/ over each prefix, which keeps each prefix as-is
⍵= Checks each element of ⍵ against its corresponding prefix
This checks each prefix for occurrences of the last element of that prefix
This gives us several lists of 0s and 1s
+/¨ This sums over each list of 0s and 1s to give us the enumeration we are looking for
The tacit version does three things
- First, it removes the instance of
⍵
used in,⍵
as,
on the right by itself can implicitly figure out that it's supposed to operate on the right argument. - Second, for
⍵=
, we replace the⍵
with⊢
, which stands for right argument - Third, now that we have no explicit arguments (in this case,
⍵
), we can remove the braces{}
as tacit functions do not use them
add a comment |
J, 7 bytes
1#.]=]
Try it online!
1-indexed.
Explanation:
] all the prefixes (filled with zeros, but there won't be any 0s in the input):
] 5 12 10 12 12 10
5 0 0 0 0 0
5 12 0 0 0 0
5 12 10 0 0 0
5 12 10 12 0 0
5 12 10 12 12 0
5 12 10 12 12 10
]= is each number from the input equal to the prefix:
(]=]) 5 12 10 12 12 10
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 1 0 1 0 0
0 1 0 1 1 0
0 0 1 0 0 1
1#. sum each row:
(1#.]=]) 5 12 10 12 12 10
1 1 1 2 3 2
K (oK), 11 bytes
{+/'x=',x}
Try it online!
1
Heh, you're happy that I made the data strictly positive…
– Adám
yesterday
@Adám Yes, otherwise I'd need to box the prefixes :)
– Galen Ivanov
yesterday
add a comment |
Python 2, 48 bytes
lambda a:[a[:i].count(v)for i,v in enumerate(a)]
Try it online!
add a comment |
05AB1E, 4 bytes
ηε¤¢
Try it online!
or as a Test Suite
Explanation
ηε # apply to each prefix of the input list
¤¢ # count occurrences of the last element
add a comment |
AWK, 15
{print a[$1]++}
Try it online!
The above does zero-based indexing. If you prefer one-based indexing, its a simple change:
{print ++a[$1]}
Try it online!
add a comment |
Python 2, 47 43 bytes
f=lambda a:a and f(a[:-1])+[a.count(a[-1])]
Try it online!
A recursive 'one-based' solution.
add a comment |
Jelly, 4 bytes
ċṪ$Ƥ
Try it online!
For each prefix of the input list, it counts the number of occurrences of its last element in itself.
Alternatively the old-school;ċ"
is also 4.
– Jonathan Allan
yesterday
add a comment |
C# (Visual C# Interactive Compiler), 44 bytes
x=>x.Select((y,i)=>x.Take(i).Count(z=>z==y))
Try it online!
22 bytes
– LiefdeWen
yesterday
You have the inversed of the challenge right now..[7,7,7]
should output[0,1,2]
, and not[0,0,0]
.
– Kevin Cruijssen
yesterday
1
@KevinCruijssen - Thanks :) Looks like I misread things, it should be fixed now.
– dana
yesterday
add a comment |
Ruby, 35 bytes
->a{f=Hash.new 0;a.map{|v|f[v]+=1}}
It's pretty mundane, unfortunately - build a hash that stores the total for each entry encountered so far.
Some other, fun options that unfortunately weren't quite short enough:
->a{a.dup.map{a.count a.pop}.reverse} # 37
->a{i=-1;a.map{|v|a[0..i+=1].count v}} # 38
add a comment |
Perl 6, 15 bytes
*>>.&{%.{$_}++}
Try it online!
You can move the ++
to before the %
for a one based index.
Explanation:
*>>.&{ } # Map the input to
% # An anonymous hash
.{$_} # The current element indexed
++ # Incremented
add a comment |
R, 41 bytes
function(x)diag(diffinv(outer(x,x,"==")))
Try it online!
Oddly, returning a zero-based index is shorter in R.
Once again Giuseppe, your superior knowledge of R has beaten me. I had a decently ingenious method at 60 bytes, but alas, it wasn't enough!
– Sumner18
yesterday
@Sumner18 post it anyway! I always learn a lot from other peoples' approaches, and getting feedback is the fastest way to learn!
– Giuseppe
yesterday
thanks for the encouragement! I've posted mine now and am always open to suggestions for improvement!
– Sumner18
yesterday
add a comment |
Java (JDK), 76 bytes
a->{for(int l=a.length,i,c;l-->0;a[l]=c)for(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;}
Try it online!
Credits
- -2 bytes thanks to Kevin Cruijssen
1
-2 bytes by changingfor(c=0,i=l;i-->0;)c+=a[l]==a[i]?1:0;
tofor(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;
.
– Kevin Cruijssen
yesterday
add a comment |
R, 62 43 bytes
x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
-19 bytes thanks to Giuseppe, by removing which, and table, and only slight changes to the implementation
Original
x=z=scan();for(i in names(r<-table(x)))z[which(x==i)]=1:r[i];z
I can't compete with Giuseppe's knowledge, so my submission is somewhat longer than his, but using my basic knowledge, I felt that this solution was rather ingenious.
r<-table(x)
counts the number of times each number appears and stores it in r, for future reference
names()
gets the values of each unique entry in the table, and we iterate over these names with a for loop.
The remaining portion checks which entries are equal to the iterations and stores a sequence of values (from 1 to the number of entries of the iteration)
Try it online!
you can remove thewhich()
to save 7 bytes.
– Giuseppe
yesterday
Your use of1:r[i]
gave me the idea to just removetable()
entirely:x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
is 43 bytes! This is a nice approach!
– Giuseppe
yesterday
add a comment |
Ruby, 34 bytes
->a{r=;a.map{|x|(r<<x).count x}}
Try it online!
I can't believe I tried->a{i=-1;a.map{|v|a[0..i+=1].count v}}
and didn't think of just building a new array, lol. Nice work.
– DaveMongoose
19 hours ago
add a comment |
bash, 37 bytes or 24 bytes
f()(for x;{ r+=$[a[x]++] ;};echo $r)
TIO
if valid, there is also this variation, as suggested by DigitalTrauma
for x;{ echo $[a[x]++];}
TIO
1
Pass the list as command line args - tio.run/##S0oszvj/Py2/SKHCuporNTkjX0ElOjG6IlZbO5ar9v///8b/… - only 24 bytes.
– Digital Trauma
yesterday
@DigitalTrauma, thanks however i don't know if it broke the rules. also as it was asked to replace list and maybe should be something like tio.run/…
– Nahuel Fouilleul
21 hours ago
1
@NahuelFouilleul It's fine, full programs are allowed too, and that's a valid method of inputting/outputting a list (IMO)
– ASCII-only
21 hours ago
add a comment |
Retina 0.8.2, 30 bytes
b(d+)b(?<=(b1b.*?)+)
$#2
Try it online! Link includes test cases. 1-indexed. Explanation: The first part of the regex matches each integer in the list in turn. The lookbehind's group matches each occurrence of that integer on that line up to and including the current integer. The integer is then substituted with the number of matches.
add a comment |
Batch, 61 bytes
@setlocal
@for %%n in (%*)do @set/ac=c%%n+=1&call echo %%c%%
1-indexed. Because variable substitution happens before parsing, the set/a
command ends up incrementing the variable name given by concatenating the letter c
with the integer from the list (numeric variables default to zero in Batch). The result is then copied to another integer for ease of output (more precisely, it saves a byte).
add a comment |
awk (46 bytes)
{delete(a);for(i=1;i<=NF;i++)$i=a[$i]++;print}
TIO
add a comment |
Tcl, 48 bytes
proc C L {lmap n $L {dict g [dict inc D $n] $n}}
Try it online!
add a comment |
Japt, 8 bytes
£¯YÄ è¶X
Try it here
£¯YÄ è¶X
:Implicit input of array U
£ :Map each X at 0-based index Y
¯ : Slice U to index
YÄ : Y+1
è : Count the elements
¶X : Equal to X
add a comment |
Haskell, 44 bytes
(#)
x#(y:z)=sum[1|a<-x,a==y]:(y:x)#z
_#e=e
Try it online!
Explanation
Traverses the list from left to right keeping the list x
of visited elements, initially :
For every encounter of a y
count all equal elements in the list x
.
add a comment |
Haxe, 58 bytes
l->{var x=[0=>0];l.map(e->++x[(x[e]==null?x[e]=0:0)+e]);};
(Requires arrow functions, so 4.0+)
var x=[0=>0]
declares a new IntMap
, with 0
as its only key (since the question say inputs are strictly positive). Unfortunately most targets throw when adding null
to a number, hence the explicit check to make sure each key is in the map before incrementing.
Also a cheeky rip off based on the JS answer:
Haxe (JS target), 41 bytes
l->{var x=[0=>0];l.map(e->x[e]=-~x[e]);};
Try both online
add a comment |
GO 121 Bytes (Not included new lines and tabs)
func cg(a int) int{
var m = make(map[int]int)
var r = make(int, len(a))
for i,v := range a{
r[i] = m[v]
m[v]++
}
return r
}
Accepts integer array and returns integer array.
New contributor
3
Welcome to PPCG. Rare to see Go represented here. You do need to count necessary newlines, but you also have a lot of spaces that can be removed, and you can also shortencg
toc
. I recommend using TIO to generate the main part of your post: Try it online!
– Adám
19 hours ago
add a comment |
C++,137 bytes
//v is input , ov is output ; //example to populate v: vector v{7,8,7};
vector<int> v,ov(v.size());map<int,int>m;transform(begin(v),end(v),begin(ov),[&m](int i){!m.count(i)?m[i]=0:m[i]+=1;return m[i];});
New contributor
I'm not sure about C++, but can't you remove the newlines to save 2 bytes?
– Stephen
yesterday
1
I don't know C++ either, but what is this? It doesn't look like a function, and I don't see how you take input?
– Jo King
19 hours ago
add a comment |
Python 2, 44 bytes
a=
for x in input():print a.count(x);a+=x,
Try it online!
The first thing I wrote tied Chas Brown's 43, so here's a different solution that's one byte longer.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178500%2fenumerate-each-series-of-identical-numbers-in-place%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
27 Answers
27
active
oldest
votes
27 Answers
27
active
oldest
votes
active
oldest
votes
active
oldest
votes
JavaScript (ES6), 26 bytes
1-indexed.
a=>a.map(o=x=>o[x]=-~o[x])
Try it online!
Commented
a => // a = input array
a.map(o = // assign the callback function of map() to the variable o, so that
// we have an object that can be used to store the counters
x => // for each value x in a:
o[x] = -~o[x] // increment o[x] and yield the result
// the '-~' syntax allows to go from undefined to 1
) // end of map()
1
I have no idea how that works, but it sure looks elegant.
– Adám
yesterday
I've not seen-~
before - that is an absolute gem.
– DaveMongoose
yesterday
Alternatively, it's possible to usea
to store the values, but it's required to-
/~
the index so no byte is saved.
– user202729
yesterday
@DaveMongoose Tips for golfing in JavaScript
– user202729
yesterday
Too bad this isn't C++, otherwise something likea=>a.map(x=>++(a[-x]|=0))
would save a byte.
– user202729
yesterday
|
show 2 more comments
JavaScript (ES6), 26 bytes
1-indexed.
a=>a.map(o=x=>o[x]=-~o[x])
Try it online!
Commented
a => // a = input array
a.map(o = // assign the callback function of map() to the variable o, so that
// we have an object that can be used to store the counters
x => // for each value x in a:
o[x] = -~o[x] // increment o[x] and yield the result
// the '-~' syntax allows to go from undefined to 1
) // end of map()
1
I have no idea how that works, but it sure looks elegant.
– Adám
yesterday
I've not seen-~
before - that is an absolute gem.
– DaveMongoose
yesterday
Alternatively, it's possible to usea
to store the values, but it's required to-
/~
the index so no byte is saved.
– user202729
yesterday
@DaveMongoose Tips for golfing in JavaScript
– user202729
yesterday
Too bad this isn't C++, otherwise something likea=>a.map(x=>++(a[-x]|=0))
would save a byte.
– user202729
yesterday
|
show 2 more comments
JavaScript (ES6), 26 bytes
1-indexed.
a=>a.map(o=x=>o[x]=-~o[x])
Try it online!
Commented
a => // a = input array
a.map(o = // assign the callback function of map() to the variable o, so that
// we have an object that can be used to store the counters
x => // for each value x in a:
o[x] = -~o[x] // increment o[x] and yield the result
// the '-~' syntax allows to go from undefined to 1
) // end of map()
JavaScript (ES6), 26 bytes
1-indexed.
a=>a.map(o=x=>o[x]=-~o[x])
Try it online!
Commented
a => // a = input array
a.map(o = // assign the callback function of map() to the variable o, so that
// we have an object that can be used to store the counters
x => // for each value x in a:
o[x] = -~o[x] // increment o[x] and yield the result
// the '-~' syntax allows to go from undefined to 1
) // end of map()
edited yesterday
answered yesterday
ArnauldArnauld
72.9k689307
72.9k689307
1
I have no idea how that works, but it sure looks elegant.
– Adám
yesterday
I've not seen-~
before - that is an absolute gem.
– DaveMongoose
yesterday
Alternatively, it's possible to usea
to store the values, but it's required to-
/~
the index so no byte is saved.
– user202729
yesterday
@DaveMongoose Tips for golfing in JavaScript
– user202729
yesterday
Too bad this isn't C++, otherwise something likea=>a.map(x=>++(a[-x]|=0))
would save a byte.
– user202729
yesterday
|
show 2 more comments
1
I have no idea how that works, but it sure looks elegant.
– Adám
yesterday
I've not seen-~
before - that is an absolute gem.
– DaveMongoose
yesterday
Alternatively, it's possible to usea
to store the values, but it's required to-
/~
the index so no byte is saved.
– user202729
yesterday
@DaveMongoose Tips for golfing in JavaScript
– user202729
yesterday
Too bad this isn't C++, otherwise something likea=>a.map(x=>++(a[-x]|=0))
would save a byte.
– user202729
yesterday
1
1
I have no idea how that works, but it sure looks elegant.
– Adám
yesterday
I have no idea how that works, but it sure looks elegant.
– Adám
yesterday
I've not seen
-~
before - that is an absolute gem.– DaveMongoose
yesterday
I've not seen
-~
before - that is an absolute gem.– DaveMongoose
yesterday
Alternatively, it's possible to use
a
to store the values, but it's required to -
/~
the index so no byte is saved.– user202729
yesterday
Alternatively, it's possible to use
a
to store the values, but it's required to -
/~
the index so no byte is saved.– user202729
yesterday
@DaveMongoose Tips for golfing in JavaScript
– user202729
yesterday
@DaveMongoose Tips for golfing in JavaScript
– user202729
yesterday
Too bad this isn't C++, otherwise something like
a=>a.map(x=>++(a[-x]|=0))
would save a byte.– user202729
yesterday
Too bad this isn't C++, otherwise something like
a=>a.map(x=>++(a[-x]|=0))
would save a byte.– user202729
yesterday
|
show 2 more comments
MATL, 4 bytes
&=Rs
This solution is 1-based
Try it out at MATL Online!
Explanation
Uses [1,2,3,2]
as an example
# Implicitly grab the input array of length N
#
# [1,2,3,2]
#
&= # Create an N x N boolean matrix by performing an element-wise comparison
# between the original array and its transpose:
#
# 1 2 3 2
# -------
# 1 | 1 0 0 0
# 2 | 0 1 0 1
# 3 | 0 0 1 0
# 2 | 0 1 0 1
#
R # Take the upper-triangular portion of this matrix (sets below-diagonal to 0)
#
# [1 0 0 0
# 0 1 0 1
# 0 0 1 0
# 0 0 0 1]
#
s # Compute the sum down the columns
#
# [1,1,1,2]
#
# Implicitly display the result
1
ah, I knew there was an old problem that made me think of something similar, it's Unique is Cheap, and the MATL solution there is one character different!
– Giuseppe
yesterday
add a comment |
MATL, 4 bytes
&=Rs
This solution is 1-based
Try it out at MATL Online!
Explanation
Uses [1,2,3,2]
as an example
# Implicitly grab the input array of length N
#
# [1,2,3,2]
#
&= # Create an N x N boolean matrix by performing an element-wise comparison
# between the original array and its transpose:
#
# 1 2 3 2
# -------
# 1 | 1 0 0 0
# 2 | 0 1 0 1
# 3 | 0 0 1 0
# 2 | 0 1 0 1
#
R # Take the upper-triangular portion of this matrix (sets below-diagonal to 0)
#
# [1 0 0 0
# 0 1 0 1
# 0 0 1 0
# 0 0 0 1]
#
s # Compute the sum down the columns
#
# [1,1,1,2]
#
# Implicitly display the result
1
ah, I knew there was an old problem that made me think of something similar, it's Unique is Cheap, and the MATL solution there is one character different!
– Giuseppe
yesterday
add a comment |
MATL, 4 bytes
&=Rs
This solution is 1-based
Try it out at MATL Online!
Explanation
Uses [1,2,3,2]
as an example
# Implicitly grab the input array of length N
#
# [1,2,3,2]
#
&= # Create an N x N boolean matrix by performing an element-wise comparison
# between the original array and its transpose:
#
# 1 2 3 2
# -------
# 1 | 1 0 0 0
# 2 | 0 1 0 1
# 3 | 0 0 1 0
# 2 | 0 1 0 1
#
R # Take the upper-triangular portion of this matrix (sets below-diagonal to 0)
#
# [1 0 0 0
# 0 1 0 1
# 0 0 1 0
# 0 0 0 1]
#
s # Compute the sum down the columns
#
# [1,1,1,2]
#
# Implicitly display the result
MATL, 4 bytes
&=Rs
This solution is 1-based
Try it out at MATL Online!
Explanation
Uses [1,2,3,2]
as an example
# Implicitly grab the input array of length N
#
# [1,2,3,2]
#
&= # Create an N x N boolean matrix by performing an element-wise comparison
# between the original array and its transpose:
#
# 1 2 3 2
# -------
# 1 | 1 0 0 0
# 2 | 0 1 0 1
# 3 | 0 0 1 0
# 2 | 0 1 0 1
#
R # Take the upper-triangular portion of this matrix (sets below-diagonal to 0)
#
# [1 0 0 0
# 0 1 0 1
# 0 0 1 0
# 0 0 0 1]
#
s # Compute the sum down the columns
#
# [1,1,1,2]
#
# Implicitly display the result
edited yesterday
answered yesterday
SueverSuever
9,6021345
9,6021345
1
ah, I knew there was an old problem that made me think of something similar, it's Unique is Cheap, and the MATL solution there is one character different!
– Giuseppe
yesterday
add a comment |
1
ah, I knew there was an old problem that made me think of something similar, it's Unique is Cheap, and the MATL solution there is one character different!
– Giuseppe
yesterday
1
1
ah, I knew there was an old problem that made me think of something similar, it's Unique is Cheap, and the MATL solution there is one character different!
– Giuseppe
yesterday
ah, I knew there was an old problem that made me think of something similar, it's Unique is Cheap, and the MATL solution there is one character different!
– Giuseppe
yesterday
add a comment |
APL (Dyalog Unicode), 7 bytes
Many, many thanks to H.PWiz, Adám and dzaima for all their help in debugging and correcting this.
+/¨⊢=,
Try it online!
Explanation
The 10-byte non-tacit version will be easier to explain first
{+/¨⍵=,⍵}
{ } A user-defined function, a dfn
,⍵ The list of prefixes of our input list ⍵
(⍵ more generally means the right argument of a dfn)
is 'scan' which both gives us our prefixes
and applies ,/ over each prefix, which keeps each prefix as-is
⍵= Checks each element of ⍵ against its corresponding prefix
This checks each prefix for occurrences of the last element of that prefix
This gives us several lists of 0s and 1s
+/¨ This sums over each list of 0s and 1s to give us the enumeration we are looking for
The tacit version does three things
- First, it removes the instance of
⍵
used in,⍵
as,
on the right by itself can implicitly figure out that it's supposed to operate on the right argument. - Second, for
⍵=
, we replace the⍵
with⊢
, which stands for right argument - Third, now that we have no explicit arguments (in this case,
⍵
), we can remove the braces{}
as tacit functions do not use them
add a comment |
APL (Dyalog Unicode), 7 bytes
Many, many thanks to H.PWiz, Adám and dzaima for all their help in debugging and correcting this.
+/¨⊢=,
Try it online!
Explanation
The 10-byte non-tacit version will be easier to explain first
{+/¨⍵=,⍵}
{ } A user-defined function, a dfn
,⍵ The list of prefixes of our input list ⍵
(⍵ more generally means the right argument of a dfn)
is 'scan' which both gives us our prefixes
and applies ,/ over each prefix, which keeps each prefix as-is
⍵= Checks each element of ⍵ against its corresponding prefix
This checks each prefix for occurrences of the last element of that prefix
This gives us several lists of 0s and 1s
+/¨ This sums over each list of 0s and 1s to give us the enumeration we are looking for
The tacit version does three things
- First, it removes the instance of
⍵
used in,⍵
as,
on the right by itself can implicitly figure out that it's supposed to operate on the right argument. - Second, for
⍵=
, we replace the⍵
with⊢
, which stands for right argument - Third, now that we have no explicit arguments (in this case,
⍵
), we can remove the braces{}
as tacit functions do not use them
add a comment |
APL (Dyalog Unicode), 7 bytes
Many, many thanks to H.PWiz, Adám and dzaima for all their help in debugging and correcting this.
+/¨⊢=,
Try it online!
Explanation
The 10-byte non-tacit version will be easier to explain first
{+/¨⍵=,⍵}
{ } A user-defined function, a dfn
,⍵ The list of prefixes of our input list ⍵
(⍵ more generally means the right argument of a dfn)
is 'scan' which both gives us our prefixes
and applies ,/ over each prefix, which keeps each prefix as-is
⍵= Checks each element of ⍵ against its corresponding prefix
This checks each prefix for occurrences of the last element of that prefix
This gives us several lists of 0s and 1s
+/¨ This sums over each list of 0s and 1s to give us the enumeration we are looking for
The tacit version does three things
- First, it removes the instance of
⍵
used in,⍵
as,
on the right by itself can implicitly figure out that it's supposed to operate on the right argument. - Second, for
⍵=
, we replace the⍵
with⊢
, which stands for right argument - Third, now that we have no explicit arguments (in this case,
⍵
), we can remove the braces{}
as tacit functions do not use them
APL (Dyalog Unicode), 7 bytes
Many, many thanks to H.PWiz, Adám and dzaima for all their help in debugging and correcting this.
+/¨⊢=,
Try it online!
Explanation
The 10-byte non-tacit version will be easier to explain first
{+/¨⍵=,⍵}
{ } A user-defined function, a dfn
,⍵ The list of prefixes of our input list ⍵
(⍵ more generally means the right argument of a dfn)
is 'scan' which both gives us our prefixes
and applies ,/ over each prefix, which keeps each prefix as-is
⍵= Checks each element of ⍵ against its corresponding prefix
This checks each prefix for occurrences of the last element of that prefix
This gives us several lists of 0s and 1s
+/¨ This sums over each list of 0s and 1s to give us the enumeration we are looking for
The tacit version does three things
- First, it removes the instance of
⍵
used in,⍵
as,
on the right by itself can implicitly figure out that it's supposed to operate on the right argument. - Second, for
⍵=
, we replace the⍵
with⊢
, which stands for right argument - Third, now that we have no explicit arguments (in this case,
⍵
), we can remove the braces{}
as tacit functions do not use them
edited yesterday
answered yesterday
Sherlock9Sherlock9
7,86411859
7,86411859
add a comment |
add a comment |
J, 7 bytes
1#.]=]
Try it online!
1-indexed.
Explanation:
] all the prefixes (filled with zeros, but there won't be any 0s in the input):
] 5 12 10 12 12 10
5 0 0 0 0 0
5 12 0 0 0 0
5 12 10 0 0 0
5 12 10 12 0 0
5 12 10 12 12 0
5 12 10 12 12 10
]= is each number from the input equal to the prefix:
(]=]) 5 12 10 12 12 10
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 1 0 1 0 0
0 1 0 1 1 0
0 0 1 0 0 1
1#. sum each row:
(1#.]=]) 5 12 10 12 12 10
1 1 1 2 3 2
K (oK), 11 bytes
{+/'x=',x}
Try it online!
1
Heh, you're happy that I made the data strictly positive…
– Adám
yesterday
@Adám Yes, otherwise I'd need to box the prefixes :)
– Galen Ivanov
yesterday
add a comment |
J, 7 bytes
1#.]=]
Try it online!
1-indexed.
Explanation:
] all the prefixes (filled with zeros, but there won't be any 0s in the input):
] 5 12 10 12 12 10
5 0 0 0 0 0
5 12 0 0 0 0
5 12 10 0 0 0
5 12 10 12 0 0
5 12 10 12 12 0
5 12 10 12 12 10
]= is each number from the input equal to the prefix:
(]=]) 5 12 10 12 12 10
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 1 0 1 0 0
0 1 0 1 1 0
0 0 1 0 0 1
1#. sum each row:
(1#.]=]) 5 12 10 12 12 10
1 1 1 2 3 2
K (oK), 11 bytes
{+/'x=',x}
Try it online!
1
Heh, you're happy that I made the data strictly positive…
– Adám
yesterday
@Adám Yes, otherwise I'd need to box the prefixes :)
– Galen Ivanov
yesterday
add a comment |
J, 7 bytes
1#.]=]
Try it online!
1-indexed.
Explanation:
] all the prefixes (filled with zeros, but there won't be any 0s in the input):
] 5 12 10 12 12 10
5 0 0 0 0 0
5 12 0 0 0 0
5 12 10 0 0 0
5 12 10 12 0 0
5 12 10 12 12 0
5 12 10 12 12 10
]= is each number from the input equal to the prefix:
(]=]) 5 12 10 12 12 10
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 1 0 1 0 0
0 1 0 1 1 0
0 0 1 0 0 1
1#. sum each row:
(1#.]=]) 5 12 10 12 12 10
1 1 1 2 3 2
K (oK), 11 bytes
{+/'x=',x}
Try it online!
J, 7 bytes
1#.]=]
Try it online!
1-indexed.
Explanation:
] all the prefixes (filled with zeros, but there won't be any 0s in the input):
] 5 12 10 12 12 10
5 0 0 0 0 0
5 12 0 0 0 0
5 12 10 0 0 0
5 12 10 12 0 0
5 12 10 12 12 0
5 12 10 12 12 10
]= is each number from the input equal to the prefix:
(]=]) 5 12 10 12 12 10
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 1 0 1 0 0
0 1 0 1 1 0
0 0 1 0 0 1
1#. sum each row:
(1#.]=]) 5 12 10 12 12 10
1 1 1 2 3 2
K (oK), 11 bytes
{+/'x=',x}
Try it online!
edited 20 hours ago
answered yesterday
Galen IvanovGalen Ivanov
6,43711032
6,43711032
1
Heh, you're happy that I made the data strictly positive…
– Adám
yesterday
@Adám Yes, otherwise I'd need to box the prefixes :)
– Galen Ivanov
yesterday
add a comment |
1
Heh, you're happy that I made the data strictly positive…
– Adám
yesterday
@Adám Yes, otherwise I'd need to box the prefixes :)
– Galen Ivanov
yesterday
1
1
Heh, you're happy that I made the data strictly positive…
– Adám
yesterday
Heh, you're happy that I made the data strictly positive…
– Adám
yesterday
@Adám Yes, otherwise I'd need to box the prefixes :)
– Galen Ivanov
yesterday
@Adám Yes, otherwise I'd need to box the prefixes :)
– Galen Ivanov
yesterday
add a comment |
Python 2, 48 bytes
lambda a:[a[:i].count(v)for i,v in enumerate(a)]
Try it online!
add a comment |
Python 2, 48 bytes
lambda a:[a[:i].count(v)for i,v in enumerate(a)]
Try it online!
add a comment |
Python 2, 48 bytes
lambda a:[a[:i].count(v)for i,v in enumerate(a)]
Try it online!
Python 2, 48 bytes
lambda a:[a[:i].count(v)for i,v in enumerate(a)]
Try it online!
answered yesterday
TFeldTFeld
14.4k21240
14.4k21240
add a comment |
add a comment |
05AB1E, 4 bytes
ηε¤¢
Try it online!
or as a Test Suite
Explanation
ηε # apply to each prefix of the input list
¤¢ # count occurrences of the last element
add a comment |
05AB1E, 4 bytes
ηε¤¢
Try it online!
or as a Test Suite
Explanation
ηε # apply to each prefix of the input list
¤¢ # count occurrences of the last element
add a comment |
05AB1E, 4 bytes
ηε¤¢
Try it online!
or as a Test Suite
Explanation
ηε # apply to each prefix of the input list
¤¢ # count occurrences of the last element
05AB1E, 4 bytes
ηε¤¢
Try it online!
or as a Test Suite
Explanation
ηε # apply to each prefix of the input list
¤¢ # count occurrences of the last element
answered yesterday
EmignaEmigna
45.5k432138
45.5k432138
add a comment |
add a comment |
AWK, 15
{print a[$1]++}
Try it online!
The above does zero-based indexing. If you prefer one-based indexing, its a simple change:
{print ++a[$1]}
Try it online!
add a comment |
AWK, 15
{print a[$1]++}
Try it online!
The above does zero-based indexing. If you prefer one-based indexing, its a simple change:
{print ++a[$1]}
Try it online!
add a comment |
AWK, 15
{print a[$1]++}
Try it online!
The above does zero-based indexing. If you prefer one-based indexing, its a simple change:
{print ++a[$1]}
Try it online!
AWK, 15
{print a[$1]++}
Try it online!
The above does zero-based indexing. If you prefer one-based indexing, its a simple change:
{print ++a[$1]}
Try it online!
edited yesterday
answered yesterday
Digital TraumaDigital Trauma
58.6k787221
58.6k787221
add a comment |
add a comment |
Python 2, 47 43 bytes
f=lambda a:a and f(a[:-1])+[a.count(a[-1])]
Try it online!
A recursive 'one-based' solution.
add a comment |
Python 2, 47 43 bytes
f=lambda a:a and f(a[:-1])+[a.count(a[-1])]
Try it online!
A recursive 'one-based' solution.
add a comment |
Python 2, 47 43 bytes
f=lambda a:a and f(a[:-1])+[a.count(a[-1])]
Try it online!
A recursive 'one-based' solution.
Python 2, 47 43 bytes
f=lambda a:a and f(a[:-1])+[a.count(a[-1])]
Try it online!
A recursive 'one-based' solution.
edited yesterday
answered yesterday
Chas BrownChas Brown
4,8391522
4,8391522
add a comment |
add a comment |
Jelly, 4 bytes
ċṪ$Ƥ
Try it online!
For each prefix of the input list, it counts the number of occurrences of its last element in itself.
Alternatively the old-school;ċ"
is also 4.
– Jonathan Allan
yesterday
add a comment |
Jelly, 4 bytes
ċṪ$Ƥ
Try it online!
For each prefix of the input list, it counts the number of occurrences of its last element in itself.
Alternatively the old-school;ċ"
is also 4.
– Jonathan Allan
yesterday
add a comment |
Jelly, 4 bytes
ċṪ$Ƥ
Try it online!
For each prefix of the input list, it counts the number of occurrences of its last element in itself.
Jelly, 4 bytes
ċṪ$Ƥ
Try it online!
For each prefix of the input list, it counts the number of occurrences of its last element in itself.
answered yesterday
Mr. XcoderMr. Xcoder
31.7k759198
31.7k759198
Alternatively the old-school;ċ"
is also 4.
– Jonathan Allan
yesterday
add a comment |
Alternatively the old-school;ċ"
is also 4.
– Jonathan Allan
yesterday
Alternatively the old-school
;ċ"
is also 4.– Jonathan Allan
yesterday
Alternatively the old-school
;ċ"
is also 4.– Jonathan Allan
yesterday
add a comment |
C# (Visual C# Interactive Compiler), 44 bytes
x=>x.Select((y,i)=>x.Take(i).Count(z=>z==y))
Try it online!
22 bytes
– LiefdeWen
yesterday
You have the inversed of the challenge right now..[7,7,7]
should output[0,1,2]
, and not[0,0,0]
.
– Kevin Cruijssen
yesterday
1
@KevinCruijssen - Thanks :) Looks like I misread things, it should be fixed now.
– dana
yesterday
add a comment |
C# (Visual C# Interactive Compiler), 44 bytes
x=>x.Select((y,i)=>x.Take(i).Count(z=>z==y))
Try it online!
22 bytes
– LiefdeWen
yesterday
You have the inversed of the challenge right now..[7,7,7]
should output[0,1,2]
, and not[0,0,0]
.
– Kevin Cruijssen
yesterday
1
@KevinCruijssen - Thanks :) Looks like I misread things, it should be fixed now.
– dana
yesterday
add a comment |
C# (Visual C# Interactive Compiler), 44 bytes
x=>x.Select((y,i)=>x.Take(i).Count(z=>z==y))
Try it online!
C# (Visual C# Interactive Compiler), 44 bytes
x=>x.Select((y,i)=>x.Take(i).Count(z=>z==y))
Try it online!
edited yesterday
answered yesterday
danadana
55135
55135
22 bytes
– LiefdeWen
yesterday
You have the inversed of the challenge right now..[7,7,7]
should output[0,1,2]
, and not[0,0,0]
.
– Kevin Cruijssen
yesterday
1
@KevinCruijssen - Thanks :) Looks like I misread things, it should be fixed now.
– dana
yesterday
add a comment |
22 bytes
– LiefdeWen
yesterday
You have the inversed of the challenge right now..[7,7,7]
should output[0,1,2]
, and not[0,0,0]
.
– Kevin Cruijssen
yesterday
1
@KevinCruijssen - Thanks :) Looks like I misread things, it should be fixed now.
– dana
yesterday
22 bytes
– LiefdeWen
yesterday
22 bytes
– LiefdeWen
yesterday
You have the inversed of the challenge right now..
[7,7,7]
should output [0,1,2]
, and not [0,0,0]
.– Kevin Cruijssen
yesterday
You have the inversed of the challenge right now..
[7,7,7]
should output [0,1,2]
, and not [0,0,0]
.– Kevin Cruijssen
yesterday
1
1
@KevinCruijssen - Thanks :) Looks like I misread things, it should be fixed now.
– dana
yesterday
@KevinCruijssen - Thanks :) Looks like I misread things, it should be fixed now.
– dana
yesterday
add a comment |
Ruby, 35 bytes
->a{f=Hash.new 0;a.map{|v|f[v]+=1}}
It's pretty mundane, unfortunately - build a hash that stores the total for each entry encountered so far.
Some other, fun options that unfortunately weren't quite short enough:
->a{a.dup.map{a.count a.pop}.reverse} # 37
->a{i=-1;a.map{|v|a[0..i+=1].count v}} # 38
add a comment |
Ruby, 35 bytes
->a{f=Hash.new 0;a.map{|v|f[v]+=1}}
It's pretty mundane, unfortunately - build a hash that stores the total for each entry encountered so far.
Some other, fun options that unfortunately weren't quite short enough:
->a{a.dup.map{a.count a.pop}.reverse} # 37
->a{i=-1;a.map{|v|a[0..i+=1].count v}} # 38
add a comment |
Ruby, 35 bytes
->a{f=Hash.new 0;a.map{|v|f[v]+=1}}
It's pretty mundane, unfortunately - build a hash that stores the total for each entry encountered so far.
Some other, fun options that unfortunately weren't quite short enough:
->a{a.dup.map{a.count a.pop}.reverse} # 37
->a{i=-1;a.map{|v|a[0..i+=1].count v}} # 38
Ruby, 35 bytes
->a{f=Hash.new 0;a.map{|v|f[v]+=1}}
It's pretty mundane, unfortunately - build a hash that stores the total for each entry encountered so far.
Some other, fun options that unfortunately weren't quite short enough:
->a{a.dup.map{a.count a.pop}.reverse} # 37
->a{i=-1;a.map{|v|a[0..i+=1].count v}} # 38
answered yesterday
DaveMongooseDaveMongoose
1414
1414
add a comment |
add a comment |
Perl 6, 15 bytes
*>>.&{%.{$_}++}
Try it online!
You can move the ++
to before the %
for a one based index.
Explanation:
*>>.&{ } # Map the input to
% # An anonymous hash
.{$_} # The current element indexed
++ # Incremented
add a comment |
Perl 6, 15 bytes
*>>.&{%.{$_}++}
Try it online!
You can move the ++
to before the %
for a one based index.
Explanation:
*>>.&{ } # Map the input to
% # An anonymous hash
.{$_} # The current element indexed
++ # Incremented
add a comment |
Perl 6, 15 bytes
*>>.&{%.{$_}++}
Try it online!
You can move the ++
to before the %
for a one based index.
Explanation:
*>>.&{ } # Map the input to
% # An anonymous hash
.{$_} # The current element indexed
++ # Incremented
Perl 6, 15 bytes
*>>.&{%.{$_}++}
Try it online!
You can move the ++
to before the %
for a one based index.
Explanation:
*>>.&{ } # Map the input to
% # An anonymous hash
.{$_} # The current element indexed
++ # Incremented
edited 19 hours ago
answered yesterday
Jo KingJo King
21.1k248110
21.1k248110
add a comment |
add a comment |
R, 41 bytes
function(x)diag(diffinv(outer(x,x,"==")))
Try it online!
Oddly, returning a zero-based index is shorter in R.
Once again Giuseppe, your superior knowledge of R has beaten me. I had a decently ingenious method at 60 bytes, but alas, it wasn't enough!
– Sumner18
yesterday
@Sumner18 post it anyway! I always learn a lot from other peoples' approaches, and getting feedback is the fastest way to learn!
– Giuseppe
yesterday
thanks for the encouragement! I've posted mine now and am always open to suggestions for improvement!
– Sumner18
yesterday
add a comment |
R, 41 bytes
function(x)diag(diffinv(outer(x,x,"==")))
Try it online!
Oddly, returning a zero-based index is shorter in R.
Once again Giuseppe, your superior knowledge of R has beaten me. I had a decently ingenious method at 60 bytes, but alas, it wasn't enough!
– Sumner18
yesterday
@Sumner18 post it anyway! I always learn a lot from other peoples' approaches, and getting feedback is the fastest way to learn!
– Giuseppe
yesterday
thanks for the encouragement! I've posted mine now and am always open to suggestions for improvement!
– Sumner18
yesterday
add a comment |
R, 41 bytes
function(x)diag(diffinv(outer(x,x,"==")))
Try it online!
Oddly, returning a zero-based index is shorter in R.
R, 41 bytes
function(x)diag(diffinv(outer(x,x,"==")))
Try it online!
Oddly, returning a zero-based index is shorter in R.
answered yesterday
GiuseppeGiuseppe
16.6k31052
16.6k31052
Once again Giuseppe, your superior knowledge of R has beaten me. I had a decently ingenious method at 60 bytes, but alas, it wasn't enough!
– Sumner18
yesterday
@Sumner18 post it anyway! I always learn a lot from other peoples' approaches, and getting feedback is the fastest way to learn!
– Giuseppe
yesterday
thanks for the encouragement! I've posted mine now and am always open to suggestions for improvement!
– Sumner18
yesterday
add a comment |
Once again Giuseppe, your superior knowledge of R has beaten me. I had a decently ingenious method at 60 bytes, but alas, it wasn't enough!
– Sumner18
yesterday
@Sumner18 post it anyway! I always learn a lot from other peoples' approaches, and getting feedback is the fastest way to learn!
– Giuseppe
yesterday
thanks for the encouragement! I've posted mine now and am always open to suggestions for improvement!
– Sumner18
yesterday
Once again Giuseppe, your superior knowledge of R has beaten me. I had a decently ingenious method at 60 bytes, but alas, it wasn't enough!
– Sumner18
yesterday
Once again Giuseppe, your superior knowledge of R has beaten me. I had a decently ingenious method at 60 bytes, but alas, it wasn't enough!
– Sumner18
yesterday
@Sumner18 post it anyway! I always learn a lot from other peoples' approaches, and getting feedback is the fastest way to learn!
– Giuseppe
yesterday
@Sumner18 post it anyway! I always learn a lot from other peoples' approaches, and getting feedback is the fastest way to learn!
– Giuseppe
yesterday
thanks for the encouragement! I've posted mine now and am always open to suggestions for improvement!
– Sumner18
yesterday
thanks for the encouragement! I've posted mine now and am always open to suggestions for improvement!
– Sumner18
yesterday
add a comment |
Java (JDK), 76 bytes
a->{for(int l=a.length,i,c;l-->0;a[l]=c)for(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;}
Try it online!
Credits
- -2 bytes thanks to Kevin Cruijssen
1
-2 bytes by changingfor(c=0,i=l;i-->0;)c+=a[l]==a[i]?1:0;
tofor(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;
.
– Kevin Cruijssen
yesterday
add a comment |
Java (JDK), 76 bytes
a->{for(int l=a.length,i,c;l-->0;a[l]=c)for(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;}
Try it online!
Credits
- -2 bytes thanks to Kevin Cruijssen
1
-2 bytes by changingfor(c=0,i=l;i-->0;)c+=a[l]==a[i]?1:0;
tofor(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;
.
– Kevin Cruijssen
yesterday
add a comment |
Java (JDK), 76 bytes
a->{for(int l=a.length,i,c;l-->0;a[l]=c)for(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;}
Try it online!
Credits
- -2 bytes thanks to Kevin Cruijssen
Java (JDK), 76 bytes
a->{for(int l=a.length,i,c;l-->0;a[l]=c)for(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;}
Try it online!
Credits
- -2 bytes thanks to Kevin Cruijssen
edited yesterday
answered yesterday
Olivier GrégoireOlivier Grégoire
8,83711843
8,83711843
1
-2 bytes by changingfor(c=0,i=l;i-->0;)c+=a[l]==a[i]?1:0;
tofor(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;
.
– Kevin Cruijssen
yesterday
add a comment |
1
-2 bytes by changingfor(c=0,i=l;i-->0;)c+=a[l]==a[i]?1:0;
tofor(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;
.
– Kevin Cruijssen
yesterday
1
1
-2 bytes by changing
for(c=0,i=l;i-->0;)c+=a[l]==a[i]?1:0;
to for(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;
.– Kevin Cruijssen
yesterday
-2 bytes by changing
for(c=0,i=l;i-->0;)c+=a[l]==a[i]?1:0;
to for(c=i=0;i<l;)c+=a[l]==a[i++]?1:0;
.– Kevin Cruijssen
yesterday
add a comment |
R, 62 43 bytes
x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
-19 bytes thanks to Giuseppe, by removing which, and table, and only slight changes to the implementation
Original
x=z=scan();for(i in names(r<-table(x)))z[which(x==i)]=1:r[i];z
I can't compete with Giuseppe's knowledge, so my submission is somewhat longer than his, but using my basic knowledge, I felt that this solution was rather ingenious.
r<-table(x)
counts the number of times each number appears and stores it in r, for future reference
names()
gets the values of each unique entry in the table, and we iterate over these names with a for loop.
The remaining portion checks which entries are equal to the iterations and stores a sequence of values (from 1 to the number of entries of the iteration)
Try it online!
you can remove thewhich()
to save 7 bytes.
– Giuseppe
yesterday
Your use of1:r[i]
gave me the idea to just removetable()
entirely:x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
is 43 bytes! This is a nice approach!
– Giuseppe
yesterday
add a comment |
R, 62 43 bytes
x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
-19 bytes thanks to Giuseppe, by removing which, and table, and only slight changes to the implementation
Original
x=z=scan();for(i in names(r<-table(x)))z[which(x==i)]=1:r[i];z
I can't compete with Giuseppe's knowledge, so my submission is somewhat longer than his, but using my basic knowledge, I felt that this solution was rather ingenious.
r<-table(x)
counts the number of times each number appears and stores it in r, for future reference
names()
gets the values of each unique entry in the table, and we iterate over these names with a for loop.
The remaining portion checks which entries are equal to the iterations and stores a sequence of values (from 1 to the number of entries of the iteration)
Try it online!
you can remove thewhich()
to save 7 bytes.
– Giuseppe
yesterday
Your use of1:r[i]
gave me the idea to just removetable()
entirely:x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
is 43 bytes! This is a nice approach!
– Giuseppe
yesterday
add a comment |
R, 62 43 bytes
x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
-19 bytes thanks to Giuseppe, by removing which, and table, and only slight changes to the implementation
Original
x=z=scan();for(i in names(r<-table(x)))z[which(x==i)]=1:r[i];z
I can't compete with Giuseppe's knowledge, so my submission is somewhat longer than his, but using my basic knowledge, I felt that this solution was rather ingenious.
r<-table(x)
counts the number of times each number appears and stores it in r, for future reference
names()
gets the values of each unique entry in the table, and we iterate over these names with a for loop.
The remaining portion checks which entries are equal to the iterations and stores a sequence of values (from 1 to the number of entries of the iteration)
Try it online!
R, 62 43 bytes
x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
-19 bytes thanks to Giuseppe, by removing which, and table, and only slight changes to the implementation
Original
x=z=scan();for(i in names(r<-table(x)))z[which(x==i)]=1:r[i];z
I can't compete with Giuseppe's knowledge, so my submission is somewhat longer than his, but using my basic knowledge, I felt that this solution was rather ingenious.
r<-table(x)
counts the number of times each number appears and stores it in r, for future reference
names()
gets the values of each unique entry in the table, and we iterate over these names with a for loop.
The remaining portion checks which entries are equal to the iterations and stores a sequence of values (from 1 to the number of entries of the iteration)
Try it online!
edited yesterday
answered yesterday
Sumner18Sumner18
4007
4007
you can remove thewhich()
to save 7 bytes.
– Giuseppe
yesterday
Your use of1:r[i]
gave me the idea to just removetable()
entirely:x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
is 43 bytes! This is a nice approach!
– Giuseppe
yesterday
add a comment |
you can remove thewhich()
to save 7 bytes.
– Giuseppe
yesterday
Your use of1:r[i]
gave me the idea to just removetable()
entirely:x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
is 43 bytes! This is a nice approach!
– Giuseppe
yesterday
you can remove the
which()
to save 7 bytes.– Giuseppe
yesterday
you can remove the
which()
to save 7 bytes.– Giuseppe
yesterday
Your use of
1:r[i]
gave me the idea to just remove table()
entirely: x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
is 43 bytes! This is a nice approach!– Giuseppe
yesterday
Your use of
1:r[i]
gave me the idea to just remove table()
entirely: x=z=scan();for(i in x)z[y]=1:sum(y<-x==i);z
is 43 bytes! This is a nice approach!– Giuseppe
yesterday
add a comment |
Ruby, 34 bytes
->a{r=;a.map{|x|(r<<x).count x}}
Try it online!
I can't believe I tried->a{i=-1;a.map{|v|a[0..i+=1].count v}}
and didn't think of just building a new array, lol. Nice work.
– DaveMongoose
19 hours ago
add a comment |
Ruby, 34 bytes
->a{r=;a.map{|x|(r<<x).count x}}
Try it online!
I can't believe I tried->a{i=-1;a.map{|v|a[0..i+=1].count v}}
and didn't think of just building a new array, lol. Nice work.
– DaveMongoose
19 hours ago
add a comment |
Ruby, 34 bytes
->a{r=;a.map{|x|(r<<x).count x}}
Try it online!
Ruby, 34 bytes
->a{r=;a.map{|x|(r<<x).count x}}
Try it online!
answered yesterday
G BG B
7,6961328
7,6961328
I can't believe I tried->a{i=-1;a.map{|v|a[0..i+=1].count v}}
and didn't think of just building a new array, lol. Nice work.
– DaveMongoose
19 hours ago
add a comment |
I can't believe I tried->a{i=-1;a.map{|v|a[0..i+=1].count v}}
and didn't think of just building a new array, lol. Nice work.
– DaveMongoose
19 hours ago
I can't believe I tried
->a{i=-1;a.map{|v|a[0..i+=1].count v}}
and didn't think of just building a new array, lol. Nice work.– DaveMongoose
19 hours ago
I can't believe I tried
->a{i=-1;a.map{|v|a[0..i+=1].count v}}
and didn't think of just building a new array, lol. Nice work.– DaveMongoose
19 hours ago
add a comment |
bash, 37 bytes or 24 bytes
f()(for x;{ r+=$[a[x]++] ;};echo $r)
TIO
if valid, there is also this variation, as suggested by DigitalTrauma
for x;{ echo $[a[x]++];}
TIO
1
Pass the list as command line args - tio.run/##S0oszvj/Py2/SKHCuporNTkjX0ElOjG6IlZbO5ar9v///8b/… - only 24 bytes.
– Digital Trauma
yesterday
@DigitalTrauma, thanks however i don't know if it broke the rules. also as it was asked to replace list and maybe should be something like tio.run/…
– Nahuel Fouilleul
21 hours ago
1
@NahuelFouilleul It's fine, full programs are allowed too, and that's a valid method of inputting/outputting a list (IMO)
– ASCII-only
21 hours ago
add a comment |
bash, 37 bytes or 24 bytes
f()(for x;{ r+=$[a[x]++] ;};echo $r)
TIO
if valid, there is also this variation, as suggested by DigitalTrauma
for x;{ echo $[a[x]++];}
TIO
1
Pass the list as command line args - tio.run/##S0oszvj/Py2/SKHCuporNTkjX0ElOjG6IlZbO5ar9v///8b/… - only 24 bytes.
– Digital Trauma
yesterday
@DigitalTrauma, thanks however i don't know if it broke the rules. also as it was asked to replace list and maybe should be something like tio.run/…
– Nahuel Fouilleul
21 hours ago
1
@NahuelFouilleul It's fine, full programs are allowed too, and that's a valid method of inputting/outputting a list (IMO)
– ASCII-only
21 hours ago
add a comment |
bash, 37 bytes or 24 bytes
f()(for x;{ r+=$[a[x]++] ;};echo $r)
TIO
if valid, there is also this variation, as suggested by DigitalTrauma
for x;{ echo $[a[x]++];}
TIO
bash, 37 bytes or 24 bytes
f()(for x;{ r+=$[a[x]++] ;};echo $r)
TIO
if valid, there is also this variation, as suggested by DigitalTrauma
for x;{ echo $[a[x]++];}
TIO
edited 21 hours ago
answered yesterday
Nahuel FouilleulNahuel Fouilleul
1,61228
1,61228
1
Pass the list as command line args - tio.run/##S0oszvj/Py2/SKHCuporNTkjX0ElOjG6IlZbO5ar9v///8b/… - only 24 bytes.
– Digital Trauma
yesterday
@DigitalTrauma, thanks however i don't know if it broke the rules. also as it was asked to replace list and maybe should be something like tio.run/…
– Nahuel Fouilleul
21 hours ago
1
@NahuelFouilleul It's fine, full programs are allowed too, and that's a valid method of inputting/outputting a list (IMO)
– ASCII-only
21 hours ago
add a comment |
1
Pass the list as command line args - tio.run/##S0oszvj/Py2/SKHCuporNTkjX0ElOjG6IlZbO5ar9v///8b/… - only 24 bytes.
– Digital Trauma
yesterday
@DigitalTrauma, thanks however i don't know if it broke the rules. also as it was asked to replace list and maybe should be something like tio.run/…
– Nahuel Fouilleul
21 hours ago
1
@NahuelFouilleul It's fine, full programs are allowed too, and that's a valid method of inputting/outputting a list (IMO)
– ASCII-only
21 hours ago
1
1
Pass the list as command line args - tio.run/##S0oszvj/Py2/SKHCuporNTkjX0ElOjG6IlZbO5ar9v///8b/… - only 24 bytes.
– Digital Trauma
yesterday
Pass the list as command line args - tio.run/##S0oszvj/Py2/SKHCuporNTkjX0ElOjG6IlZbO5ar9v///8b/… - only 24 bytes.
– Digital Trauma
yesterday
@DigitalTrauma, thanks however i don't know if it broke the rules. also as it was asked to replace list and maybe should be something like tio.run/…
– Nahuel Fouilleul
21 hours ago
@DigitalTrauma, thanks however i don't know if it broke the rules. also as it was asked to replace list and maybe should be something like tio.run/…
– Nahuel Fouilleul
21 hours ago
1
1
@NahuelFouilleul It's fine, full programs are allowed too, and that's a valid method of inputting/outputting a list (IMO)
– ASCII-only
21 hours ago
@NahuelFouilleul It's fine, full programs are allowed too, and that's a valid method of inputting/outputting a list (IMO)
– ASCII-only
21 hours ago
add a comment |
Retina 0.8.2, 30 bytes
b(d+)b(?<=(b1b.*?)+)
$#2
Try it online! Link includes test cases. 1-indexed. Explanation: The first part of the regex matches each integer in the list in turn. The lookbehind's group matches each occurrence of that integer on that line up to and including the current integer. The integer is then substituted with the number of matches.
add a comment |
Retina 0.8.2, 30 bytes
b(d+)b(?<=(b1b.*?)+)
$#2
Try it online! Link includes test cases. 1-indexed. Explanation: The first part of the regex matches each integer in the list in turn. The lookbehind's group matches each occurrence of that integer on that line up to and including the current integer. The integer is then substituted with the number of matches.
add a comment |
Retina 0.8.2, 30 bytes
b(d+)b(?<=(b1b.*?)+)
$#2
Try it online! Link includes test cases. 1-indexed. Explanation: The first part of the regex matches each integer in the list in turn. The lookbehind's group matches each occurrence of that integer on that line up to and including the current integer. The integer is then substituted with the number of matches.
Retina 0.8.2, 30 bytes
b(d+)b(?<=(b1b.*?)+)
$#2
Try it online! Link includes test cases. 1-indexed. Explanation: The first part of the regex matches each integer in the list in turn. The lookbehind's group matches each occurrence of that integer on that line up to and including the current integer. The integer is then substituted with the number of matches.
answered yesterday
NeilNeil
79.6k744177
79.6k744177
add a comment |
add a comment |
Batch, 61 bytes
@setlocal
@for %%n in (%*)do @set/ac=c%%n+=1&call echo %%c%%
1-indexed. Because variable substitution happens before parsing, the set/a
command ends up incrementing the variable name given by concatenating the letter c
with the integer from the list (numeric variables default to zero in Batch). The result is then copied to another integer for ease of output (more precisely, it saves a byte).
add a comment |
Batch, 61 bytes
@setlocal
@for %%n in (%*)do @set/ac=c%%n+=1&call echo %%c%%
1-indexed. Because variable substitution happens before parsing, the set/a
command ends up incrementing the variable name given by concatenating the letter c
with the integer from the list (numeric variables default to zero in Batch). The result is then copied to another integer for ease of output (more precisely, it saves a byte).
add a comment |
Batch, 61 bytes
@setlocal
@for %%n in (%*)do @set/ac=c%%n+=1&call echo %%c%%
1-indexed. Because variable substitution happens before parsing, the set/a
command ends up incrementing the variable name given by concatenating the letter c
with the integer from the list (numeric variables default to zero in Batch). The result is then copied to another integer for ease of output (more precisely, it saves a byte).
Batch, 61 bytes
@setlocal
@for %%n in (%*)do @set/ac=c%%n+=1&call echo %%c%%
1-indexed. Because variable substitution happens before parsing, the set/a
command ends up incrementing the variable name given by concatenating the letter c
with the integer from the list (numeric variables default to zero in Batch). The result is then copied to another integer for ease of output (more precisely, it saves a byte).
answered yesterday
NeilNeil
79.6k744177
79.6k744177
add a comment |
add a comment |
awk (46 bytes)
{delete(a);for(i=1;i<=NF;i++)$i=a[$i]++;print}
TIO
add a comment |
awk (46 bytes)
{delete(a);for(i=1;i<=NF;i++)$i=a[$i]++;print}
TIO
add a comment |
awk (46 bytes)
{delete(a);for(i=1;i<=NF;i++)$i=a[$i]++;print}
TIO
awk (46 bytes)
{delete(a);for(i=1;i<=NF;i++)$i=a[$i]++;print}
TIO
answered yesterday
Nahuel FouilleulNahuel Fouilleul
1,61228
1,61228
add a comment |
add a comment |
Tcl, 48 bytes
proc C L {lmap n $L {dict g [dict inc D $n] $n}}
Try it online!
add a comment |
Tcl, 48 bytes
proc C L {lmap n $L {dict g [dict inc D $n] $n}}
Try it online!
add a comment |
Tcl, 48 bytes
proc C L {lmap n $L {dict g [dict inc D $n] $n}}
Try it online!
Tcl, 48 bytes
proc C L {lmap n $L {dict g [dict inc D $n] $n}}
Try it online!
edited yesterday
answered yesterday
sergiolsergiol
2,5121925
2,5121925
add a comment |
add a comment |
Japt, 8 bytes
£¯YÄ è¶X
Try it here
£¯YÄ è¶X
:Implicit input of array U
£ :Map each X at 0-based index Y
¯ : Slice U to index
YÄ : Y+1
è : Count the elements
¶X : Equal to X
add a comment |
Japt, 8 bytes
£¯YÄ è¶X
Try it here
£¯YÄ è¶X
:Implicit input of array U
£ :Map each X at 0-based index Y
¯ : Slice U to index
YÄ : Y+1
è : Count the elements
¶X : Equal to X
add a comment |
Japt, 8 bytes
£¯YÄ è¶X
Try it here
£¯YÄ è¶X
:Implicit input of array U
£ :Map each X at 0-based index Y
¯ : Slice U to index
YÄ : Y+1
è : Count the elements
¶X : Equal to X
Japt, 8 bytes
£¯YÄ è¶X
Try it here
£¯YÄ è¶X
:Implicit input of array U
£ :Map each X at 0-based index Y
¯ : Slice U to index
YÄ : Y+1
è : Count the elements
¶X : Equal to X
edited yesterday
answered yesterday
ShaggyShaggy
19.2k21666
19.2k21666
add a comment |
add a comment |
Haskell, 44 bytes
(#)
x#(y:z)=sum[1|a<-x,a==y]:(y:x)#z
_#e=e
Try it online!
Explanation
Traverses the list from left to right keeping the list x
of visited elements, initially :
For every encounter of a y
count all equal elements in the list x
.
add a comment |
Haskell, 44 bytes
(#)
x#(y:z)=sum[1|a<-x,a==y]:(y:x)#z
_#e=e
Try it online!
Explanation
Traverses the list from left to right keeping the list x
of visited elements, initially :
For every encounter of a y
count all equal elements in the list x
.
add a comment |
Haskell, 44 bytes
(#)
x#(y:z)=sum[1|a<-x,a==y]:(y:x)#z
_#e=e
Try it online!
Explanation
Traverses the list from left to right keeping the list x
of visited elements, initially :
For every encounter of a y
count all equal elements in the list x
.
Haskell, 44 bytes
(#)
x#(y:z)=sum[1|a<-x,a==y]:(y:x)#z
_#e=e
Try it online!
Explanation
Traverses the list from left to right keeping the list x
of visited elements, initially :
For every encounter of a y
count all equal elements in the list x
.
answered yesterday
BMOBMO
11.7k22188
11.7k22188
add a comment |
add a comment |
Haxe, 58 bytes
l->{var x=[0=>0];l.map(e->++x[(x[e]==null?x[e]=0:0)+e]);};
(Requires arrow functions, so 4.0+)
var x=[0=>0]
declares a new IntMap
, with 0
as its only key (since the question say inputs are strictly positive). Unfortunately most targets throw when adding null
to a number, hence the explicit check to make sure each key is in the map before incrementing.
Also a cheeky rip off based on the JS answer:
Haxe (JS target), 41 bytes
l->{var x=[0=>0];l.map(e->x[e]=-~x[e]);};
Try both online
add a comment |
Haxe, 58 bytes
l->{var x=[0=>0];l.map(e->++x[(x[e]==null?x[e]=0:0)+e]);};
(Requires arrow functions, so 4.0+)
var x=[0=>0]
declares a new IntMap
, with 0
as its only key (since the question say inputs are strictly positive). Unfortunately most targets throw when adding null
to a number, hence the explicit check to make sure each key is in the map before incrementing.
Also a cheeky rip off based on the JS answer:
Haxe (JS target), 41 bytes
l->{var x=[0=>0];l.map(e->x[e]=-~x[e]);};
Try both online
add a comment |
Haxe, 58 bytes
l->{var x=[0=>0];l.map(e->++x[(x[e]==null?x[e]=0:0)+e]);};
(Requires arrow functions, so 4.0+)
var x=[0=>0]
declares a new IntMap
, with 0
as its only key (since the question say inputs are strictly positive). Unfortunately most targets throw when adding null
to a number, hence the explicit check to make sure each key is in the map before incrementing.
Also a cheeky rip off based on the JS answer:
Haxe (JS target), 41 bytes
l->{var x=[0=>0];l.map(e->x[e]=-~x[e]);};
Try both online
Haxe, 58 bytes
l->{var x=[0=>0];l.map(e->++x[(x[e]==null?x[e]=0:0)+e]);};
(Requires arrow functions, so 4.0+)
var x=[0=>0]
declares a new IntMap
, with 0
as its only key (since the question say inputs are strictly positive). Unfortunately most targets throw when adding null
to a number, hence the explicit check to make sure each key is in the map before incrementing.
Also a cheeky rip off based on the JS answer:
Haxe (JS target), 41 bytes
l->{var x=[0=>0];l.map(e->x[e]=-~x[e]);};
Try both online
answered yesterday
Aurel BílýAurel Bílý
1,07398
1,07398
add a comment |
add a comment |
GO 121 Bytes (Not included new lines and tabs)
func cg(a int) int{
var m = make(map[int]int)
var r = make(int, len(a))
for i,v := range a{
r[i] = m[v]
m[v]++
}
return r
}
Accepts integer array and returns integer array.
New contributor
3
Welcome to PPCG. Rare to see Go represented here. You do need to count necessary newlines, but you also have a lot of spaces that can be removed, and you can also shortencg
toc
. I recommend using TIO to generate the main part of your post: Try it online!
– Adám
19 hours ago
add a comment |
GO 121 Bytes (Not included new lines and tabs)
func cg(a int) int{
var m = make(map[int]int)
var r = make(int, len(a))
for i,v := range a{
r[i] = m[v]
m[v]++
}
return r
}
Accepts integer array and returns integer array.
New contributor
3
Welcome to PPCG. Rare to see Go represented here. You do need to count necessary newlines, but you also have a lot of spaces that can be removed, and you can also shortencg
toc
. I recommend using TIO to generate the main part of your post: Try it online!
– Adám
19 hours ago
add a comment |
GO 121 Bytes (Not included new lines and tabs)
func cg(a int) int{
var m = make(map[int]int)
var r = make(int, len(a))
for i,v := range a{
r[i] = m[v]
m[v]++
}
return r
}
Accepts integer array and returns integer array.
New contributor
GO 121 Bytes (Not included new lines and tabs)
func cg(a int) int{
var m = make(map[int]int)
var r = make(int, len(a))
for i,v := range a{
r[i] = m[v]
m[v]++
}
return r
}
Accepts integer array and returns integer array.
New contributor
New contributor
answered yesterday
rock starrock star
101
101
New contributor
New contributor
3
Welcome to PPCG. Rare to see Go represented here. You do need to count necessary newlines, but you also have a lot of spaces that can be removed, and you can also shortencg
toc
. I recommend using TIO to generate the main part of your post: Try it online!
– Adám
19 hours ago
add a comment |
3
Welcome to PPCG. Rare to see Go represented here. You do need to count necessary newlines, but you also have a lot of spaces that can be removed, and you can also shortencg
toc
. I recommend using TIO to generate the main part of your post: Try it online!
– Adám
19 hours ago
3
3
Welcome to PPCG. Rare to see Go represented here. You do need to count necessary newlines, but you also have a lot of spaces that can be removed, and you can also shorten
cg
to c
. I recommend using TIO to generate the main part of your post: Try it online!– Adám
19 hours ago
Welcome to PPCG. Rare to see Go represented here. You do need to count necessary newlines, but you also have a lot of spaces that can be removed, and you can also shorten
cg
to c
. I recommend using TIO to generate the main part of your post: Try it online!– Adám
19 hours ago
add a comment |
C++,137 bytes
//v is input , ov is output ; //example to populate v: vector v{7,8,7};
vector<int> v,ov(v.size());map<int,int>m;transform(begin(v),end(v),begin(ov),[&m](int i){!m.count(i)?m[i]=0:m[i]+=1;return m[i];});
New contributor
I'm not sure about C++, but can't you remove the newlines to save 2 bytes?
– Stephen
yesterday
1
I don't know C++ either, but what is this? It doesn't look like a function, and I don't see how you take input?
– Jo King
19 hours ago
add a comment |
C++,137 bytes
//v is input , ov is output ; //example to populate v: vector v{7,8,7};
vector<int> v,ov(v.size());map<int,int>m;transform(begin(v),end(v),begin(ov),[&m](int i){!m.count(i)?m[i]=0:m[i]+=1;return m[i];});
New contributor
I'm not sure about C++, but can't you remove the newlines to save 2 bytes?
– Stephen
yesterday
1
I don't know C++ either, but what is this? It doesn't look like a function, and I don't see how you take input?
– Jo King
19 hours ago
add a comment |
C++,137 bytes
//v is input , ov is output ; //example to populate v: vector v{7,8,7};
vector<int> v,ov(v.size());map<int,int>m;transform(begin(v),end(v),begin(ov),[&m](int i){!m.count(i)?m[i]=0:m[i]+=1;return m[i];});
New contributor
C++,137 bytes
//v is input , ov is output ; //example to populate v: vector v{7,8,7};
vector<int> v,ov(v.size());map<int,int>m;transform(begin(v),end(v),begin(ov),[&m](int i){!m.count(i)?m[i]=0:m[i]+=1;return m[i];});
New contributor
edited 10 hours ago
New contributor
answered yesterday
qqqqqqqqqq
1012
1012
New contributor
New contributor
I'm not sure about C++, but can't you remove the newlines to save 2 bytes?
– Stephen
yesterday
1
I don't know C++ either, but what is this? It doesn't look like a function, and I don't see how you take input?
– Jo King
19 hours ago
add a comment |
I'm not sure about C++, but can't you remove the newlines to save 2 bytes?
– Stephen
yesterday
1
I don't know C++ either, but what is this? It doesn't look like a function, and I don't see how you take input?
– Jo King
19 hours ago
I'm not sure about C++, but can't you remove the newlines to save 2 bytes?
– Stephen
yesterday
I'm not sure about C++, but can't you remove the newlines to save 2 bytes?
– Stephen
yesterday
1
1
I don't know C++ either, but what is this? It doesn't look like a function, and I don't see how you take input?
– Jo King
19 hours ago
I don't know C++ either, but what is this? It doesn't look like a function, and I don't see how you take input?
– Jo King
19 hours ago
add a comment |
Python 2, 44 bytes
a=
for x in input():print a.count(x);a+=x,
Try it online!
The first thing I wrote tied Chas Brown's 43, so here's a different solution that's one byte longer.
add a comment |
Python 2, 44 bytes
a=
for x in input():print a.count(x);a+=x,
Try it online!
The first thing I wrote tied Chas Brown's 43, so here's a different solution that's one byte longer.
add a comment |
Python 2, 44 bytes
a=
for x in input():print a.count(x);a+=x,
Try it online!
The first thing I wrote tied Chas Brown's 43, so here's a different solution that's one byte longer.
Python 2, 44 bytes
a=
for x in input():print a.count(x);a+=x,
Try it online!
The first thing I wrote tied Chas Brown's 43, so here's a different solution that's one byte longer.
answered 5 hours ago
LynnLynn
49.5k794227
49.5k794227
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178500%2fenumerate-each-series-of-identical-numbers-in-place%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
So basically the number of times it has appeared the sequence so far?
– Jo King
yesterday
1
@JoKing Yes, that's another way to state it, but "so far" implies zero-based, and "until and including this" implies one-based. I wanted to keep the choice.
– Adám
yesterday