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.
-
\$\begingroup\$ Can the input numbers be on separate lines? \$\endgroup\$seshoumara– seshoumara2016年09月16日 05:48:10 +00:00Commented Sep 16, 2016 at 5:48
-
\$\begingroup\$ @seshoumara That sounds reasonable, yes. \$\endgroup\$Calvin's Hobbies– Calvin's Hobbies2016年09月16日 05:48:31 +00:00Commented Sep 16, 2016 at 5:48
68 Answers 68
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;};
Python 2, (削除) 63 (削除ここまで) (削除) 30 (削除ここまで) 63 bytes
lambda a:a.sort()or[`i`for i in a if len(`i`)==len(`a[0]`)][-1]
-
\$\begingroup\$ Bugged for
[1, 99, 620, 10000, 3]. \$\endgroup\$orlp– orlp2016年09月16日 01:54:35 +00:00Commented Sep 16, 2016 at 1:54 -
\$\begingroup\$ I agree with orlp, doesn't work \$\endgroup\$Destructible Lemon– Destructible Lemon2016年09月16日 02:07:19 +00:00Commented Sep 16, 2016 at 2:07
-
\$\begingroup\$ Once again, a wrong anser with an upvote? This should be amended or deleted \$\endgroup\$edc65– edc652016年09月16日 13:27:25 +00:00Commented Sep 16, 2016 at 13:27
Perl 5, 34 + 4 (-plF ) = 38 bytes
$a[$#F]=$_ if$_>$a[$#F]}{$\=1*"@a"
Linear time based on the number of entries in the list. Memory space is proportional to the length of the longest number.
AWK, 54 bytes
n||n=99{(n>x=length)&&n=x;x-n||0ドル>r&&r=0ドル}END{print r}
Takes input numbers on separate lines.
TI-84 BASIC (TI-84 Plus CE Python), 17 bytes
Input A
int(log(⸤A
max(⸤A*(Ans=min(Ans
gets the logarithm of each number in the list (int(log(⸤A), removes numbers that don't have the smallest amount of digits (⸤A*(Ans=min(Ans), and returns the max