Largest and Smallest Possible Number

Vedant Kandoi 11/15/2018. 10 answers, 2.191 views
code-golf

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


TFeld 11/15/2018.

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

Try it online!

-2 bytes, thanks to Arnauld


Emigna 11/15/2018.

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

Arnauld 11/15/2018.

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

Try it online!

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"

Shaggy 11/15/2018.

Japt, 14 11 bytes

á m¬ñn é v2

Try it

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

Japt, 14 11 10 bytes

á m¬ñn gJò

Try it online!


Erik the Outgolfer 11/15/2018.

Jelly, 6 bytes

Œ!VṢ.ị

Try it online!

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)

Travis 11/16/2018.

Ruby, 56 45 bytes

->a{a.permutation.map{|p|p.join.to_f}.minmax}

Try it online!

-11 bytes, thanks Jordan


Sok 11/15/2018.

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


nwellnhof 11/15/2018.

Perl 6, 41 bytes

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

Try it online!

Alternatives:

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

BMO 11/15/2018.

Husk, 10 bytes

§,▼▲morṁsP

Try it online or verify all!

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)

HighResolutionMusic.com - Download Hi-Res Songs

1 Ariana Grande

7 Rings flac

Ariana Grande. 2019. Writer: Ariana Grande;Richard Rodgers;TBHits;Njomza;Michael "Mikey" Foster;Kaydence;Tayla Parx;Scootie;Oscar Hammerstein II;Victoria Monét.
2 Alan Walker

Lily flac

Alan Walker. 2018. Writer: Alan Walker;Lars Kristian Rosness;Magnus Bertelsen;K-391;Didrik Handlykken;Marcus Arnbekk.
3 Alec Benjamin

Let Me Down Slowly flac

Alec Benjamin. 2019. Writer: Alec Benjamin;Sir Nolan;Michael Pollack.
4 Alan Walker

Lost Control flac

Alan Walker. 2018. Writer: Alan Walker;Thomas Troelsen;Mood Melodies;Sorana;Fredrik Borch Olsen;Magnus "Magnify" Martinsen.
5 Skylar Grey

Everything I Need flac

Skylar Grey. 2018. Writer: Elliott Taylor;Rupert Gregson-Williams;Skylar Grey.
6 Post Malone

Sunflower flac

Post Malone. 2018. Writer: Carl Rosen;Louis Bell;Billy Walsh;Carter Lang;Swae Lee;Post Malone.
7 Westlife

Hello My Love flac

Westlife. 2019. Writer: Steve Mac;Ed Sheeran.
8 Alan Walker

Different World flac

Alan Walker. 2018. Writer: Shy Nodi;Alan Walker;Fredrik Borch Olsen;James Njie;Marcus Arnbekk;Gunnar Greve Pettersen;K-391;Corsak;Shy Martin;Magnus Bertelsen.
9 Sam Smith

Fire On Fire flac

Sam Smith. 2018. Writer: Steve Mac;Sam Smith.
10 Conor Maynard

Way Back Home (Sam Feldt Edit) flac

Conor Maynard. 2018. Writer: Ji Hye Lee;Shaun.
11 Normani

Dancing With A Stranger flac

Normani. 2019. Writer: Mikkel S. Eriksen;Tor Hermansen;Jimmy Napes;Normani;Sam Smith.
12 Slushii

Never Let You Go flac

Slushii. 2019. Writer: Sofía Reyes;Slushii;Aviella Winder.
13 Skrillex

Face My Fears flac

Skrillex. 2019. Writer: Poo Bear;Skrillex;Utada Hikaru.
14 Alora & Senii

Love U So flac

Alora & Senii. 2019. Writer: Alora & Senii.
15 The Chainsmokers

Hope flac

The Chainsmokers. 2018. Writer: Kate Morgan;Chris Lyon;Alex Pall;Andrew Taggart.
16 Imagine Dragons

Believer flac

Imagine Dragons. 2019. Writer: Dan Reynolds;Lil Wayne;Wayne Sermon;Ben McKee;Daniel Platzman;Robin Fredriksson;Mattias Larsson;Justin Tranter.
17 Alan Walker

I Don't Wanna Go flac

Alan Walker. 2018.
18 Mike Perry

Runaway flac

Mike Perry. 2019. Writer: Andreas Wiman;Dimitri Vangelis;Richard Müller;Sasha Rangas;Stefan Van Leijsen;Mike Perry.
19 Gesaffelstein

Lost In The Fire flac

Gesaffelstein. 2019. Writer: Ahmad Balshe;Nate Donmoyer;Gesaffelstein;DaHeala;The Weeknd.
20 Hozier

Almost (Sweet Music) flac

Hozier. 2019. Writer: Hozier.

Related questions

Hot questions

Language

Popular Tags