42
\$\begingroup\$

Given a nonempty list of positive decimal integers, output the largest number from the set of numbers with the fewest digits.

The input list will not be in any particular order and may contain repeated values.

Examples:

[1] -> 1
[9] -> 9
[1729] -> 1729
[1, 1] -> 1
[34, 3] -> 3
[38, 39] -> 39
[409, 12, 13] -> 13
[11, 11, 11, 1] -> 1
[11, 11, 11, 11] -> 11
[78, 99, 620, 1] -> 1
[78, 99, 620, 10] -> 99
[78, 99, 620, 100] -> 99
[1, 5, 9, 12, 63, 102] -> 9
[3451, 29820, 2983, 1223, 1337] -> 3451
[738, 2383, 281, 938, 212, 1010] -> 938

The shortest code in bytes wins.

\$\endgroup\$
2
  • \$\begingroup\$ Can the input numbers be on separate lines? \$\endgroup\$
    – seshoumara
    Sep 16, 2016 at 5:48
  • \$\begingroup\$ @seshoumara That sounds reasonable, yes. \$\endgroup\$ Sep 16, 2016 at 5:48

54 Answers 54

1
2
1
\$\begingroup\$

Factor, 25 bytes

[ all-shortest supremum ]

Try it online!

Get the largest of the shortests.

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

Japt -h, 4 bytes

Input as an array of integer strings, output as an integer string.

üÊÎñ

Try it

üÊÎñ     :Implicit input of array
ü        :Group and sort by
 Ê       :  Length
  Î      :First element
   ñ     :Sort
         :Implicit output of last element
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 5 bytes

⁽₍LN∵

Try it Online!

How?

⁽₍LN∵
⁽     # Next element as a lambda:
 ₍    #  Apply both of the next two elements and wrap the results in a list:
  L   #   Length...
   N  #   ...and negate
    ∵ # Get the minimum of the input by that function
\$\endgroup\$
1
\$\begingroup\$

Pip, 11 bytes

@Y#_AE-_SKa

Try It Online!

\$\endgroup\$
1
  • \$\begingroup\$ Great idea, but unfortunately it doesn't work. If you put the length and the negated value in a List instead of a Range, it works; and you can get back to 11 bytes by using Y. \$\endgroup\$
    – DLosc
    Jun 15 at 18:35
1
\$\begingroup\$

C (clang), 77 bytes

m,n,i,j;f(*a,l){for(j=99;l--;m=i<j|i>j^n>m?j=i,n:m)i=log10(n=a[l]);return m;}

Try it online!

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

Python 2, 58 bytes

def F(x):l={len(`i`):i for i in sorted(x)};print l[min(l)]
\$\endgroup\$
0
\$\begingroup\$

Python 3, 56 bytes

lambda a:sorted(sorted(a),key=lambda x:-len(str(x)))[-1]

Uses a lambda in a lambda!

Python 2, 53 bytes

s=lambda a:sorted(sorted(a),key=lambda x:-len(`x`))[-1]

Same but with backticks

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

Pip, 11 bytes

(SNgSK-#_v)

Takes input as command-line args. Try it online!

First time using the Sort-Keyed operator! Like Python's sorted(), it takes a function that is applied to each item of the iterable and the result used as a sort key. Here's how this program works:

 SNg         List of cmdline args, sorted numerically in increasing order
    SK       Sort with key function...
      -#_    ... negative length(x), thus putting the shortest numbers at the end but not
               affecting the relative ordering among numbers with the same length
(        v)  Get the last element (index -1) and auto-print
\$\endgroup\$
0
\$\begingroup\$

Clojure, 63 bytes

(reduce #(if(=(quot %1 10)(quot %2 10))(max %1 %2) %1)(sort x)) 

as in:

(reduce #(if(=(quot %1 10)(quot %2 10))(max %1 %2) %1)(sort[3 7 121 11 8 2 10 9]))
=> 9

Though I'm sure there's a way to make it smaller.

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

PHP , 86 Bytes

<?$l=strlen($r=min($a=$_GET[a]));foreach($a as$v)if($v>$r&strlen($v)==$l)$r=$v;echo$r;
\$\endgroup\$
0
\$\begingroup\$

Pyke, 7 bytes

.#_il)h

Try it here!

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

C#, 119 bytes

int L(int[]n){var g=n.GroupBy(o=>(o+"").Length);return g.Where(o=>o.Key==g.Min(k=>k.Key)).OrderBy(o=>o).First().Max();}

Ungolfed:

public int L(int[]n)
{
  var g = n.GroupBy(o => (o+"").Length);
  return g.Where(o => o.Key == g.Min(k => k.Key)).OrderBy(o => o).First().Max();
}

Magical Linq...

Usage:

var a = new LargestNumberFewestDigits();
Console.Write(a.L(new int[] { 738, 2383, 281, 938, 212, 1010 }));

Output:

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

Java 8, 110 bytes

int f(int[]a){java.util.Arrays.sort(a);int i=a[0];for(int z:a)i=(z+"").length()>(i+"").length()?i:z;return i;}

Ungolfed:

int f(int[]a){
    java.util.Arrays.sort(a);                  //sorts the array in ascending order
    int i=a[0];                                //declare output variable, uses 1 less byte than using a[0]
    for(int z:a) {
        i=(z+"").length()>(i+"").length()?i:z; //for each int, if the length isn't longer set as output
    }
    return i;
}

Any elements that aren't longer in a sorted array must be a higher value.

Also lambda version (101 bytes):

a->{java.util.Arrays.sort(a);int i=a[0];for(int z:a)i=(z+"").length()>(i+"").length()?i:z;return i;};
\$\endgroup\$
0
\$\begingroup\$

C# (85 bytes)

It's a full function:

int y(int[] t) {int m=t.Min(i=>(i+"").Length);return t.Max(n=>(n+"").Length==m?n:0);}

Unriddled:

int y(int[] t) 
{
    int m = t.Min(i => (i+"").Length);
    return t.Max(n => (n+"").Length == m ? n : 0);
}
\$\endgroup\$
1
  • \$\begingroup\$ There's an useless whitespace after you function signature and in the parameter list. Lambda statement should also work. \$\endgroup\$
    – Yytsi
    Sep 17, 2016 at 17:21
0
\$\begingroup\$

Python 2, 63 30 63 bytes

lambda a:a.sort()or[`i`for i in a if len(`i`)==len(`a[0]`)][-1]
\$\endgroup\$
3
  • \$\begingroup\$ Bugged for [1, 99, 620, 10000, 3]. \$\endgroup\$
    – orlp
    Sep 16, 2016 at 1:54
  • \$\begingroup\$ I agree with orlp, doesn't work \$\endgroup\$ Sep 16, 2016 at 2:07
  • \$\begingroup\$ Once again, a wrong anser with an upvote? This should be amended or deleted \$\endgroup\$
    – edc65
    Sep 16, 2016 at 13:27
0
\$\begingroup\$

C# 85 bytes

int S(int[] i){var m=i.Min();return i.Where(v=>v<m*10&&$"{v}{m}".Length%2==0).Max();}

Usage:

Console.WriteLine(S(new[] { 738, 2383, 281, 938, 212, 1010 }));

Output: 938

Explanation

Get the minimum value from the array and assume that the minimum value is the value with the shortest length.

Filter the array to remove all values greater than 10 * the minimum. i.e if the value is 8, the first check removes all values greater than 80.

Also filter to remove all values where the length of the combined string is odd. "8" + "9" is length 2 which is even, "8" + "10" is length 3 which is odd and eliminated.

int S(int[] i)
{
    var m = i.Min();
    return i.Where(v => v < m * 10 && $"{v}{m}".Length % 2 == 0).Max();
}
\$\endgroup\$
1
  • \$\begingroup\$ int[]i will work. \$\endgroup\$
    – Yytsi
    Sep 17, 2016 at 17:22
0
\$\begingroup\$

C# 72 bytes

int S(int[]i){return i.OrderBy(v=>(""+v).Length).ThenBy(v=>-v).First();}

Borrowing from some of the other answers, a very efficient way to accomplish this task is to sort by number of digits, then by value descending. This moves the answer to the first position. Linq accomplishes this fairly simply with the OrderBy and ThenBy statements.

Console.WriteLine(S(new[] { 738, 2383, 281, 938, 212, 1010 }));

Output: 938

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

Racket 148 bytes

(λ(l)(define l2(map number->string l))(apply max(map string->number
(filter(lambda(x)(eq?(string-length x)(apply min(map string-length l2))))l2))))

Testing:

(f (list 1 5 9 12 63 102))

Output:

9

Detailed version:

(define (f sl)
  (define sl2 (map number->string sl))
  (apply max 
         (map string->number
              (filter (lambda(x)
                        (equal? (string-length x)
                                (apply min (map string-length sl2))
                                )) sl2))))
\$\endgroup\$
0
\$\begingroup\$

SQLite, 49 bytes

SELECT n FROM a ORDER BY length(n),n DESC LIMIT 1

Of course only if the list is allowed to be in a table. The type of the field should be INTEGER.

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

C++11, 109 bytes

[](auto a){sort(a.begin(),a.end(),[](auto x,auto y){return (int)log10(x)<=(int)log10(y)&&x>y;});return a[0];}

a should be a container like vector or list. E.g. (lambda as f)

cout<<f(vector<int>{ 10, 13, 19, 100, 12200 });
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 136 bytes

Golfed:

(defun o(&rest r &aux(s(sort r '<)))(flet((f(x)(floor(log x 10))))(apply 'max(remove-if(lambda(x)(<(apply 'min(mapcar 'f s))(f x)))s))))

Ungolfed:

(defun o (&rest r 
      &aux (s (sort r '<)))
  (flet ((f (x) (floor (log x 10))))
    (apply 'max 
       (remove-if (lambda (x)
            (< (apply 'min (mapcar 'f s))
               (f x)))
              s))))
\$\endgroup\$
0
\$\begingroup\$

Object Pascal, 169 167 bytes

A function which takes variable array as input:

function m(a:array of integer):integer;var i:integer;begin m:=a[0];for i in a do if(int(log10(i))<int(log10(m)))or((int(log10(i))=int(log10(m)))and(i>m))then m:=i;end;

(Yes I know, pascal sucks at this, in addition there is no sort function in it's library..)

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

Coconut, 56 bytes

This is just a Coconut-ification of the best (currently only) Python 3 answer (as well as some of the other answers), so most of the credit should go to them. Any valid Python 3 is Coconut which means it's at most the same length. Also, Coconut compiles to Python.

lambda a:sorted(sorted(a),key=lambda x:-len(str(x)))[-1] can be changed into ->sorted(sorted(_),key=->-len(str(_)))[-1]. This compiles to lambda _=None: sorted(sorted(_), key=lambda _=None: -len(str(_)))[-1] (along with a ton of other stuff like additional Coconut builtins).

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

JavaScript (Node.js), 53 bytes

a=>a.reduce((N,n)=>~(L=Math.log10)(N)!=~L(n)^N>n?N:n)

Attempt This Online!

Explanation

For any two values N,n, we need to decide which to return, this table describes that:

         length
        | < = >
     ---+-------
value < | N n .
      = | . ? .
      > | . N n

Where . will never happen and ? means either N or n works.

This looks a lot like an exclusive or (XOR) table:

      A
    | 0 1
 ---|-----
B 0 | 0 1
  1 | 1 0

Where we XOR length(N) != length(n) with N > n. The cheapest way (I think) to get the length of a number in JS is using Math.log10, then flooring the result with a bitwise operator (in this case ~).

I also [ab]use the fact that ^ has lower operator precedence than comparisons.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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