Sii il più equo possibile


33

introduzione

In questa sfida dovresti dividere un numero intero in due pezzi. Dal momento che a nessuno piace ottenere il pezzo più piccolo di torta, il tuo obiettivo è quello di essere il più equo possibile. Ad esempio, se si desidera dividere il numero intero 7129in due parti, ci sono 3 modi possibili per farlo.

7,129, 71,29e 712,9sono tutte possibilità, ma71,29 è il modo più giusto di dividerlo in due pezzi perché riduce al minimo la differenza tra i due:

7 129 -> |7-129| = 122
71 29 -> |71-29| = 42
712 9 -> |712-9| = 703

Sfida

Dato un numero intero determinare il miglior modo possibile di partizionarlo come descritto sopra e riportare la differenza risultante.

Regole

  • La divisione ha senso solo per numeri interi di lunghezza almeno due, l'ingresso sarà sempre ≥ 10
  • L'input può essere un numero intero, un elenco di cifre o una stringa
  • Non è necessario gestire input non validi

Casi test

Devi solo segnalare la differenza risultante, il partizionamento è qui solo a scopo illustrativo:

10 -> 1,0 -> 1
11 -> 1,1 -> 0
12 -> 1,2 -> 1
13 -> 1,3 -> 2
101 -> 1,01 -> 0
128 -> 12,8 -> 4
313 -> 3,13 -> 10
1003 -> 1,003 -> 2
7129 -> 71,29 -> 42
81128 -> 81,128 -> 47
999999 -> 999,999 -> 0
9999999 -> 999,9999 or 9999,999 -> 9000

Risposte:


11

Brachylog , 12 11 byte

La mia prima risposta su Brachylog

Prendi input come stringa

{~cĊịᵐ-ȧ}ᶠ⌋

Provalo online!

Spiegazione:

will find all possible outputs for the predicate in {…} and store them in a list. ~c says that the output is a list that, when concatenated, is equal to the input. Next Ċ asserts that the the output of ~c has length 2.

ịᵐ converts both elements of the output into integers (this gets rid of leading 0s), takes the absolute difference of the two elements.

Once we have our list of all possible outputs, we get the minimum element with


10

Haskell, 48 bytes

f n=minimum[abs$n`div`10^k-n`mod`10^k|k<-[1..n]]

[1..n] makes this too slow for the larger test cases.

Try it online!


Nice job! I got so blind sided by using strings that I forgot that I could use arithmetic.
Wheat Wizard

9

05AB1E, 9 bytes

Code:

ā¤âε£ÆÄ}W

Uses the 05AB1E encoding. Try it online!

Explanation

ā            # Get the array [1, 2, .., len(input)]
 ¤â          # Cartesian product with the last element, (e.g. for input 12345:
               [[1, 5], [2, 5], [3, 5], [4, 5], [5, 5]])
   ε   }     # For each element:
    £        #   Get the substrings (12345 [3, 5] £ --> [123, 45])
     Æ       #   Reduce by subtraction
      Ä      #   Get the absolute value
        W    # Take the minimum of all results

1
If you replace £ with °‰ you won't need ¤â anymore.
Emigna


7

Perl 6, 40 bytes

{min map {abs [-] @$_},m:ex/^(.+)(.+)$/}

Test it

Expanded:

{  # bare block lambda with implicit parameter 「$_」

  min
    map
      {
        abs
          [-]    # reduce with &infix:«-»
            @$_  # the input of this inner block as a Positional
      },

      # split 「$_」 into 2 in every possible way
      m
      :exhaustive
      /^ (.+) (.+) $/
}



6

Prolog (SWI), 195 189 154 117 112 bytes

35 bytes saved thanks to Eminga

A*H:-findall(X,(between(0,A,I),r(A,I,X)),L),sort(L,[H|_]),!.
r(A,B,C):-Z is 10**B,divmod(A,Z,X,Y),C is abs(X-Y).

Try it online!

This is my first try at prolog golfing so it may be a bit horrendous. Here is how it works.

At the highest level we have *. * takes A and H, and determines if H is the smallest way to split A.

    A*H:-
      findall(X,(between(0,A,I),call(r,A,I,X)),L),
      sort(L,[H|_]),
      !.

The first line here uses a technique from this SO post, to essentially perform a map of the predicate r(A) over the integers from 0 to A. Since r confirms the values of each partitions this will give us the values of all possible partitions, plus a whole load of extra junk. All these partitions will be stored in L in no particular order. Once that is done we sort the list to find the smallest element. We then use a cut to prevent backtracing.

Next we have the definition of r. First r calculates the two results of the split naming them X and Y.

r(A,B,C):-
  Z is 10**B,
  divmod(A,Z,X,Y),
  C is abs(X-Y).

Then we assert that C is the difference of them and is positive.

  C is abs(X-Y).

There seem to be a misstake here as X is div(A,10**B),Y is div(A,10**B) will always give C=0 (meaning H will always be 0 as well). Should be Y is mod(A,10**B) I presume.
Emigna

The second row could also be r(A,B,C):-Z is 10**B,divmod(A,Z,X,Y),C is abs(X-Y). saving 32 bytes (If you're using SWI prolog at least, not sure about other versions).
Emigna

The first row could start with for example A*H instead of l(A,H) saving another 3. And if you are using SWI, you could add a TIO link
Emigna

Also, I don't think you need the ,! do you? There shouldn't be any backtracking at that point.
Emigna

@Emigna Thanks for the tips, I'll be implementing them shortly. I also thought ,! would not be necessary but when I test the program it does backtrace. It seems to try every possible ordering of L and then sorts them all. Meaning it will give the same answer A! times.
Wheat Wizard

5

Haskell, 68 65 bytes

f x=minimum[abs$read(take i x)-read(drop i x)|i<-[1..length x-1]]

Try it online!

Explanation

minimum              -- Minimum of ...
 [abs$               -- The absolute value of ...
  read(take i x)     -- The first i characters of x
  -                  -- Minus ...
   read(drop i x)    -- The last i characters of x
 |i<-[1..length x-1] -- From i=1 to i=length x - 1
 ]

4

Charcoal, 14 bytes

I⌊Eθ↔⁻I…θκI✂θκ

Try it online! Link is to verbose version of code. Conveniently I get to use the 2-arg variant of Slice. Explanation:

   θ            Input string
  E             Map over characters
        θ   θ   Input string
         κ   κ  Current map index
       …        Mold to length (i.e. head)
           ✂    Slice (i.e. tail)
      I   I     Cast to integer
     ⁻          Subtract
    ↔           Absolute value
 ⌊              Minimum
I               Cast to string
                Implicitly print

4

Jelly, 9 8 bytes

ḌÐƤḊạḌƤṂ

Try it online!

-1 byte thanks to Dennis. Input is a list of digits.

Explanation

ḌÐƤḊạḌƤṂ
ḌÐƤ          Convert to integer from decimal for all Ƥostfixes. [1,2,3]->[123,23,3]
   Ḋ         Remove the first element ->[23,3]
     ḌƤ      Convert to integer from decimal for all Ƥrefixes [1,2,3]->[1,12,123]
    ạ        Absolute difference. [23,3]ạ[1,12,123]->[22,9,123]
       Ṃ     Minimum

Hm, your explanation doesn't seem to reflect what your code actually does.
Erik the Outgolfer

@EriktheOutgolfer Is it the “remove the last element” part when it should say “remove the first element”? I’ll fix that, thanks for pointing it out
dylnan

3

Funky, 159 134 99 bytes

s=>{S={}fori=1i<#s i++{S[#S]=(((v=s::sublist)(0,i)::reduce@..-v(i)::reduce@..)^2)^.5};math.min...S}

Actually fitting the spec is shorter it seems.

Try it online!


3

Retina, 36 bytes

\B
,$'¶$`
\d+
$*
(1*),\1

Om`^.*
\G1

Try it online!

Explanation

\B
,$'¶$`

This generates all possible partitions on separate lines, as well as a trailing line with the original input.

\d+
$*

Convert each number in each partition to unary.

(1*),\1

Remove a maximal and equal amount of 1s from both parts of each partition (i.e. remove the minimum, and subtract it from the maximum, which gives the absolute difference).

Om`^.*

Sort the lines.

\G1

Count the 1s on the first line, which gives the minimal absolute difference.


3

J, 32, 27 23 bytes

-5 bytes thanks to FrownyFrog! -4 bytes if input is a string.

[:<./}:@(".\)|@-1}.".\.

Try it online!

Original: Takes a number as input

(".\(}:@[([:<./|@-)}.@])".\.)@":

How it works:

                             @": - convert the number to list of chars and
(".\                    ".\.)    - form all prefixes/suffixes and convert them to numbers
    (}:@[          }.@])         - drop the last prefix / first suffix
         (     |@-)              - find the absolute differences
          [:<./                  - find the minimum

Try it online!


@FrownyFrog - Thanks!
Galen Ivanov

3

JavaScript (ES6), 64 bytes

Takes input as a string.

f=([c,...s],l=0)=>c?Math.min(Math.abs((l+=c)-s.join``),f(s,l)):l

Test cases

Commented

f = ([c, ...s],           // c = current character, s = array of remaining characters
                l = 0) => // l = left part of the integer, initialized to 0 (see footnote)
  c ?                     // if c is defined:
    Math.min(             //   return the minimum of:
      Math.abs(           //     1) the absolute value of:
        (l += c) -        //       the updated left part
        s.join``          //       minus the right part
      ),                  //     end of Math.abs()
      f(s, l)             //     2) the result of a recursive call
    )                     //   end of Math.min()
  :                       // else:
    l                     //   stop the recursion by returning l (now equal to the input)

Non-recursive (ES7), 65 bytes

Takes input as a string.

s=>Math.min(...[...s].map(c=>((l+=c)-s.slice(++i))**2,i=l=0))**.5

Test cases

Commented

s =>                            // given s
  Math.min(...                  // get the minimum value in the result of this map():
    [...s].map(c =>             //   for each character c in s:
      ((l += c)                 //     append c to l (the left part)
                - s.slice(++i)) //     and subtract the right part from it
      ** 2,                     //     square the result
      i =                       //     start with i = 0 (split position)
      l = 0                     //     and l = 0 (left part, see footnote)
    )                           //   end of map()
  )                             // end of Math.min()
  ** .5                         // return the square root of the smallest square

Note: In both versions, l is coerced to a string on the first iteration. Normally, we should be careful about leading zeros in a numeric literal: 0123 - 10 === 73 because 0123 is parsed as an octal value (this is now deprecated, but still valid in non-strict mode). But '0123' - '10' === 113, the leading zero being this time ignored. So, it's sound to do so.

From the specification of the abstract operation ToNumber applied to a string:

A StringNumericLiteral that is decimal may have any number of leading 0 digits


3

APL (Dyalog), 27 bytes

{⌊/|-/⍎¨↑⊂∘⍵¨↓1,∘.=⍨⍳¯1+≢⍵}

Try it online!

How?

¯1+≢⍵ - length of n minus 1

∘.=⍨⍳ - identity matrix

      1,∘.=⍨⍳3
1 1 0 0
1 0 1 0
1 0 0 1

1, - prepend 1 for each row

- split by rows

⊂∘⍵¨ - for each, partition the string by it

      1 0 1 0  '7129'
┌──┬──┐
7129
└──┴──┘

- flatten

-/ - reduce each pair with subtraction

| - take absolute values

⌊/ - minimum


APL (Dyalog), 35 bytes

{⌊/|-/⍎¨(⊂∘⍵⍤1)1,∘.=⍨⍳¯1+≢⍵}

Try it online!


3

Jelly, 11 bytes

ŒṖṖLÐṂḌạ/€Ṃ

Try it online!

-3 bytes thanks to dylnan

How it works

ŒṖṖLÐṂḌạ/€Ṃ - Main link. Argument: n (integer)    e.g    7129
ŒṖ          - Partitions of n's digits;                  [[7, 1, 2, 9], [7, 1, [2, 9]], [7, [1, 2], 9], [7, [1, 2, 9]], [[7, 1], 2, 9], [[7, 1], [2, 9]], [[7, 1, 2], 9], [7, 1, 2, 9]]
  Ṗ         - Remove the final element                   [[7, 1, 2, 9], [7, 1, [2, 9]], [7, [1, 2], 9], [7, [1, 2, 9]], [[7, 1], 2, 9], [[7, 1], [2, 9]], [[7, 1, 2], 9]]
    ÐṂ      - Keep the lists with the minimum...         [[7, [1, 2, 9]], [[7, 1], [2, 9]], [[7, 1, 2], 9]]
   L        -   length
      Ḍ     - From digits                                [[7, 129], [71, 29], [712, 9]]
        /   - Reduce...
         €  - ...each...
       ạ    - ...by absolute difference                  [122, 42, 703]
          Ṃ - Take the minimum                           42

You can change L=2$$Ðf to ṖLÐṂ in this case
dylnan



1

MATL, 15 bytes

"GX@:&)UwU-|vX<

Input is a string representing the integer.

Try it online! Or verify all test cases.

Explanation

"         % Implicit input. Do the following as many times as input length
  G       %   Push input
  X@      %   Push iteration index (1-based), k
  :       %   Range: gives [1 2 ... k]
  &)      %   Two-ouput reference indexing: gives a substring with the first
          %   k characters in the input and then a substring with the rest
  U       %   Convert to number
  wU      %   Swap, convert to number
  -|      %   Absolute difference
  v       %   Vertically concatenate stack. This concatenates the obtained
          %   absolute difference with the minimum so far; does nothing in 
          %   the first iteration
  X<      %   Minimum of array
          % Implicit end. Implicit display


1

Clean, 106 83 bytes

import StdEnv
@n#f=toInt o(%)n
=hd(sort[abs(f(0,i)-f(i+1,size n))\\i<-[0..size n]])

Defines the function @, taking a string.

Mostly self-evident, the only tricky bit is f=toInt o(%)n: This takes the toInt class of functions, and composes it (o) with the curried slice operator class (%) already supplied with the first argument (n). Since there is only one type (String, equivalent to {#Char}) which has overloads for both % and toInt the line actually compiles, whereas normally it's hard to compose functions when golfing due to the lack of contextual information given to the compiler.

Try it online!


1

Jelly, 12 bytes

JṬ€œṗ€⁸Ḍạ/€Ṃ

A monadic link taking a list of digits and returning the integer.

Try it online!

How?

JṬ€œṗ€⁸Ḍạ/€Ṃ - Link: list of digits     e.g. [7,1,2,9]
J            - range of length               [1,2,3,4]
 Ṭ€          - untruth €ach                  [[1],[0,1],[0,0,1],[0,0,0,1]]
      ⁸      - chain's left argument         [7,1,2,9]
   œṗ€       - partition at truthy for €ach  [[[],[7,1,2,9]],[7,[1,2,9]],[[7,1],[2,9]],[[7,1,2],9]]
       Ḍ     - undecimal (vectorises)        [[0,7129],[7,129],[71,29],[712,9]]
         /€  - reduce €ach by:
        ạ    - absolute difference           [7129,122,42,703]
           Ṃ - minimum                       42

1

Pyth, 10 bytes

hSaMv<./Ql

Test suite

Takes input as a string.

This uses one of Pyth's more recent features, which is that applying a function to a list defaults to mapping the function over the list, if no other behavior is defined. This means that v applied to a list of list of strings evaluates all of the strings.

hSaMv<./Ql
hSaMv<./QlQ    Implicit variable
      ./Q      Form all partitions of the input string.
               Split it in all possible ways, maintaining the order.
               Partitions are ordered from shortest to longest.
     <   lQ    Take the prefix as long as the input string.
               This keeps just the splits into one and two pieces.
    v          Evaluate. All strings are converted to numbers.
  aM           Map the absolute difference function.
hS             Minimum

Note that the list of splits allows the split into 1 piece, but the value of this will always be larger than the minimum, so it is safely ignored.


1

Tcl, 116 bytes

foreach d [split [set b [set R $argv]] {}] {append L $d
regexp .(.+) $R - R
set b [expr min($b,abs($L-$R))]}
puts $b

Try it online!

Explanation

b ← R ← input number
for each digit (d) in the input number:
  L += d
  strip first digit off of R using a regular expression
  b ← min( b, distance between L and R )
print b

It works by using a regex trick allowing a degenerate final case that will always compute to greater than the minimum difference. For “12345” the values are:

1 2345 → 2344
12 345 → 333
123 45 → 78
1234 5 → 1229
12345 5 → 12340 (degenerate case)

You can shave bytes using lmap instead of foreach: tio.run/##LYuxCsMgFEV3v@IOb1DaZO8/ZHItDlolBEx4qC2FkG9/…
sergiol


1

APL+WIN, 31 bytes

⌊/|(⍎¨m↓¨⊂n)-⍎¨(m←⍳¯1+⍴n)↑¨⊂n←⎕

Prompts for screen input of integer as a string.

Explanation:

m←⍳¯1+⍴n Create a list of numbers from 1 to length of string - 1

↑¨⊂n←⎕ Using m create a nested vector taking successively characters from the front of the string defined by m

⍎¨ Convert from character to integer

(⍎¨m↓¨⊂n) Using m create a nested vector dropping successively characters from the front of the string defined by m 

⌊/| take the minimum absolute value after subtracting the two vectors of integers

I don't know APL, is there a way to test this?
ბიმო

Unfortunately APL+WIN is not in TIO. If you want to play with APL you can download a copy of APLX from the Dyalog website for free and my code works with it. It does not work in Dyalog's Try APL on-line. dyalog.com/aplx.htm
Graham


1

C# (.NET Core), 112 107 + 18 = 125 bytes

n=>Enumerable.Range(1,n.Length-1).Min(i=>System.Math.Abs(int.Parse(n.Remove(i))-int.Parse(n.Substring(i))))

Try it online!

The count includes the 18 bytes in using System.Linq;. Takes input as a string.

  • 5 bytes saved by Caius Jard!

string.Remove might save you a few bytes
Caius Jard

1

Common Lisp, 131 bytes

My first time participating in code golf and I decided to use Lisp, since I like it.

Here is my solution:

(defun f (s) (loop for i from 1 below (length s) minimizing (abs (- (parse-integer (subseq s 0 i)) (parse-integer (subseq s i))))))

Input needs to be a string, not integer or list.


3
Welcome to PPCG! Unfortunately I don't know Lisp, but I noticed that you can shorten this by 11 bytes if you make it an unnamed function and remove some whitespace, see here. If you haven't seen this, maybe you'll find some tips.
ბიმო
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.