7
\$\begingroup\$

Given a non-empty list of digits 0 though 9, output the smallest number that can be produced by an expression formed by reordering these digits and introducing exponentiation signs ^, with adjacent digits getting concatenated as multi-digit numbers. Exponentiation is evaluated as right-associative.

For example, [4, 2, 3, 3] could become 2^34^3 which evaluates as 2^(34^3), but simply writing 2334 is smallest here. You can assume that any array containing a 0 gives a minimum of zero (a special case), or that it's achievable by an expression like 0^rest, or like 00 for just zeroes.

The input list can be taken as any ordered sequence, but not as an unordered (multi)set.

Test cases:

[2,3] => 8 (2^3)
[3,3] => 27 (3^3)
[4,3] => 34 (34)
[1,2,3] => 1 (1^2^3)
[2,2,2] => 16 (2^2^2)
[2,3,2] => 81 (2^3^2) 
[3,0] => 0 (Special, or maybe 0^3)
[0,1,0] => 0 (Special, or maybe 0^10)

Shortest code in each language wins. I'll accept an answer just to give the +15 rep without any specific accepting standard.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ "Acception only act as a +15." Are you saying that when/if you accept an answer, it will only count as 15 reputation? \$\endgroup\$
    – ouflak
    May 11, 2020 at 12:05
  • \$\begingroup\$ I like the challenge concept, but your text is hard to read and it took me a few tries to understand it. \$\endgroup\$
    – xnor
    May 11, 2020 at 12:05
  • \$\begingroup\$ @ouflak That means if someone is accept, it only mean he get 15 reputation, I don't claim any accepting standard \$\endgroup\$
    – l4m2
    May 11, 2020 at 12:07
  • 7
    \$\begingroup\$ For golfers who might want to write a non-brute-force solution, it looks like the minimal result is the digits concatenated in sorted order, except for smaller values of 0 for any list with a 0, 1 for any list with a 1, and these six values produced from power expressions: 2^2=4, 2^3=8, 2^4=16, 3^3=27, 2^2^2=16, 3^2^2=81. \$\endgroup\$
    – xnor
    May 11, 2020 at 12:23
  • 1
    \$\begingroup\$ Probably worth including [2,3,2] in the examples. \$\endgroup\$ May 11, 2020 at 17:16

6 Answers 6

2
\$\begingroup\$

05AB1E, 12 11 bytes

œε.œJ€.«m}ß

Try it online or verify all test cases or try it online with added debug-lines.

Explanation:

œ            # Get all permutations of the (implicit) input-list
 ε           # Map each permutation to:
  .œ         #  Get all its partitions
    J        #  Join each inner-most list together
     €       #  For each inner list:
      .«     #   Right-reduce it by:
        m    #    Taking the power
         }ß  # After the map: pop and push the flattened minimum
             # (after which it is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

Jelly, 10 bytes

Œ!*@/€;ḌƊṂ

A monadic link accepting a list of digits yields an integer.

Try it online!

How?

Checks digital and power-wise evaluations of all permutations even though we only need to check forward-sorted-digital, forward-sorted-power-wise, and reverse-sorted-power-wise (and only for [3,2,2]), because it's far terser.

Note that there is no need to check any mixture of digital and power-wise evaluations (they can never be strictly smaller than one of the three previously mentioned evaluations)

Œ!*@/€;ḌƊṂ - Link: list, L
Œ!         - all permutations (of L) -> P
        Ɗ  - last three links as a monad:
     €     -   for each (p in P):
    /      -     reduce with:
   @       -       using swapped arguments:
  *        -         exponentiation
       Ḍ   -   un-decimal (vectorises across P)
      ;    -   concatenate (these two lists of numbers)
         Ṃ - minimum
\$\endgroup\$
1
  • \$\begingroup\$ Notice that you happen to use 0^1^0^0 on [0,0,0,1] and 0000 on [0,0,0,0]. Unexpected work :) \$\endgroup\$
    – l4m2
    May 11, 2020 at 19:34
1
\$\begingroup\$

Japt -g, 12 bytes

á Ër!p mDìÃn

Try it

\$\endgroup\$
7
  • \$\begingroup\$ Fails for [2,2,3], which results in 64 instead of 81. \$\endgroup\$ May 11, 2020 at 16:30
  • \$\begingroup\$ You don't write \$2{^2} {^3}\$, but \$3^{2^2}\$ is fine \$\endgroup\$
    – l4m2
    May 11, 2020 at 16:33
  • \$\begingroup\$ @KevinCruijssen, 2**3**2=64 \$\endgroup\$
    – Shaggy
    May 11, 2020 at 17:04
  • \$\begingroup\$ "and power must go from right to left" -> 2**(3**2) = 512 \$\endgroup\$ May 11, 2020 at 17:09
  • 2
    \$\begingroup\$ It means that the precedence is to be treated as right-most first, i.e. 2**3**2 is to be \$2^{(3^2)}=2^9=512\$. \$\endgroup\$ May 11, 2020 at 17:14
0
\$\begingroup\$

Io, 77 bytes

Based on xnor's observation in the comment section. -12 bytes thanks to Arnauld's idea.

method(x,y :=x sort;if(y==list(2,2,3),81,y join()asNumber min(y reduce(**))))

Try it online!

Explanation

method(x,           // Anonymous method with param x
    y := x sort     // Assign y to sorted x
    if(y == list(2,2,3), // Edge case treatment
        81,              // (It's a kinda weird case that goes right to left)
        ,y join()asNumber// Join the sorted digit-list into a single number.
        min(                // Minimum with:
            y reduce(**)))) // the sorted list reduced by the power function.
\$\endgroup\$
0
0
\$\begingroup\$

JavaScript (ES7),  65  62 bytes

Fixed thanks to @xnor and @l4m2
Saved 3 bytes thanks to @l4m2

a=>Math.min(x=a.sort().join``,a[0]&&eval(x-223?a.join`**`:81))

Try it online!

How?

The general case is to look for the minimum of the concatenation of the digits in ascending order and an exponent chain, also in ascending order.

But as noticed by @xnor, there are a few special cases:

  • if the list contains a \$0\$, the result is always \$0\$
  • for [3,2,2], the minimum value is \$3^{2^2}=81\$
\$\endgroup\$
9
  • 2
    \$\begingroup\$ Be careful with the [3,2,2] input \$\endgroup\$
    – xnor
    May 11, 2020 at 13:31
  • \$\begingroup\$ @xnor Thank you for the notification. Hopefully fixed now. \$\endgroup\$
    – Arnauld
    May 11, 2020 at 13:46
  • \$\begingroup\$ Fail on [0,0,1] \$\endgroup\$
    – l4m2
    May 11, 2020 at 15:40
  • \$\begingroup\$ @l4m2 Thanks. Updated. \$\endgroup\$
    – Arnauld
    May 11, 2020 at 16:02
  • \$\begingroup\$ Fail on [3,2,2,0,0]. I consider x*a[0] in numbers for min instead \$\endgroup\$
    – l4m2
    May 11, 2020 at 16:08
0
\$\begingroup\$

perl -alp, 83 bytes

%_=(22,4,23,8,24,16,33,27,222,16,223,81);$_=join"",sort@F;$_=/0/||/1/?$&:$_{$_}||$_

Try it online!

Reads a list of white space separated numbers from STDIN, writes the answer to STDOUT. If the input contains a 0, output 0, else, if the input contains a 1, output a 1. Else, if it's one of 6 special cases, output the corresponding value. Otherwise, output the sorted input.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.