Output the largest number with the fewest digits

37

1

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.

Calvin's Hobbies

Posted 2016-09-16T00:27:45.880

Reputation: 84 000

Can the input numbers be on separate lines? – seshoumara – 2016-09-16T05:48:10.157

@seshoumara That sounds reasonable, yes. – Calvin's Hobbies – 2016-09-16T05:48:31.793

Answers

13

Pyth, 7 3 6 bytes

eS.ml`

Test Suite

Explanation:

e      Still grab the last element
 S      Still sort
  .ml`   But prefilter the list for those with the (m)inimum length.

7 byte solution:

eSh.gl`

Test Suite

Explanation:

   .g   Group items in (implicit) input by:
     l  The length of
      ` their representation
  h     Get those with the shortest length
 S      Sort the resulting list
e       and grab the last (i.e. largest) element

Steven H.

Posted 2016-09-16T00:27:45.880

Reputation: 2 841

6

Python 2, 48 42 bytes

-6 bytes thanks to @Dennis (use min rather than sorted)

lambda l:min(l,key=lambda x:(len(`x`),-x))

All test cases are at ideone

Take the minimum of the list by (length, -value)

Jonathan Allan

Posted 2016-09-16T00:27:45.880

Reputation: 67 804

1min should work instead of sorted. – Dennis – 2016-09-16T03:48:57.943

@Dennis, oh jeez - thanks! Probably different enough to have posted that yourself though. – Jonathan Allan – 2016-09-16T04:14:16.237

Swapping sorted()[0] for min? I consider that a trivial modification of your original code. – Dennis – 2016-09-16T04:35:53.903

There's also len(`x`)+1./x for the same length. Too bad you need the 1.. – xnor – 2016-09-16T06:37:41.663

Well, that's shorter than what I came up with. Good job! – mbomb007 – 2016-09-16T19:39:27.330

6

Jelly, 7 bytes

DL,NµÞḢ

Test it at TryItOnline
Or see all test cases also at TryItOnline

How?

DL,NµÞḢ - Main link takes one argument, the list, e.g. [738, 2383, 281, 938, 212, 1010]
D       - convert to decimal, e.g. [[7,3,8],[2,3,8,3],[2,8,1],[9,3,8],[2,1,2],[1,0,1,0]]
 L      - length, e.g. [3,4,3,3,3,4]
   N    - negate, e.g [-738, -2383, -281, -938, -212, -1010]
  ,     - pair, e.g. [[3,-738],[4,-2383],[3,-281],[3,-938],[3,-212],[4,-1010]]
    µ   - make a monadic chain
     Þ  - sort the input by that monadic function, e.g [938,738,281,212,2383,1010]
          (the lists in the example are not created, but we sort over the values shown)
      Ḣ - pop and return the first element, e.g. 938

Jonathan Allan

Posted 2016-09-16T00:27:45.880

Reputation: 67 804

1Great use of sort! – miles – 2016-09-16T03:21:59.620

@miles your way was still inspired :) – Jonathan Allan – 2016-09-16T03:28:54.987

5

05AB1E, 5 bytes

Code:

({é¬(

Explanation:

(      # Negate the list, e.g. [22, 33, 4] -> [-22, -33, -4]
 {     # Sort, e.g. [-22, -33, -4] -> [-33, -22, -4]
  é    # Sort by length, e.g. [-33, -22, -4] -> [-4, -22, -33]
   ¬   # Get the first element.
    (  # And negate that.

Uses the CP-1252 encoding. Try it online!

Adnan

Posted 2016-09-16T00:27:45.880

Reputation: 41 965

4

Ruby, 34 bytes

->a{a.max_by{|n|[-n.to_s.size,n]}}

See it on eval.in: https://eval.in/643153

Jordan

Posted 2016-09-16T00:27:45.880

Reputation: 5 001

4

MATL, 14 bytes

10&YlktX<=G*X>

Try it online!

Explanation:

  &Yl           % Log
10              % Base 10
     kt         % Floor and duplicate
       X<       % Find the smallest element
         =      % Filter out elements that do not equal the smallest element
          G     % Push the input again
           *    % Multiply (this sets numbers that do not have the fewest digits to 0)
            X>  % And take the maximum

James

Posted 2016-09-16T00:27:45.880

Reputation: 54 537

4

Retina, 24 16 bytes

O^`
O$#`
$.0
G1`

Try it online! or run all test cases.

Saved 8 bytes thanks to Martin!

The all test is using a slightly older version of the code, but the algorithm is identical. I'll update it to be closer when I get more time.

The trailing newline is significant. Sorts the numbers by reverse numeric value, then sorts them by number of digits. This leaves us with the largest number with the fewest digits in the first position, so we can just delete the remaining digits.

FryAmTheEggman

Posted 2016-09-16T00:27:45.880

Reputation: 16 206

If you make the input linefeed-separated you can omit the regex from both sorting stages and then use `G1`` for the last stage. – Martin Ender – 2016-09-16T05:56:22.083

Also, the first stage doesn't need #. You only care about relative order for a given integer length, and within one length lexicographic sorting of numbers is correct. – Martin Ender – 2016-09-16T06:21:49.400

@MartinEnder Thanks! I've added both your tips. I should have suggested \w+ as the default for sorting, that way I wouldn't need to struggle as much to make the test suites ;) – FryAmTheEggman – 2016-09-16T18:12:05.083

Here is another 16, in case it gives you any ideas for further golfing: http://retina.tryitonline.net/#code=bWAkCn4KT15gClJzYH4uKw&input=MQoxMgo5CjYzCjUKMTAy

– Martin Ender – 2016-09-16T18:44:46.673

4

Perl 6, 18 bytes

*.min:{.chars,-$_}

Explanation:

*\        # Whatever lambda
.min:     # find the minimum using

{         # bare block lambda with implicit parameter 「$_」

  .chars, # number of characters first ( implicit method call on 「$_」 )
  -$_     # then negative of the value in case of a tie
}

Usage:

say [738, 2383, 281, 938, 212, 1010].&( *.min:{.chars,-$_} ); # 938

my &code = *.min:{.chars,-$_}

say code [78, 99, 620, 10]; # 99

Brad Gilbert b2gills

Posted 2016-09-16T00:27:45.880

Reputation: 12 713

4

Mathematica, 33 31 bytes

Max@MinimalBy[#,IntegerLength]&

MinimalBy selects all the elements of the original input list with the smallest score according to IntegerLength, i.e., with the smallest number of digits; and then Max outputs the largest one.

Thanks to Martin Ender for finding, and then saving, 2 bytes for me :)

Greg Martin

Posted 2016-09-16T00:27:45.880

Reputation: 13 940

3

Jelly, 8 bytes

DL€İMị¹Ṁ

Try it online! or Verify all test cases.

Explanation

DL€İMị¹Ṁ  Input: list A
D         Convert each integer to a list of base 10 digits
 L€       Get the length of each list (number of digits of each)
   İ      Take the reciprocal of each
    M     Get the indices of the maximal values
      ¹   Get A
     ị    Select the values at those indices from A
       Ṁ  Find the maximum and return

miles

Posted 2016-09-16T00:27:45.880

Reputation: 15 654

How is this 8 bytes? Do all of these characters fit in ASCII? – Federico Poloni – 2016-09-16T14:29:37.780

1

@FedericoPoloni Yes, they do fit, although in another codepage.

– Erik the Outgolfer – 2016-09-16T14:52:10.350

3

J, 21 14 bytes

Saved 7 bytes thanks to miles and (indirectly) Jonathan!

{.@/:#@":"0,.-

This is a four-chain:

{.@/: (#@":"0 ,. -)

Let's walk over the input 10 27 232 1000. The inner fork consists of three tines. #@":"0 calculates the sizes, ,. concats each size with its negated (-) member. For input 10 27 232 1000, we are left with this:

   (#@":"0 ,. -) 10 27 232 1000
2   _10
2   _27
3  _232
4 _1000

Now, we have {.@/: as the outer tine. This is monadic first ({.) over dyadic sort (/:). That is, we'll be taking the first element of the result of dyadic /:. This sorts its right argument according to its left argument, which gives us for our input:

   (/: #@":"0 ,. -) 10 27 232 1000
27 10 232 1000

Then, using {. gives us the first element of that list, and we are done:

   ({.@/: #@":"0 ,. -) 10 27 232 1000
27

Old version

>./@(#~]=<./@])#@":"0

Still working on improvements. I golfed it down from 30, and I think this is good enough. I'm going to first break it down into basic parts:

   size =: #@":"0
   max =: >./
   min =: <./
   over =: @
   right =: ]
   left =: [
   selectMin =: #~ right = min over right

   f =: max over selectMin size
   f 3 4 5
5
   f 3 4 53
4
   f 343 42 53
53

Here's how this works.

>./@(#~ ] = <./@]) #@":"0

This is a monadic train, but this part is a hook. The verb >./@(#~ ] = <./@]) is called with left argument as the input to the main chain and the sizes, defined as #@":"0, as the right argument. This is computed as length (#) over (@) default format (":), that is, numeric stringification, which is made to apply to the 0-cells (i.e. members) of the input ("0).

Let's walk over the example input 409 12 13.

   (#@":"0) 409 12 13
3 2 2

Now for the inner verb, >./@(#~ ] = <./@]). It looks like >./@(...), which effectively means maximum value (>./) of (@) what's inside (...). As for the inside, this is a four-train, equivalent to this five-train:

[ #~ ] = <./@]

[ refers to the original argument, and ] refers to the size array; 409 12 13 and 3 2 2 respectively in this example. The right tine, <./@], computes the minimum size, 2 in this case. ] = <./@] is a boolean array of values equal to the minimum, 0 1 1 in this case. Finally, [ #~ ... takes values from the left argument according the right-argument mask. This means that elements that correspond to 0 are dropped and 1 retained. So we are left with 12 13. Finally, according to the above, the max is taken, giving us the correct result of 13, and we are done.

Conor O'Brien

Posted 2016-09-16T00:27:45.880

Reputation: 36 228

Some shuffling plus a hook can save a byte >./@#~[:(=<./)#@":"0. I think there might be a bit more to save – miles – 2016-09-16T02:05:07.313

@miles XD I just finished writing explanation. Ah well, let me take a look at this beauty... – Conor O'Brien – 2016-09-16T02:06:19.920

Jonathan found a better method. If we convert it to J, its 14 bytes {.@/:#@":"0,.- but the input has to be shaped as a list – miles – 2016-09-16T03:29:11.837

@miles "shaped as a list"? You mean, like 400 12 13? – Conor O'Brien – 2016-09-16T10:49:54.807

3

JavaScript (ES6), 51

l=>l.sort((a,b)=>(a+l).length-(b+l).length||b-a)[0]

Test

f=l=>l.sort((a,b)=>(a+l).length-(b+l).length||b-a)[0]

;[
 [[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]
].forEach(([l,x])=>{
  var r=f(l)
  console.log(r==x?'OK':'KO',l+' -> '+r)
})  

edc65

Posted 2016-09-16T00:27:45.880

Reputation: 31 086

2

JavaScript (ES6), 62 bytes

var solution =

a=>a.map(n=>(l=`${n}`.length)>a?l>a+1|n<r?0:r=n:(a=l-1,r=n))|r

;document.write('<pre>' + `
[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
`.split('\n').slice(1, -1).map(c =>
  c + ', result: ' + solution(eval(c.slice(0, c.indexOf('->'))))
).join('\n'))

user81655

Posted 2016-09-16T00:27:45.880

Reputation: 10 181

2

Java 7, 112 104 bytes

int c(int[]a){int i=a[0],j;for(int b:a)i=(j=(i+"").length()-(b+"").length())>0?b:b>i&j==0?b:i;return i;}

Different approach to save multiple bytes thanks to @Barteks2x.

Ungolfed & test cases:

Try it here.

class M{
  static int c(int[] a){
    int i = a[0],
        j;
    for(int b : a){
      i = (j = (i+"").length() - (b+"").length()) > 0
           ? b
           : b > i & j == 0
              ? b
              : i;
    }
    return i;
  }

  public static void main(String[] a){
    System.out.println(c(new int[]{ 1 }));
    System.out.println(c(new int[]{ 9 }));
    System.out.println(c(new int[]{ 1729 }));
    System.out.println(c(new int[]{ 1, 1 }));
    System.out.println(c(new int[]{ 34, 3 }));
    System.out.println(c(new int[]{ 409, 12, 13 }));
    System.out.println(c(new int[]{ 11, 11, 11, 1 }));
    System.out.println(c(new int[]{ 11, 11, 11, 11 }));
    System.out.println(c(new int[]{ 78, 99, 620, 1 }));
    System.out.println(c(new int[]{ 78, 99, 620, 100 }));
    System.out.println(c(new int[]{ 1, 5, 9, 12, 63, 102 }));
    System.out.println(c(new int[]{ 3451, 29820, 2983, 1223, 1337 }));
    System.out.println(c(new int[]{ 738, 2383, 281, 938, 212, 1010 }));
  }
}

Output:

1
9
1729
1
3
13
1
11
1
99
9
3451
938

Kevin Cruijssen

Posted 2016-09-16T00:27:45.880

Reputation: 67 575

1shorter version: int c(int[]a){int i=a[0],j;for(int b:a)i=(j=(i+"").length()-(b+"").length())>0?b:b>i&j==0?b:i;return i;} – barteks2x – 2016-09-17T20:40:45.453

@Barteks2x Thanks, I've edited it. – Kevin Cruijssen – 2016-09-17T21:29:18.470

2

dc, 54 bytes

?dZsL0sN[dsNdZsL]su[dlN<u]sU[dZlL=UdZlL>ukz0<R]dsRxlNp

Explanation:

?dZsL0sN                  # read input, initialize L (length) and N (number)
[dsNdZsL]su               # macro (function) 'u' updates the values of L and N
[dlN<u]sU                 # macro 'U' calls 'u' if N < curr_nr
[dZlL=U dZlL>ukz0<R]dsR   # macro 'R' is a loop that calls 'U' if L == curr_nr_len
                          #or 'u' if L > curr_nr_len
xlNp                      # the main: call 'R' and print N at the end

Run example: 'input.txt' contains all the test cases in the question's statement

while read list;do echo "$list -> "$(dc -f program.dc <<< $list);done < input.txt

Output:

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

seshoumara

Posted 2016-09-16T00:27:45.880

Reputation: 2 878

2

bash, awk, sort 53 bytes

set `awk '{print $0,length($0)}'|sort -rnk2n`;echo $1

Read input from stdin, one value per line

bash and sort, 58 57 bytes

set `sort -n`;while((${#2}==${#1}));do shift;done;echo $1

Emmanuel

Posted 2016-09-16T00:27:45.880

Reputation: 259

doesn't work for last sample gave 2383 instead of 938 – Archemar – 2016-09-16T14:47:17.000

@Archemar sorry I misread the question, it is corrected now – Emmanuel – 2016-09-16T16:54:45.343

You can remove the space between while and ((. – seshoumara – 2016-09-17T11:26:24.833

1

JavaScript ES6, 80 77 70 bytes

a=>Math.max(...a.filter(l=>l.length==Math.min(...a.map(i=>i.length))))

I hope I am going in the right direction...

Downgoat

Posted 2016-09-16T00:27:45.880

Reputation: 27 116

Could you replace a.map(i=>i.length).sort((a,b)=>a-b)[0] with Math.min(...a.map(i=>i.length))? – user81655 – 2016-09-16T04:58:53.593

@user81655 yup, I can. I thought I had made that edit but apparently I did not – Downgoat – 2016-09-16T04:59:54.530

You could also try negating the minimum so that you can reuse the Math.max: a=>(m=Math.max)(...a.filter(l=>l.length==-m(...a.map(i=>-i.length)))) It seems to save only 1 byte though. – user81655 – 2016-09-16T05:03:24.543

For another byte the filter can be replaced with a map that returns 0 for values that do not pass the test: a=>(m=Math.max)(...a.map(l=>l.length+m(...a.map(i=>-i.length))?0:l)) – user81655 – 2016-09-16T05:12:38.647

1

Brachylog, 16 bytes

or:@]feL:la#=,Lh

Try it online!

Explanation

or                 Sort the list in descending order.
  :@]f             Find all suffixes of the list.
      eL           Take one suffix L of the list.
        :la        Apply length to all numbers in that suffix.
           #=,     All lengths must be equal.
              Lh   Output is the first element of L.

Fatalize

Posted 2016-09-16T00:27:45.880

Reputation: 32 976

1

Haskell, 39 bytes

snd.maximum.map((0-).length.show>>=(,))

Damien

Posted 2016-09-16T00:27:45.880

Reputation: 2 407

This doesn't work, it prefers 34 to 2. – xnor – 2016-09-16T06:39:42.210

oh, thanks. I have to rethink it.. – Damien – 2016-09-16T06:42:04.057

Works better now! – Damien – 2016-09-16T06:52:51.420

1

Javascript (ES6), 57 54 53 bytes

l=>l.sort((a,b)=>(s=a=>1/a+`${a}`.length)(a)-s(b))[0]

For the record, my previous version was more math-oriented but 1 byte bigger:

l=>l.sort((a,b)=>(s=a=>1/a-~Math.log10(a))(a)-s(b))[0]

Test cases

let f =
l=>l.sort((a,b)=>(s=a=>1/a+`${a}`.length)(a)-s(b))[0]

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

Arnauld

Posted 2016-09-16T00:27:45.880

Reputation: 111 334

1

Perl, 38 37 bytes

Includes +1 for -a

Give input on STDIN:

perl -M5.010 maxmin.pl <<< "3451 29820 2983 1223 1337"

maxmin.pl:

#!/usr/bin/perl -a
\$G[99-y///c][$_]for@F;say$#{$G[-1]}

Uses memory linear in the largest number, so don't try this on too large numbers. A solution without that flaw is 38 bytes:

#!/usr/bin/perl -p
$.++until$\=(sort/\b\S{$.}\b/g)[-1]}{

All of these are very awkward and don't feel optimal at all...

Ton Hospel

Posted 2016-09-16T00:27:45.880

Reputation: 14 114

1

MATL, 11 bytes

tV48\&XS0))

Input is a column vector (using ; as separator), such as

[78; 99; 620; 100]

Try it online! Or verify all test cases.

Explanation

Let's use input [78; 99; 620; 100] as an example.

t      % Input column vector implicitly. Duplicate
       %   STACK: [78; 99; 620; 100], [78; 99; 620; 100]
V      % Convert to string. Each number is a row, left-padded with spaces
       %   STACK: [78; 99; 620; 100], [' 78'; ' 99'; '620'; '100']
48\    % Modulo 48. This transforms each digit into the corresponding number,
       % and space into 32. Thus space becomes the largest "digit"
       %   STACK: [78; 99; 620; 100], [32 7 8; 32 9 9; 6 2 0; 1 0 0]
&XS    % Sort rows in lexicographical order, and push the indices of the sorting
       %   STACK: [78; 99; 620; 100], [4; 3; 1; 2]
0)     % Get last value
       %   STACK: [78; 99; 620; 100], 2
)      % Index
       %   STACK: 99
       % Implicitly display

Luis Mendo

Posted 2016-09-16T00:27:45.880

Reputation: 87 464

1Nice to see the stack states in your explanation! – flawr – 2016-09-16T23:18:27.090

1

R, 72 41 36 bytes

Rewrote the function with a new approach. Golfed 5 bytes thanks to a suggestion from @bouncyball.

n=nchar(i<-scan());max(i[n==min(n)])

Explained:

        i<-scan()       # Read input from stdin
n=nchar(         );     # Count the number of characters in each number in i
max(             )      # Return the maximum of the set where
    i[n==min(n)]        # the number of characters is the minimum number of characters.

function(i){while(1){if(length(o<-i[nchar(i)==T]))return(max(o));T=T+1}}

Indented/explained:

function(i){               # Take an input i
  while(1){                # Do the following continuously:
    if(length(
        o<-i[nchar(i)==T]) # Define o to be the subset of i with numbers of length T,
      )                    # where T is 1 (a built-in!).
                           # We take the length of this subset (its size), and then pass
                           # it to if(). Thanks to weak typing, this numeric is converted
                           # to a logical value. When this occurs, zero evaluates to FALSE
                           # and any non-zero number evaluates to TRUE. Therefore, the if()
                           # is TRUE iff the subset is not empty.
      return(max(o));      # If it's true, then we just return the largest element of the
                           # subset, breaking out of our loop.
    T=T+1                  # Otherwise, increment our counter and continue.
  }
}

rturnbull

Posted 2016-09-16T00:27:45.880

Reputation: 3 689

1Save 4 bytes by not defining function: i=scan();n=nchar(i);max(i[n==min(n)]) – bouncyball – 2016-09-16T14:13:49.290

@bouncyball Thanks! And 1 further byte saved by n=nchar(i<-scan()). – rturnbull – 2016-09-16T19:32:23.957

1

Bash + coreutils, 58 bytes

d=`sort -n`;egrep ^.{`sed q<<<"$d"|wc -L`}$<<<"$d"|tail -1

Input format is one value per line. Golfing suggestions are welcomed.

Explanation:

d=`sort -n`                             #save the list in ascending numerical order
egrep ^.{                    }$<<<"$d"  #print only list lines having as many chars
         `sed q<<<"$d"|wc -L`                 #as the first sorted line does
|tail -1                                #and then get the last one (the answer)

seshoumara

Posted 2016-09-16T00:27:45.880

Reputation: 2 878

+1 thank you now I know that sed q = head -1 – Emmanuel – 2016-09-19T00:58:07.300

1

Python 2 - 41 bytes

lambda l:max((-len(`x`),x) for x in l)[1]

davidedb

Posted 2016-09-16T00:27:45.880

Reputation: 111

0

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).

Solomon Ucko

Posted 2016-09-16T00:27:45.880

Reputation: 439

0

Python 2, 63 30 63 bytes

lambda a:a.sort()or[`i`for i in a if len(`i`)==len(`a[0]`)][-1]

acrolith

Posted 2016-09-16T00:27:45.880

Reputation: 3 728

Bugged for [1, 99, 620, 10000, 3]. – orlp – 2016-09-16T01:54:35.820

I agree with orlp, doesn't work – Destructible Lemon – 2016-09-16T02:07:19.767

Once again, a wrong anser with an upvote? This should be amended or deleted – edc65 – 2016-09-16T13:27:25.087

0

Python 2, 58 bytes

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

Daniel

Posted 2016-09-16T00:27:45.880

Reputation: 6 425

0

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

Destructible Lemon

Posted 2016-09-16T00:27:45.880

Reputation: 5 908

0

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

DLosc

Posted 2016-09-16T00:27:45.880

Reputation: 21 213

0

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.

user3810626

Posted 2016-09-16T00:27:45.880

Reputation: 111

0

PHP , 86 Bytes

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

Jörg Hülsermann

Posted 2016-09-16T00:27:45.880

Reputation: 13 026

0

Pyke, 7 bytes

.#_il)h

Try it here!

Blue

Posted 2016-09-16T00:27:45.880

Reputation: 26 661

0

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

Pete Arden

Posted 2016-09-16T00:27:45.880

Reputation: 1 151

0

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;};

Al R

Posted 2016-09-16T00:27:45.880

Reputation: 81

0

PowerShell v2+, 41 bytes

($args[0]|sort -des|sort{"$_".length})[0]

Takes input $args, sorts it by value in -descending order (so bigger numbers are first), then sorts that by the .length in ascending order (so shorter lengths are first). We then take the [0] element, which will be the biggest number with the fewest digits.

Examples

PS C:\Tools\Scripts\golfing> @(78,99,620,1),@(78,99,620,10),@(78,99,620,100),@(1,5,9,12,63,102),@(3451,29820,2983,1223,1337),@(738,2383,281,938,212,1010)|%{($_-join',')+" -> "+(.\output-largest-number-fewest-digits.ps1 $_)}
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

AdmBorkBork

Posted 2016-09-16T00:27:45.880

Reputation: 41 581

0

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);
}

Daniel Lerps

Posted 2016-09-16T00:27:45.880

Reputation: 151

There's an useless whitespace after you function signature and in the parameter list. Lambda statement should also work. – Yytsi – 2016-09-17T17:21:42.450

0

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();
}

Grax32

Posted 2016-09-16T00:27:45.880

Reputation: 1 282

int[]i will work. – Yytsi – 2016-09-17T17:22:55.123

0

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

Grax32

Posted 2016-09-16T00:27:45.880

Reputation: 1 282

0

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

rnso

Posted 2016-09-16T00:27:45.880

Reputation: 1 635

0

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.

Jonas

Posted 2016-09-16T00:27:45.880

Reputation: 177

0

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 });

Jonas

Posted 2016-09-16T00:27:45.880

Reputation: 177

0

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

user59819

Posted 2016-09-16T00:27:45.880

Reputation:

0

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..)

hdrz

Posted 2016-09-16T00:27:45.880

Reputation: 321