Task
Given an array of non-negative numbers, output the largest and smallest possible number that can be formed by joining them.
Rules
Input,Output can be in any convenient format.
The array may have at most 1 decimal number.
Examples
input:[22,33,44,55.55,33]
output:4433332255.55,55.5522333344
input:[34,900,3,11,9]
output:990034311,113349009
input:[99.93,9,3,39]
output:939399.93,99.933399
input:[45.45,45,45,45]
output:45454545.45,45.45454545
input:[12,21,34,43,45.1]
output:4334211245.1,45.112213443
This is code-golf so shortest code wins.
10 Answers 10
Python 2, (削除) 84 (削除ここまで) (削除) 80 (削除ここまで) (削除) 78 (削除ここまで) 76 bytes
lambda a:[''.join(sorted(a,key=lambda s:'.'in s or s+s)[::i])for i in[-1,1]]
-2 bytes, thanks to Arnauld
05AB1E, 11 bytes
œJΣ'.¡ï}Á2£
Try it online! or as a Test Suite
Explanation
œ # get permutations of input
J # join each
Σ } # sort by
'.¡ # splitting into [before decimal, after decimal]
ï # and converting each to int
Á # rotate the result right
2£ # and take the first 2 values
-
\$\begingroup\$ Too bad we don't have a
cast to float
. I remember there was one in the Legacy, but I just looked it up and apparently it was astringified cast to float
, which is pretty useless since you want floats instead of strings to sort on.. xD \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年11月15日 13:25:16 +00:00Commented Nov 15, 2018 at 13:25 -
\$\begingroup\$ @KevinCruijssen: Yeah. We had an
is_number
but unfortunately noto_number
. \$\endgroup\$Emigna– Emigna2018年11月15日 13:33:23 +00:00Commented Nov 15, 2018 at 13:33
JavaScript (ES6), (削除) 68 (削除ここまで) 66 bytes
a=>[1,-1].map(n=>a.sort((a,b)=>[a%1||a]+b<[b%1||b]+a?n:-n).join``)
How?
We use the following test to compare two values in the input array:
[a % 1 || a] + b < [b % 1 || b] + a
The expression x % 1 || x
returns the decimal part of \$x\$ if \$x\$ is a decimal number, or leaves \$x\$ unchanged otherwise.
The expression [x % 1 || x] + y
coerces the above result to a string and concatenates it with the other value.
If there's a decimal number in the list, it must always be considered as the smallest value. By applying our conversion, a decimal number is turned into a string starting with "0."
, which is lexicographically ordered before anything else.
Examples:
a | b | [a%1||a]+b | [b%1||b]+a
----+-----+------------+------------
4 | 5 | "45" | "54"
10 | 11 | "1011" | "1110"
8 | 80 | "880" | "808"
7 | 9.5 | "79.5" | "0.57"
Japt, (削除) 14 (削除ここまで) 11 bytes
á m¬ñn é v2
1 byte saved thanks to Luis, please +1
his solution too.
á :Permutations
m :Map
¬ : Join
ñ :Sort by
n : Converting each to a number
é :Rotate right
v2 :Remove & return the first 2 elements
-
\$\begingroup\$ Ok, this is better!! damn!! that
é
was hidden all this time!! \$\endgroup\$Luis felipe De jesus Munoz– Luis felipe De jesus Munoz2018年11月15日 13:07:46 +00:00Commented Nov 15, 2018 at 13:07
-
\$\begingroup\$ Nice one. I had
á ®¬nÃn
for the first line - kicking myself that I didn't think of your way. \$\endgroup\$Shaggy– Shaggy2018年11月15日 12:34:12 +00:00Commented Nov 15, 2018 at 12:34 -
-
-
\$\begingroup\$ Dang! Looks like I gave up to soon; very nicely done. \$\endgroup\$Shaggy– Shaggy2018年11月15日 17:18:15 +00:00Commented Nov 15, 2018 at 17:18
-
\$\begingroup\$ Yep. Oliver helped me in the chat! very clever way to get first and last value from an array! \$\endgroup\$Luis felipe De jesus Munoz– Luis felipe De jesus Munoz2018年11月15日 17:19:04 +00:00Commented Nov 15, 2018 at 17:19
Jelly, 6 bytes
Œ!VṢ.ị
Explanation:
Œ!VṢ.ị Arguments: x
Œ! Permutations of x
V Concatenate the representations of each permutation's elements and evaluate the result as Jelly code
Ṣ Sort
.ị Get element at index 0.5, i.e. elements at indices 0 (last) and 1 (first)
-
\$\begingroup\$ I never program in Jelly, but
.ị
is a pretty cool way of getting both the first and last item of a list. Nice answer! +1 from me. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年11月15日 13:10:53 +00:00Commented Nov 15, 2018 at 13:10 -
1\$\begingroup\$ @KevinCruijssen It actually gets the last and first item. :P \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年11月15日 13:12:00 +00:00Commented Nov 15, 2018 at 13:12
-
\$\begingroup\$ I've also never programmed in Jelly, but I'm confused as to how
.ị
works. If I'm reading the docs correctly, I would thinkị
gets the element of y at floor(x) and ceil(x), and.
is0.5
. Doesn't that mean it will get elements of y at index 0 and 1? \$\endgroup\$Cowabunghole– Cowabunghole2018年11月15日 16:23:27 +00:00Commented Nov 15, 2018 at 16:23 -
\$\begingroup\$ Is Jelly 1-based indexed? That would explain this behavior but I don't see anything about that in the docs. \$\endgroup\$Cowabunghole– Cowabunghole2018年11月15日 16:59:49 +00:00Commented Nov 15, 2018 at 16:59
-
1\$\begingroup\$ @Cowabunghole Yes, Jelly is 1-indexed. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年11月15日 17:13:48 +00:00Commented Nov 15, 2018 at 17:13
Ruby, (削除) 56 (削除ここまで) 45 bytes
->a{a.permutation.map{|p|p.join.to_f}.minmax}
-11 bytes, thanks Jordan
-
1
Pyth, (削除) 13 (削除ここまで) 12 bytes
hM_BSvsM.p`M
Outputs in form [smallest, largest]
. Try it online here, or verify all the test cases at once here.
hM_BSvsM.p`MQ Implicit: Q=eval(input())
Trailing Q inferred
`MQ Stringify all elements of Q
.p Generate all permutations of the above
sM Concatenate each permutation
v Evaluate each as a number
S Sort them
_B Pair the sorted list with its reverse
hM Take the first element of each, implicit print
Edit: Saved a byte by taking stringification out of the mapping function. Previous version: hM_BSmvs`Md.p
Perl 6, 41 bytes
{.max,.min}o+<<*.permutations.map(*.join)
Alternatives:
{.max,.min}o+<<*.permutations.map:{.join}
{.max,.min}o{[map +*.join,.permutations]}
{.max,.min}o{+<<map *.join,.permutations}
Husk, 10 bytes
§,▼▲さんかくmorṁsP
Explanation
§,▼▲さんかくm(rṁs)P -- example input: [2,3,1.1]
P -- permutations: [[2,3,1.1],[3,2,1.1],[1.1,3,2],[3,1.1,2],[1.1,2,3],[2,1.1,3]]
m( ) -- map the following
(example with [1.1,2,3])
ṁs -- | show each and join: "1.123"
r -- | read: 1.123
-- : [231.1,321.1,1.132,31.12,1.123,21.13]
§, -- fork and join as tuple
▼ -- | min: 1.123
▲さんかく -- | max: 321.1
-- : (1.123,321.1)
[[55.55,22,33,33,44],[44,33,33,22,55.55]]
be an acceptable output format? \$\endgroup\$