g | x | w | all
Bytes Lang Time Link
170Pascal250907T071545ZKai Burg
024Common Lisp REPL170501T223658Zuser6516
024Python 3170503T173109Ztpvascon
011Perl 6160208T213751ZBrad Gil
040Common Lisp160208T141430Zcoredump
057𝔼𝕊𝕄𝕚𝕟160207T235608ZMama Fun
011Mathematica160207T181638ZLegionMa
00405AB1E160207T120453ZAdnan
005K5160207T160535ZJohnE
014Haskell160207T155541Znimi
003MATL160207T133331ZLuis Men
036Javascript160207T144332Zremoved
005Pyth160207T134755ZPurkkaKo
006Pyth160207T125755ZBlue
030Python 2160207T124151Zorlp
039ES6160207T122122ZNeil
002Jelly160207T115856Zlynn
006CJam160207T114313ZMartin E

Pascal, 170 B

This task is simply finding the difference between the sums of two lists. A binary function taking conformant‐array parameters can evaluate it as follows.

type R=real;Z=integer;function
f(a:array[m..n:Z]of R;d:array[p..q:Z]of R):R;var x,y:R;i:Z;begin
x:=0;y:=0;for i:=m to n do x:=x+a[i];for i:=p to q do y:=y+d[i];f:=x-y end

Prettified:

    function electricCurrentAtNodeDifference(
            electricCurrentInbound:
            array[inboundMinimum‥inboundMaximum: integer] of real;
            electricCurrentOutbound:
            array[outboundMinimum‥outboundMaximum: integer] of real
        ): real;
        var
            electricCurrentInboundSum, electricCurrentOutboundSum: real;
            i: integer;
        begin
            electricCurrentInboundSum  ≔ 0.0; { A }
            electricCurrentOutboundSum ≔ 0.0; { A }
            
            for i ≔ inboundMinimum to inboundMaximum do
            begin
                electricCurrentInboundSum ≔ electricCurrentInboundSum +
                                            electricCurrentInbound[i]
            end;
            
            for i ≔ outboundMinimum to outboundMaximum do
            begin
                electricCurrentOutboundSum ≔ electricCurrentOutboundSum +
                                             electricCurrentOutbound[i]
            end;
            
            electricCurrentAtNodeDifference ≔ electricCurrentInboundSum −
                                              electricCurrentOutboundSum
        end

Common Lisp REPL, SBCL 28 24 bytes

write this into REPL:

#.`(-(+ #1=,@(read))#1#)

then write input lists like this:

(2 3 4)
(2 3)

I hope it's okay to use such list format (instead of e.g. '(2 3 4)) I used coredump's answer as formula for my solution and then achieved his calculation effect in a different way.

Explanation

Let e_1,...,e_n be elements of the first list and f_1,...,f_{n-1} be elements of second list. We want to evaluate expression (-(+ e_1 e_2 ... e_n)f_1 f_2 ...f_{n-1}) It would mean subtracting elements of second list from sum of elements of first list. The needed expression is constructed like so:

backqoute stops evaluation

#1= saves a bit of writting, remembering ,@(read)

,@ stops effects of backquote (so that (read) will be evaluated) and takes elements out of a list.

(read) asks for input

#1# "loads" Lisp object saved by #1=

#. does evaluation of printed representation of an Lisp object

Python 3, 24 bytes

lambda a,b:sum(a)-sum(b)

or

Python 2, 19 bytes

print sum(a)-sum(b)

depending if I am required to print the result or just create a function that returns it.

Perl 6, 11 bytes

*.sum-*.sum

Usage:

# give it a lexical name
my &code = *.sum-*.sum;

say code [1, 2, 3], [1, 2]; # 3
say code [4, 5, 6], [7, 8]; # 0
say code [5, 7, 3, 4, 5, 2], [8, 4, 5, 2, 1]; # 6

Common Lisp, 40

(lambda(x y)(-(reduce'+ x)(reduce'+ y)))

𝔼𝕊𝕄𝕚𝕟, 5 chars / 7 bytes

⨭î-⨭í

Try it here (Firefox only).

Wut.

Explanation

sum(input1) - sum(input2)

Mathematica, 17 11 bytes

Tr@#-Tr@#2&

Quite simple.

05AB1E, 4 bytes

Code:

OEO-

Explanation:

O     # Take the sum of the input list
 E    # Evaluate input
  O   # Take the sum of the input list
   -  # Substract from each other

Thanks to Luis Mendo for reminding me that I need to implement a concatenate function. If I've had implemented it sooner, it would have been 3 bytes:

Non-competing version (3 bytes):

The first list is the leaving current list, the second is the entering current list. Code:

(«O

Explanation:

(    # Negate the list, e.g. [3, 4, 5] would become [-3, -4, -5]
 «   # Concatenate the second list to the first
  O  # Take the sum and implicitly output it

Uses CP-1252 encoding.

K5, 5 bytes

-/+/'

Difference over (-/) sum over (+/) each (').

In action:

  (-/+/')'((1 2 3;1 2);(4 5 6;7 8);(5 7 3 4 5 2;8 4 5 2 1))
3 0 6

Haskell, 14 bytes

(.sum).(-).sum

Usage example: ( (.sum).(-).sum ) [5,7,3,4,5,2] [8,4,5,2,1] -> 6.

Sum each list and take the difference.

MATL, 3 4.0 bytes

_hs

Inputs are: leaving currents first, then entering currents.

Try it online!

_     % implicitly input array with leaving currents (except one). Negate
h     % implicitly input array with entering currents. Concatenate  
s     % sum of all elements in concatenated array

Javascript, 36 bytes

(a,b)=>eval(a.join`+`+'-'+b.join`-`)

f=
(a,b)=>
    eval(
        a.join`+`
        +'-'+
        b.join`-`
    )

document.body.innerHTML = '<pre>' +
  'f([1,2,3],[1,2]) = ' + f([1,2,3],[1,2]) + '\n' +
  'f([4,5,6],[7,8]) = ' + f([4,5,6],[7,8]) + '\n' +
  'f([5,7,3,4,5,2],[8,4,5,2,1]) = ' + f([5,7,3,4,5,2],[8,4,5,2,1]) + '\n' +
'</pre>'

Pyth, 5 bytes

-FsMQ

Try it online. Test suite.

Map sum on both input lists, then Fold subtraction (-).

This could also be written as -sQsE, which takes the lists on two lines.

Pyth, 6 bytes

-.*sRQ

Explanation

       - autoassign Q = eval(input())
   sRQ - map(sum, Q)
-.*    - imp_print(minus(*^))

Try it here

Python 2, 30 bytes

a,b=map(sum,input());print a-b

ES6, 39 bytes

(i,o)=>i.reduceRight((r,a,j)=>r+a-o[j])

Because I wanted to use reduceRight.

Jelly, 2 bytes

_S

Try it here!

Takes the entering currents in the first argument, and the leaving currents in the second argument. _ subtracts them pairwise, leaving the single element from the longer list as-is, and S sums the result.

CJam, 8 6 bytes

q~.-:+

Input uses two CJam-style arrays.

Run all test cases. (This reads multiple test cases at once and includes a framework to process each line individually, discarding the expected result from the input.)

Explanation

q~  e# Read and evaluate input.
.-  e# Elementwise difference.
:+  e# Get sum.

.- works reliably because we're guaranteed that the first list is always longer than the second. (Otherwise, the extraneous elements of the second list would be appended to the result which would add them to the sum instead of subtracting them.)