Avec une liste non décimale d’entiers décimaux positifs, indiquez le plus grand nombre de l’ensemble des nombres comportant le moins de chiffres.
La liste des entrées ne sera pas dans un ordre particulier et peut contenir des valeurs répétées.
Exemples:
[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
Le code le plus court en octets gagne.
code-golf
number
arithmetic
Les passe-temps de Calvin
la source
la source
Réponses:
Pyth,
736 octetsSuite de tests
Explication:
Solution 7 octets:
Suite de tests
Explication:
la source
Python 2,
4842 bytes-6 bytes thanks to @Dennis (use
min
rather thansorted
)All test cases are at ideone
Take the minimum of the list by (length, -value)
la source
min
should work instead ofsorted
.sorted()[0]
formin
? I consider that a trivial modification of your original code.len(`x`)+1./x
for the same length. Too bad you need the1.
.Jelly, 7 bytes
Test it at TryItOnline
Or see all test cases also at TryItOnline
How?
la source
05AB1E, 5 bytes
Code:
Explanation:
Uses the CP-1252 encoding. Try it online!
la source
Ruby, 34 bytes
See it on eval.in: https://eval.in/643153
la source
MATL, 14 bytes
Try it online!
Explanation:
la source
Retina,
2416 bytesTry 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.
la source
G1`
for the last stage.#
. You only care about relative order for a given integer length, and within one length lexicographic sorting of numbers is correct.\w+
as the default for sorting, that way I wouldn't need to struggle as much to make the test suites ;)Mathematica,
3331 bytesMinimalBy 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 :)
la source
Perl 6, 18 bytes
Explanation:
Usage:
la source
Jelly, 8 bytes
Try it online! or Verify all test cases.
Explanation
la source
JavaScript (ES6), 51
Test
la source
J,
2114 bytesSaved 7 bytes thanks to miles and (indirectly) Jonathan!
This is a four-chain:
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 input10 27 232 1000
, we are left with this: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:Then, using
{.
gives us the first element of that list, and we are done:Old version
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:
Here's how this works.
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
.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
and3 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 to0
are dropped and1
retained. So we are left with12 13
. Finally, according to the above, the max is taken, giving us the correct result of13
, and we are done.la source
>./@#~[:(=<./)#@":"0
. I think there might be a bit more to save{.@/:#@":"0,.-
but the input has to be shaped as a list400 12 13
?JavaScript (ES6), 62 bytes
la source
dc, 54 bytes
Explanation:
Run example: 'input.txt' contains all the test cases in the question's statement
Output:
la source
Java 7,
112104 bytesDifferent approach to save multiple bytes thanks to @Barteks2x.
Ungolfed & test cases:
Try it here.
Output:
la source
bash, awk, sort 53 bytes
Read input from stdin, one value per line
bash and sort,
5857 bytesla source
while
and((
.JavaScript ES6,
807770 bytesI hope I am going in the right direction...
la source
a.map(i=>i.length).sort((a,b)=>a-b)[0]
withMath.min(...a.map(i=>i.length))
?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.filter
can be replaced with amap
that returns0
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))
Brachylog, 16 bytes
Try it online!
Explanation
la source
Haskell, 39 bytes
la source
34
to2
.Javascript (ES6),
575453 bytesFor the record, my previous version was more math-oriented but 1 byte bigger:
Test cases
la source
MATL, 11 bytes
Input is a column vector (using
;
as separator), such asTry it online! Or verify all test cases.
Explanation
Let's use input
[78; 99; 620; 100]
as an example.la source
Perl,
3837 bytesIncludes +1 for
-a
Give input on STDIN:
maxmin.pl
:Uses memory linear in the largest number, so don't try this on too large numbers. A solution without that flaw is 38 bytes:
All of these are very awkward and don't feel optimal at all...
la source
R,
724136 bytesRewrote the function with a new approach. Golfed 5 bytes thanks to a suggestion from @bouncyball.
Explained:
Indented/explained:
la source
function
:i=scan();n=nchar(i);max(i[n==min(n)])
n=nchar(i<-scan())
.Bash + coreutils, 58 bytes
Input format is one value per line. Golfing suggestions are welcomed.
Explanation:
la source
sed q
=head -1
Python 2 - 41 bytes
la source
Python 2, 58 bytes
la source
Python 3, 56 bytes
Uses a lambda in a lambda!
Python 2, 53 bytes
Same but with backticks
la source
Pip, 11 bytes
Takes input as command-line args. Try it online!
First time using the
S
ort-K
eyed operator! Like Python'ssorted()
, 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:la source
Clojure, 63 bytes
as in:
Though I'm sure there's a way to make it smaller.
la source
PHP , 86 Bytes
la source