可能的最大和最小数目


16

任务

给定非负数数组,输出可以通过连接它们而形成的最大和最小数。

规则

输入,输出可以采用任何方便的格式。

该数组最多可以有1个十进制数。

例子

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

这是代码高尔夫球,因此最短的代码获胜。


1
第一个输出不是:4422333355.55,55.5544333322吗?它们分别较小和较大。
ouflak

@ouflak,您必须输出最大和最小。
Vedant Kandoi '18

1
没关系,现在看看。与小数点混淆了。
ouflak

[[55.55,22,33,33,44],[44,33,33,22,55.55]]是可接受的输出格式吗?
毛茸茸的

1
任何方法都可以@Jordan
Vedant Kandoi '18

Answers:



3

05AB1E,11个字节

œJΣ'.¡ï}Á2£

在线尝试! 或作为测试套件

说明

 œ             # 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

太糟糕了,我们没有cast to float。我记得在Legacy中有一个,但是我只是看了一下,显然是a stringified cast to float,它非常没用,因为您希望使用浮点数而不是字符串进行排序
。.xD

@KevinCruijssen:是的。我们一个is_number,但遗憾的是没有to_number
Emigna '18

3

JavaScript(ES6),68 66字节

a=>[1,-1].map(n=>a.sort((a,b)=>[a%1||a]+b<[b%1||b]+a?n:-n).join``)

在线尝试!

怎么样?

我们使用以下测试比较输入数组中的两个值:

[a % 1 || a] + b < [b % 1 || b] + a

x % 1 || xXXX

该表达式[x % 1 || x] + y将上述结果强制为字符串,并将其与其他值连接在一起。

如果列表中有十进制数,则必须始终将其视为最小值。通过应用转换,十进制数字将转换为以开头的字符串"0.",该字符串按字典顺序排在其他任何东西之前。

例子:

  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"

3

Japt,14个 11字节

á m¬ñn é v2

尝试一下

多亏Luis节省了1个字节,请也+1解决他的问题。

á               :Permutations
  m             :Map
   ¬            :  Join
    ñ           :Sort by
     n          :  Converting each to a number
       é        :Rotate right
         v2     :Remove & return the first 2 elements

好的,这样更好!该死的!!这é是隐藏的这么长的时间!
路易斯·费利佩·德·耶稣·穆诺兹


3

果冻,6个字节

Œ!VṢ.ị

在线尝试!

说明:

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

我从不使用Jelly编程,但是这.ị是获取列表的第一项和最后一项的一种很酷的方法。好答案!向我+1。
凯文·克鲁伊森

1
@KevinCruijssen实际上得到了最后一个项目。:P
暴民埃里克(Erik the Outgolfer)

我也从未在Jelly中编程,但是我对.ị工作原理感到困惑。如果我正确阅读了文档,我认为会在floor(x)和ceil(x)处获取y的元素,并且.0.5。这是否意味着它将在索引0和1处获得y的元素?
Cowabunghole

Jelly 1是基于索引的吗?那可以解释这种行为,但是我在文档中什么都没有看到。
Cowabunghole

1
@Cowabunghole是的,Jelly是1索引的。
Erik the Outgolfer


2

Pyth,13 12字节

hM_BSvsM.p`M

以形式输出[smallest, largest]在此处在线尝试,或在此处一次验证所有测试用例。

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

编辑:通过将字符串化移出映射函数来保存一个字节。先前版本: hM_BSmvs`Md.p


2

Perl 6,41个字节

{.max,.min}o+<<*.permutations.map(*.join)

在线尝试!

备择方案:

{.max,.min}o+<<*.permutations.map:{.join}
{.max,.min}o{[map +*.join,.permutations]}
{.max,.min}o{+<<map *.join,.permutations}

1

外壳,10字节

§,▼▲morṁsP

在线尝试全部验证!

说明

§,▼▲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)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.