Most contributing rows
Given a non-empty matrix of non-negative integers, answer which unique rows contribute most to the sum total of elements in the matrix.
Answer by any reasonable indication, for example a mask of the unique rows order of appearance (or sort order), or indices (zero- or one- based) of those, or a submatrix consisting of the rows (in any order) or some kind of dictionary construct… — but do explain it!
Examples
[[1,2,3],[2,0,4],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
:
The unique rows are [1,2,3]
, [2,0,4]
, and [6,3,0]
each respectively contributing 6, 6, and 9 each time they occur. However, they occur once, thrice and twice respectively, so all of their respective occurrences contribute 6, 18, and 18 to the total (42), so the latter two rows are the ones that contribute most. Valid answers are therefore:
[false,true,true]
mask in appearance/sort order or[1,2]
/[2,3]
zero/one-based indices of the above or[[2,0,4],[6,3,0]]
the actual rows
⋮
[[1,2],[3,1],[2,3],[1,2],[3,1],[2,3],[1,2]]
[false,false,true]
(appearance order) / [false,true,false]
(sort order)[2]
/[3]
(appearance order) / [1]
/[2]
(sort order)[[2,3]]
⋮
code-golf matrix subsequence
add a comment |
Given a non-empty matrix of non-negative integers, answer which unique rows contribute most to the sum total of elements in the matrix.
Answer by any reasonable indication, for example a mask of the unique rows order of appearance (or sort order), or indices (zero- or one- based) of those, or a submatrix consisting of the rows (in any order) or some kind of dictionary construct… — but do explain it!
Examples
[[1,2,3],[2,0,4],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
:
The unique rows are [1,2,3]
, [2,0,4]
, and [6,3,0]
each respectively contributing 6, 6, and 9 each time they occur. However, they occur once, thrice and twice respectively, so all of their respective occurrences contribute 6, 18, and 18 to the total (42), so the latter two rows are the ones that contribute most. Valid answers are therefore:
[false,true,true]
mask in appearance/sort order or[1,2]
/[2,3]
zero/one-based indices of the above or[[2,0,4],[6,3,0]]
the actual rows
⋮
[[1,2],[3,1],[2,3],[1,2],[3,1],[2,3],[1,2]]
[false,false,true]
(appearance order) / [false,true,false]
(sort order)[2]
/[3]
(appearance order) / [1]
/[2]
(sort order)[[2,3]]
⋮
code-golf matrix subsequence
add a comment |
Given a non-empty matrix of non-negative integers, answer which unique rows contribute most to the sum total of elements in the matrix.
Answer by any reasonable indication, for example a mask of the unique rows order of appearance (or sort order), or indices (zero- or one- based) of those, or a submatrix consisting of the rows (in any order) or some kind of dictionary construct… — but do explain it!
Examples
[[1,2,3],[2,0,4],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
:
The unique rows are [1,2,3]
, [2,0,4]
, and [6,3,0]
each respectively contributing 6, 6, and 9 each time they occur. However, they occur once, thrice and twice respectively, so all of their respective occurrences contribute 6, 18, and 18 to the total (42), so the latter two rows are the ones that contribute most. Valid answers are therefore:
[false,true,true]
mask in appearance/sort order or[1,2]
/[2,3]
zero/one-based indices of the above or[[2,0,4],[6,3,0]]
the actual rows
⋮
[[1,2],[3,1],[2,3],[1,2],[3,1],[2,3],[1,2]]
[false,false,true]
(appearance order) / [false,true,false]
(sort order)[2]
/[3]
(appearance order) / [1]
/[2]
(sort order)[[2,3]]
⋮
code-golf matrix subsequence
Given a non-empty matrix of non-negative integers, answer which unique rows contribute most to the sum total of elements in the matrix.
Answer by any reasonable indication, for example a mask of the unique rows order of appearance (or sort order), or indices (zero- or one- based) of those, or a submatrix consisting of the rows (in any order) or some kind of dictionary construct… — but do explain it!
Examples
[[1,2,3],[2,0,4],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
:
The unique rows are [1,2,3]
, [2,0,4]
, and [6,3,0]
each respectively contributing 6, 6, and 9 each time they occur. However, they occur once, thrice and twice respectively, so all of their respective occurrences contribute 6, 18, and 18 to the total (42), so the latter two rows are the ones that contribute most. Valid answers are therefore:
[false,true,true]
mask in appearance/sort order or[1,2]
/[2,3]
zero/one-based indices of the above or[[2,0,4],[6,3,0]]
the actual rows
⋮
[[1,2],[3,1],[2,3],[1,2],[3,1],[2,3],[1,2]]
[false,false,true]
(appearance order) / [false,true,false]
(sort order)[2]
/[3]
(appearance order) / [1]
/[2]
(sort order)[[2,3]]
⋮
code-golf matrix subsequence
code-golf matrix subsequence
edited 2 hours ago
asked 2 hours ago
Adám
28.7k269188
28.7k269188
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Jelly, 5 bytes
Ġị§§M
Try it online!
1-based indices of sorted unique elements of input.
Woha, that is short.
– Adám
2 hours ago
add a comment |
Pyth, 10 bytes
{s.MssZ.gk
Try it online!
add a comment |
Python 3, 153 145 bytes
-8 bytes thanks to @Mr. Xcoder!
from itertools import*
def f(l):a=[[sum(map(sum,[*s])),k]for k,s in groupby(sorted(l))];m=max(a)[0];return[v[1]for i,v in enumerate(a)if v[0]==m]
Try it online!
add a comment |
Haskell, 60 bytes
import Data.Lists
f x=nub$argmaxes(e->sum e*countElem e x)x
Returns a list of the rows.
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%2f178184%2fmost-contributing-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Jelly, 5 bytes
Ġị§§M
Try it online!
1-based indices of sorted unique elements of input.
Woha, that is short.
– Adám
2 hours ago
add a comment |
Jelly, 5 bytes
Ġị§§M
Try it online!
1-based indices of sorted unique elements of input.
Woha, that is short.
– Adám
2 hours ago
add a comment |
Jelly, 5 bytes
Ġị§§M
Try it online!
1-based indices of sorted unique elements of input.
Jelly, 5 bytes
Ġị§§M
Try it online!
1-based indices of sorted unique elements of input.
edited 2 hours ago
answered 2 hours ago
Erik the Outgolfer
31.4k429103
31.4k429103
Woha, that is short.
– Adám
2 hours ago
add a comment |
Woha, that is short.
– Adám
2 hours ago
Woha, that is short.
– Adám
2 hours ago
Woha, that is short.
– Adám
2 hours ago
add a comment |
Pyth, 10 bytes
{s.MssZ.gk
Try it online!
add a comment |
Pyth, 10 bytes
{s.MssZ.gk
Try it online!
add a comment |
Pyth, 10 bytes
{s.MssZ.gk
Try it online!
Pyth, 10 bytes
{s.MssZ.gk
Try it online!
answered 2 hours ago
Mr. Xcoder
31.6k759198
31.6k759198
add a comment |
add a comment |
Python 3, 153 145 bytes
-8 bytes thanks to @Mr. Xcoder!
from itertools import*
def f(l):a=[[sum(map(sum,[*s])),k]for k,s in groupby(sorted(l))];m=max(a)[0];return[v[1]for i,v in enumerate(a)if v[0]==m]
Try it online!
add a comment |
Python 3, 153 145 bytes
-8 bytes thanks to @Mr. Xcoder!
from itertools import*
def f(l):a=[[sum(map(sum,[*s])),k]for k,s in groupby(sorted(l))];m=max(a)[0];return[v[1]for i,v in enumerate(a)if v[0]==m]
Try it online!
add a comment |
Python 3, 153 145 bytes
-8 bytes thanks to @Mr. Xcoder!
from itertools import*
def f(l):a=[[sum(map(sum,[*s])),k]for k,s in groupby(sorted(l))];m=max(a)[0];return[v[1]for i,v in enumerate(a)if v[0]==m]
Try it online!
Python 3, 153 145 bytes
-8 bytes thanks to @Mr. Xcoder!
from itertools import*
def f(l):a=[[sum(map(sum,[*s])),k]for k,s in groupby(sorted(l))];m=max(a)[0];return[v[1]for i,v in enumerate(a)if v[0]==m]
Try it online!
edited 2 hours ago
answered 2 hours ago
betseg
7,82821755
7,82821755
add a comment |
add a comment |
Haskell, 60 bytes
import Data.Lists
f x=nub$argmaxes(e->sum e*countElem e x)x
Returns a list of the rows.
add a comment |
Haskell, 60 bytes
import Data.Lists
f x=nub$argmaxes(e->sum e*countElem e x)x
Returns a list of the rows.
add a comment |
Haskell, 60 bytes
import Data.Lists
f x=nub$argmaxes(e->sum e*countElem e x)x
Returns a list of the rows.
Haskell, 60 bytes
import Data.Lists
f x=nub$argmaxes(e->sum e*countElem e x)x
Returns a list of the rows.
answered 23 mins ago
nimi
31.3k32085
31.3k32085
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%2f178184%2fmost-contributing-rows%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