| Bytes | Lang | Time | Link |
|---|---|---|---|
| 170 | Pascal | 250907T071545Z | Kai Burg |
| 024 | Common Lisp REPL | 170501T223658Z | user6516 |
| 024 | Python 3 | 170503T173109Z | tpvascon |
| 011 | Perl 6 | 160208T213751Z | Brad Gil |
| 040 | Common Lisp | 160208T141430Z | coredump |
| 057 | 𝔼𝕊𝕄𝕚𝕟 | 160207T235608Z | Mama Fun |
| 011 | Mathematica | 160207T181638Z | LegionMa |
| 004 | 05AB1E | 160207T120453Z | Adnan |
| 005 | K5 | 160207T160535Z | JohnE |
| 014 | Haskell | 160207T155541Z | nimi |
| 003 | MATL | 160207T133331Z | Luis Men |
| 036 | Javascript | 160207T144332Z | removed |
| 005 | Pyth | 160207T134755Z | PurkkaKo |
| 006 | Pyth | 160207T125755Z | Blue |
| 030 | Python 2 | 160207T124151Z | orlp |
| 039 | ES6 | 160207T122122Z | Neil |
| 002 | Jelly | 160207T115856Z | lynn |
| 006 | CJam | 160207T114313Z | Martin 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
⨭î-⨭í
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.
_ % 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
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(*^))
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
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.)