14
\$\begingroup\$

Given two different lists of non-negative integers, return the list that has the highest maximum (e.g. [4, 2], [3, 3, 1] -> [4, 2]).

If they both have the same maximum, return the list that contains more instances of this maximum (e.g. [4, 2, 4], [4, 3, 3, 3, 1, 3] -> [4, 2, 4]).

If, after these comparisons, they are equal, do the same comparison but with their next highest item (e.g. [2, 3, 4, 4], [4, 4, 3, 3, 1] -> [4, 4, 3, 3, 1]).

If, after all these comparisons, they are still considered to be equal, output the longer list (e.g. [4, 3, 2, 1, 0], [1, 2, 3, 4] -> [4, 3, 2, 1, 0]).

Make your code as short as possible.

Test Cases

[4, 4, 4, 4, 2, 4], [4, 4, 4, 4, 3, 2] -> [4, 4, 4, 4, 2, 4]
[0], [] -> [0]
[0, 0], [0] -> [0, 0]
[1], [0, 0] -> [1]
[4, 4, 4, 4, 4, 2], [4, 4, 4, 4, 4] -> [4, 4, 4, 4, 4, 2]
[1, 0], [0, 0, 0] -> [1, 0]
\$\endgroup\$
0

14 Answers 14

5
\$\begingroup\$

Pyth, 4 bytes

eo_S

Try it online

Explanation

eo_S
 o  NQ    Order the inputs...
  _S      ... by their reversed sorted values...
e         ... and take the last.
\$\endgroup\$
5
\$\begingroup\$

Python 2, 44 bytes

lambda*x:max(x,key=lambda y:sorted(y)[::-1])

Try it online!

\$\endgroup\$
2
4
\$\begingroup\$

Jelly, 4 bytes

NÞÞṪ

Try it online!

How it works

NÞÞṪ  Main link. Argument: [u, v] (pair of vectors)

  Þ   Sort [u, v], using the link to the left as key.
NÞ      Sort u (or v) by the negatives of its values.
        This sorts the vector in descending order.
   Ṫ  Tail; select the last, lexicographically larger vector.
\$\endgroup\$
2
\$\begingroup\$

Haskell, 37 35 bytes

import Data.Lists
argmax$sortOn(0-)

The input is taken as a two element list, e.g. ( argmax$sortOn(0-) ) [[4,4,4,4,2,4], [4,4,4,4,3,2]].

Find the element in the input list which is the maximum after sorting by negating the values (i.e. descending order).

\$\endgroup\$
2
\$\begingroup\$

Clean, 42 bytes

import StdEnv
s=sortBy(>)
?a b|s a>s b=a=b

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Huh, that's an interesting syntax. I take it it means "return a if the pattern guard is true and b otherwise"? \$\endgroup\$
    – Laikoni
    Jan 22, 2018 at 21:02
  • \$\begingroup\$ @Laikoni Yep. Pattern guards in Clean are syntactically similar to a C-style if ... else if ..., where you can nest/chain them. And just like you can omit else if(true), you can skip the last guard condition. (however, only once per line) \$\endgroup\$
    – Οurous
    Jan 22, 2018 at 21:09
1
\$\begingroup\$

JavaScript (ES7), 52 bytes

(a,b,m=d=>d.map(n=>N+=1e4**n,N=0)&&N)=>m(a)>m(b)?a:b

This method works without sorting the arrays. Instead, it calculates the sum of 10,000 raised to each array's elements. The largest sum represents the array with the highest score. (This solution assumes that neither array has more than 10,000 elements.)

Test cases

let f=

(a,b,m=d=>d.map(n=>N+=1e4**n,N=0)&&N)=>m(a)>m(b)?a:b


console.log(JSON.stringify(f([4, 4, 4, 4, 2, 4], [4, 4, 4, 4, 3, 2]))); // [4, 4, 4, 4, 2, 4]
console.log(JSON.stringify(f([0], []))); // [0]
console.log(JSON.stringify(f([0, 0], [0]))); // [0, 0]
console.log(JSON.stringify(f([1], [0, 0]))); // [1]
console.log(JSON.stringify(f([4, 4, 4, 4, 4, 2], [4, 4, 4, 4, 4]))); // [4, 4, 4, 4, 4, 2]
console.log(JSON.stringify(f([1, 0], [0, 0, 0]))); // [1, 0]
console.log(JSON.stringify(f([3], [11]))); // [11]
console.log(JSON.stringify(f([4, 3, 3, 3, 1, 3], [4, 2, 4]))); // [4, 2, 4]
console.log(JSON.stringify(f([3,3],[3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]))); //[3, 3]

\$\endgroup\$
1
\$\begingroup\$

Ruby, 33 bytes

->*a{a.max_by{|x|x.sort.reverse}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 2, 79 bytes

lambda*a:max(a,key=lambda l:sum(zip(map(l.count,s(l)),s(l)),())[::-1])
s=sorted

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ruby 149 bytes

->n,m{(n+m).sort.reverse.map{|i|r=((a=n.index(i))&&m.index(i)?(b=n.count(i))==(c=m.count(i))?nil:b>c ?n:m:a ?n:m);return r if r};n.size>m.size ? n:m}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Husk, 3 bytes

►Ö_

Try it online!

This submission basically takes a two-element list of lists as input and retrieves the maximum (), sorted by their values sorted in descending order (Ö_).

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 5 bytes

Σ{R}θ

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 98 bytes

(a,b,g=a=>[...a].sort((a,b)=>b-a),h=([a,...b],[c,...d])=>a==c?h(b,d):a<c|!(1/a))=>h(g(a),g(b))?b:a

g sorts a copy of its parameter in reverse order (since sort mutates the array), while h recursively performs the elementwise comparison of the arrays.

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 17 bytes

*.max(*.sort(-*))

Test it (Lambda Lambda Lambda)

  • -* lambda that numerically negates the input
  • *.sort(-*) lambda that uses that uses the results of applying that to compare elements
  • *.max(*.sort(-*)) lambda that finds the max of those results, and uses that to determine which input to return.
\$\endgroup\$
0
\$\begingroup\$

J, 20 bytes

[:{.]\:[:#.[:>\:~&.>

Ungolfed:

[: {. ] \: [: #. [: > \:~&.>

Essentially the Pyth answer, translated unpithily into J.

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.