Biorąc pod uwagę nieujemną liczbę całkowitą, zwraca bezwzględną różnicę między sumą cyfr parzystych a sumą cyfr nieparzystych.
Domyślne zasady
Obowiązują standardowe luki.
Możesz pobierać dane wejściowe i dostarczać dane wyjściowe dowolną standardową metodą wejścia / wyjścia.
Możesz wziąć dane wejściowe jako ciąg znaków, jako liczbę całkowitą lub jako listę cyfr.
To jest golf golfowy , więc wygrywa najkrótszy kod w bajtach w każdym języku !
Przypadki testowe
Wejście ~> Wyjście 0 ~> 0 (| 0-0 | = 0) 1 ~> 1 (| 1-0 | = 1) 12 ~> 1 (| 2-1 | = 1) 333 ~> 9 (| 0- (3 + 3 + 3) | = 9) 459 ~> 10 (| 4- (5 + 9) | = 10) 2469 ~> 3 (| (2 + 4 + 6) -9 | = 3) 1234 ~> 2 (| (2 + 4) - (1 + 3) | = 2)
code-golf
number
number-theory
Pan Xcoder
źródło
źródło
Odpowiedzi:
Galaretka , 6 bajtów
Wypróbuj online!
Jak to działa
źródło
Raise -1 to A's digits, yielding 1 for even digits and 0 for odd ones.
hmm, myślę, że coś pomieszałeś, czy coś ...Skrypty SHENZHEN I / O MCxxxx, 197 (126 + 71) bajtów
Chip 1 (MC6000):
Chip 2 (MC4000):
źródło
<!-- -->
skomentować kod bezpośrednio po liście zamiast tekstu wypełniającego. Lub wciąć kod o 4 dodatkowe spacje).Python 2, 39 bajtów
Pobiera liczbę całkowitą jako listę. Wypróbuj online
-3 bajty dzięki @ Mr.Xcoder
-1 bajtów dzięki @ovs
źródło
[i,-i][i%2]
zamiasti%2and i or -i
do 40 bajtów .(-1)**i*i
za 39 bajtówTI-Basic,
189 bajtówWyjaśnienie
Mnoży każdą cyfrę na liście przez -1 do jej potęgi, negując każdą cyfrę nieparzystą, przed ich zsumowaniem.
źródło
C (gcc) ,
595857 bajtówWypróbuj online!
źródło
R,
3029 bajtówd = scan()
pobiera numer wejściowy po jednej cyfrze po drugiej.-1 bajt dzięki @Giuseppe!
źródło
abs(sum((d=scan())-2*d*d%%2))
C #, 57 bajtów
Pobiera dane wejściowe jako
i
i sumuje liczby całkowite, zmieniając szanse na ujemne.źródło
namespace System.Linq{
i wykonać rzeczywistą funkcję. Zobacz inne odpowiedzi w języku C # w celach informacyjnychMain
. Only reason I thought I'd answer with it. Oh wells.Mathematica, 20 bytes
takes as input a list of digits
special thanx to @LLlAMnYP for letting me know about the "new rules"
źródło
*
.Japt, 8 bytes
Test it online!
Explanation
źródło
Neim, 7 bytes
Explanation:
źródło
APL, 8 bytes
Try it online!
How?
¯1*⊢
- -1n forn
in⍵
[
4 5 9
→1 ¯1 ¯1
]⊢+.×
- verctorized multiplication witho
, then sum[
+/ 4 5 9 × 1 ¯1 ¯1
→+/ 4 ¯5 ¯9
→¯10
]|
- absolute valueźródło
|⊢+.ׯ1*⊢
with the new input spec.JavaScript (ES6),
4338 bytesTakes input as
a stringan array of digits.Test cases
Show code snippet
źródło
EDIT: A more golf-centered approach:
EXCEL,
423629 bytesSaved 6 bytes thanks to Magic Octopus Urn Saved 7 bytes by using Dennis' -1^ approach (which, I just learned, works on arrays in excel)
Takes a list of integers in A column for input. Probably can be golfed further, or by using the string version, taking a string in A1 for input.
EXCEL, 256 bytes
źródło
You may take input as a String, as an Integer or as a list of digits.
your 42 byte answer should be the answer you use.Julia 0.5, 19 bytes
Try it online!
źródło
Husk, 7 bytes
Try it online!
Takes a list of digits as input.
Still missing an "abs" builtin, but a good result all the same :)
Explanation
Ṡ!¡_
is a function that takes a numbern
and then appliesn-1
times the function_
(negation) ton
. This results inn
for oddn
or-n
for evenn
.ṁ
applies a function to each element of a list and sums the results.≠0
returns the absolute difference between a number and 0.źródło
05AB1E, 6 bytes
Thanks to Dennis for the -1 power trick. Takes input as a list of digits
Try it online!
Explanation
źródło
È2*<*O
like a filthy casual.PHP, 51 bytes
adds digit to
$s
if odd, subtracts if even. Run as pipe with-nR
.or
using Dennis´
-1
power trick.źródło
Mathematica, 67 bytes
źródło
PHP, 54 bytes
Try it online!
PHP, 57 bytes
store the even and odd sums in an array
Try it online!
PHP, 57 bytes
store the even and odd sums in two variables
Try it online!
źródło
${1}
and even sum in${0}
:while(~$n=$argn[$i++])${$n&1}+=$n;echo abs(${1}-${0});
for(;~$n=$argn[$i++];$s+=$n)$u+=($n&1)*$n;echo abs($s-2*$u);
andfor(;~$n=$argn[$i++];)$u+=(($n&1)-.5)*2*$n;echo abs($u);
is an interesting wayHaskell,
474239382625 bytes-1 thanks to nimi
-12 thanks to Bruce
-1 thanks to xnor
Try it online!
źródło
s
:((*)=<<((-1)^))
.(\x->x*(-1)^x)
.Perl 6, 28 bytes
Try it online!
Takes a list of digits as input.
$_
is the input argument..map(* % 2 * 2 - 1)
maps each digit to either1
or-1
depending on whether the digit is odd or even, respectively.Z*
zips the original list of digits with the even/odd list using multiplication.źródło
Braingolf, 18 bytes
Try it online!
Takes input as a list of digits
Explanation
źródło
R,
7243 bytesFirst,
d = scan()
takes the number as input, one digit after the other (thanks to @Giuseppe comment !)Then,
b = d %% 2 <1
associates tob
aTRUE
orFALSE
value at each index depending on the digits' parity. Therefore,b
values areTRUE
for even numbers, and!b
areTRUE
for odd values.Finaly,
abs(sum(d[b]) - sum(d[!b]))
does the job.źródło
<1
is one byte shorter than==0
, but note that you may take input as a list of digits as well.Bash
14113999 BytesTry it online!
źródło
Java (OpenJDK 8), 55 bytes
Try it online!
Naive implementation.
źródło
C#, 67 bytes
źródło
05AB1E, 7 bytes
Try it online!
źródło
x86-64 Machine Code, 30 bytes
The above code defines a function that accepts a list/array of integer digits and returns the absolute difference between the sum of its even digits and the sum of its odd digits.
As in C, assembly language doesn't implement lists or arrays as first-class types, but rather represents them as a combination of a pointer and a length. Therefore, I have arranged for this function to accept two parameters: the first is a pointer to the beginning of the list of digits, and the second is an integer specifying the total length of the list (total number of digits, one-indexed).
The function conforms to the System V AMD64 calling convention, which is standard on Gnu/UNIX systems. In particular, the first parameter (pointer to the beginning of the list) is passed in
RDI
(as this is 64-bit code, it is a 64-bit pointer), and the second parameter (length of the list) is passed inESI
(this is only a 32-bit value, because that's more than enough digits to play with, and naturally it is assumed to be non-zero). The result is returned in theEAX
register.If it's any clearer, this would be the C prototype (and you can use this to call the function from C):
Ungolfed assembly mnemonics:
Here's a brief walk-through of the code:
EAX
andEDX
registers, which will be used to hold the sum totals of even and odd digits. TheEAX
register is cleared byXOR
ing it with itself (2 bytes), and then theEDX
register is cleared by sign-extending the EAX into it (CDQ
, 1 byte).Then, we go into the loop that iterates through all of the digits passed in the array. It retrieves a digit, tests to see if it is even or odd (by testing the least-significant bit, which will be 0 if the value is even or 1 if it is odd), and then jumps or falls through accordingly, adding that value to the appropriate accumulator. At the bottom of the loop, we decrement the digit counter (
ESI
) and continue looping as long as it is non-zero (i.e., as long as there are more digits left in the list to be retrieved).The only thing tricky here is the initial MOV instruction, which uses the most complex addressing mode possible on x86.* It takes
RDI
as the base register (the pointer to the beginning of the list), scalesRSI
(the length counter, which serves as the index) by 4 (the size of an integer, in bytes) and adds that to the base, and then subtracts 4 from the total (because the length counter is one-based and we need the offset to be zero-based). This gives the address of the digit in the array, which is then loaded into theECX
register.After the loop has finished, we do the subtraction of the odds from the evens (
EAX -= EDX
).Finally, we compute the absolute value using a common trick—the same one used by most C compilers for the
abs
function. I won't go into details about how this trick works here; see code comments for hints, or do a web search.__
* The code can be re-written to use simpler addressing modes, but it doesn't make it any shorter. I was able to come up with an alternative implementation that dereferenced
RDI
and incremented it by 8 each time through the loop, but because you still have to decrement the counter inESI
, this turned out to be the same 30 bytes. What had initially given me hope is thatadd eax, DWORD PTR [rdi]
is only 2 bytes, the same as adding two enregistered values. Here is that implementation, if only to save anyone attempting to outgolf me some effort :-)źródło
TI-BASIC,
116 bytesTakes input as a list.
i²^Ans
saves two bytes over(-1)^Ans
because we don't need the parentheses.źródło
J, 14 bytes
Try it online!
explanation
źródło