Most contributing rows

17

3

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]]

Adám

Posted 2018-12-31T15:52:04.570

Reputation: 37 779

Answers

5

Jelly, 5 bytes

Ġị§§M

Try it online!

1-based indices of sorted unique elements of input.

Erik the Outgolfer

Posted 2018-12-31T15:52:04.570

Reputation: 38 134

3Woha, that is short. – Adám – 2018-12-31T16:44:44.083

4

Pyth, 9 bytes

-1 thanks to FryAmTheEggman!

{s.MssZ.g

Try it online!

Mr. Xcoder

Posted 2018-12-31T15:52:04.570

Reputation: 39 774

1The final k isn't necessary. Also .M*sZ/QZ{ seems to be a same-length solution then. – FryAmTheEggman – 2018-12-31T21:12:12.493

1@FryAmTheEggman Oh lol how did I forget about auto-complete? Thanks a lot! – Mr. Xcoder – 2018-12-31T21:42:36.760

4

R, 64 bytes

function(M)max(x<-tapply(rowSums(M),apply(M,1,toString),sum))==x

Try it online!

Returns a boolean vector with TRUE/FALSE in sort order (lexicographic).
The unique rows are shown as vector names, so it is easy to identify the most contributing ones.

digEmAll

Posted 2018-12-31T15:52:04.570

Reputation: 4 599

3

Python 3, 153 145 129 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))];return[v[1]for v in a if v[0]==max(a)[0]]

Try it online!

betseg

Posted 2018-12-31T15:52:04.570

Reputation: 8 493

2

Haskell, 60 bytes

import Data.Lists
f x=nub$argmaxes(\e->sum e*countElem e x)x

Returns a list of the rows.

nimi

Posted 2018-12-31T15:52:04.570

Reputation: 34 639

2

Charcoal, 25 bytes

IΦθ∧⁼κ⌕θι⁼×№θιΣι⌈Eθ×№θλΣλ

Try it online! Link is to verbose version of code. Default output format is each row element on its own line and rows double-spaced. Explanation:

  θ                         Input array
 Φ                          Filtered where
     κ                      Current index
    ⁼                       Equals
      ⌕                     First index of
        ι                   Current row
       θ                    In input array
   ∧                        Logical And
           №                Count of
             ι              Current row
            θ               In input array
          ×                 Multiplied by
              Σ             Sum of
               ι            Current row
         ⁼                  Equals
                ⌈           Maximum of
                  θ         Input array
                 E          Mapped over rows
                    №       Count of
                      λ     Current row
                     θ      In input array
                   ×        Multiplied by
                       Σ    Sum of
                        λ   Current row
I                           Cast to string
                            Implicitly printed

Neil

Posted 2018-12-31T15:52:04.570

Reputation: 95 035

2

Mathematica, 48 bytes

Last[SortBy[Gather[m], Total[Flatten[#]] &]][[1]]

or

TakeLargestBy[Gather[m], Total[#, 2] &, 1][[1, 1]]

where (for example)

m = {{1, 2, 3}, {2, 0, 4}, {7, 9, 5}, {6, 3, 0}, {2, 0, 4}, 
     {6, 3, 0}, {2, 0, 4}, {7, 9, 5}};

David G. Stork

Posted 2018-12-31T15:52:04.570

Reputation: 213

2You can use shorthand and remove whitespace to save bytes: SortBy[Gather@m,Total@*Flatten][[-1,1]] – Doorknob – 2019-01-01T01:56:45.193

1

This looks like it takes input from a predefined variable, which is not allowed. Submissions have to be full programs or function by default.

– Dennis – 2019-01-02T00:52:30.957

TakeLargestBy[Gather[m], Total[#, 2] &, 1][[1, 1]] /@ m – David G. Stork – 2019-01-02T00:54:20.553

This is not valid; it only returns one of the rows with greatest values rather than all of them. – lirtosiast – 2019-01-02T01:50:50.163

1

JavaScript (ES6), 88 bytes

Outputs an array of Boolean values in appearance order.

m=>m.map(h=o=r=>h=(v=o[r]=~~o[r]+eval(r.join`+`))<h?h:v)&&Object.keys(o).map(x=>o[x]==h)

Try it online!

Arnauld

Posted 2018-12-31T15:52:04.570

Reputation: 111 334

1

Groovy, 76 bytes

{l->m=l.toSet().sort().collect{it.sum()*l.count(it)}
m.collect{it==m.max()}}

Try it online!

Returns as booleans in sort order

ASCII-only

Posted 2018-12-31T15:52:04.570

Reputation: 4 687

1

Scala, 63 bytes

l=>{var m=l.distinct.map(n=>n.sum*l.count(n==))
m.map(m.max==)}

Try it online!

Returns booleans in appearance order

ASCII-only

Posted 2018-12-31T15:52:04.570

Reputation: 4 687

1

APL (Dyalog Unicode), 12 bytes

(⌈/=⊢)+.×∘⍴⌸

Try it online!

-2 thanks to Adám. -1 thanks to alternate output format.

Erik the Outgolfer

Posted 2018-12-31T15:52:04.570

Reputation: 38 134

Now comes the challenge of explanation… – Adám – 2019-01-02T09:30:01.933

1

Wolfram Language (Mathematica), 42 bytes

#~Position~Max@#&@(Total[#,2]&/@Gather@#)&

Try it online!

lirtosiast

Posted 2018-12-31T15:52:04.570

Reputation: 20 331

1

Python 2, 81 78 bytes

lambda a:{u for u in a if a.count(u)*sum(u)==max(a.count(t)*sum(t)for t in a)}

Try it online!

3 bytes thx to Black Owl Kai.

Given a collection of tuples, the output is a set of those tuples having the desired maximal property.

Chas Brown

Posted 2018-12-31T15:52:04.570

Reputation: 8 959

78 bytes – Black Owl Kai – 2019-01-02T11:49:38.580

@Black Owl Kai: thx! I missed that... – Chas Brown – 2019-01-02T20:29:29.970

1

Japt, 13 11 bytes

-2 bytes from @Shaggy

ü¬®xx
m¥Urw

Try it online!

Luis felipe De jesus Munoz

Posted 2018-12-31T15:52:04.570

Reputation: 9 639

111 bytes – Shaggy – 2019-01-03T10:00:55.427

@Shaggy I came up with the same thing :P

– Oliver – 2019-01-03T15:29:25.703

0

C# (Visual C# Interactive Compiler), 126 bytes

n=>{var i=n.OrderBy(a=>a.Sum()*n.Count(a.SequenceEqual));return i.Where((a,b)=>i.Take(b).Count(a.SequenceEqual)<1).Reverse();}

Try it online!

Most of this code is spent taking out all duplicate values, since the default comparer for lists do not compare the values inside the lists. That means that I cannot use Distinct(), GroupBy(), or Contains to filter the list.

Embodiment of Ignorance

Posted 2018-12-31T15:52:04.570

Reputation: 7 014

0

K (ngn/k), 17 bytes

{&a=|/a:+//'x@=x}

Try it online!

{ } function with argument x

=x group - form a dictionary in which the keys are rows and the values are lists of their indices in the matrix

x@ index the original matrix with that. the result is again a dictionary with the rows as keys. the values are multiple copies of the corresponding key

+//' sum until convergence each (acts only on the values; keys remain as they are)

a: assign to a

|/ maximum (of the values)

a=|/a a row-to-boolean dictionary of which rows contribute the most

& "where", i.e. which keys correspond to values of 1

ngn

Posted 2018-12-31T15:52:04.570

Reputation: 11 449

0

Japt, 11 10 bytes

ü¬ü_xxÃÌmâ

Run it online

Oliver

Posted 2018-12-31T15:52:04.570

Reputation: 7 160

0

05AB1E, 10 9 bytes

ês{γOOZQÏ

Try it online or verify all test cases.

Explanation:

ê          # Sort and uniquify the (implicit) input list of lists
           #  i.e. [[2,0,4],[1,2,3],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
           #   → [[1,2,3],[2,0,4],[6,3,0]]
 s         # Swap so the (implicit) input list of lists is at the top again
  {        # Sort it
           #  i.e. [[2,0,4],[1,2,3],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
           #   → [[1,2,3],[2,0,4],[2,0,4],[2,0,4],[6,3,0],[6,3,0]]
   γ       # Group the sorted list of lists
           #  i.e. [[1,2,3],[2,0,4],[2,0,4],[2,0,4],[6,3,0],[6,3,0]]
           #   → [[[1,2,3]],[[2,0,4],[2,0,4],[2,0,4]],[[6,3,0],[6,3,0]]]
    O      # Take the sum of each inner-most lists
           #  i.e. [[[1,2,3]],[[2,0,4],[2,0,4],[2,0,4]],[[6,3,0],[6,3,0]]]
           #   → [[6],[6,6,6],[9,9]]
     O     # Take the sum of each inner list
           #  i.e. [[6],[6,6,6],[9,9]] → [6,18,18]
      Z    # Get the max (without popping the list of sums)
           #  i.e. [6,18,18] → 18
       Q   # Check for each if this max is equal to the sum
           #  i.e. [6,18,18] and 18 → [0,1,1]
        Ï  # Filter the uniquified list of lists on truthy values (and output implicitly)
           #  i.e. [[1,2,3],[2,0,4],[6,3,0]] and [0,1,1] → [[2,0,4],[6,3,0]]

Kevin Cruijssen

Posted 2018-12-31T15:52:04.570

Reputation: 67 575

0

Gaia, 10 bytes

ȯẋ_¦Σ¦:⌉=¦

Try it online!

Since Gaia doesn't accept lists through inputs very easily, this is a function that accepts a list from the top from the top of the stack and leaves the result on top (as masks of sorted order).

ȯ           Sort the list
 ẋ          Split it into runs of the same element (in this case, runs of the same row)
  _¦        Flatten each sublist
    Σ¦      Sum each sublist
      :⌉    Find the maximum sum
        =¦  Compare each sum for equality with the maximum

Business Cat

Posted 2018-12-31T15:52:04.570

Reputation: 8 927

0

J, 16 bytes

[:(=>./)+/^:2/.~

Try it online!

A monadic verb that gives the boolean result in appearance order.

How it works

[:(=>./)+/^:2/.~
             /.~  Self-classify; collect identical rows in appearance order
                  For each collected rows (= a matrix),
        +/^:2       Sum all the elements into one value
[:(=>./)          Compute the boolean vector:
    >./             Is the max of the array
   =                equal to this element?

Bubbler

Posted 2018-12-31T15:52:04.570

Reputation: 16 616