Parallel accounts (Day 2)
$begingroup$
Challenge Taken with permission from my University Code Challenge Contest
After finishing her studies a couple of months ago, Marie opened a bank account to start receiving the payment of her first job in town. Since then she has been performing a few transactions with it. Her first payment was $1000 dollars. With that money she paid for a dinner in which she invited her parents (The dinner cost $150 dollars), then, she did a purchase in a well-known supermarket ($80 dollars) and a hotel reservation for her vacations ($200). At the end of the month she received her payment again (1040 dollars, a little more than the previous month) and the day after she spent another $70 dollars at the supermarket.
Today, she realized that if after paying the first $80 dollars in the supermarket a second account had been created and the first one frozen, both accounts would have exactly the same balance:
$$ underbrace{1000quad -150quad -80}_{Total=770}quad underbrace{-200quad 1040quad -70}_{Total=770} $$
The event was so rare to her that she wants to continue ascertaining if the movements of her account and those of her friends have also this feature or not.
Challenge
Given a list of transactions, output the number of instants of time in which the owner of the bank account could have created a second account so that both had the same final balance.
Example: [1000, -150, -80, -200, 1040, -70]
$$ color{red}{1)quadunderbrace{}_{Total=0}quad underbrace{1000quad -150quad -80quad -200quad 1040quad -70}_{Total=1540}} $$
$$ color{red}{2)quadunderbrace{1000}_{Total=1000}quad underbrace{-150quad -80quad -200quad 1040quad -70}_{Total=540}} $$
$$ color{red}{3)quadunderbrace{1000quad -150}_{Total=850}quad underbrace{-80quad -200quad 1040quad -70}_{Total=690}} $$
$$ color{green}{4)quadunderbrace{1000quad -150quad -80}_{Total=770}quad underbrace{-200quad 1040quad -70}_{Total=770}} $$
$$ color{red}{5)quadunderbrace{1000quad -150quad -80quad-200}_{Total=570}quad underbrace{ 1040quad -70}_{Total=970}} $$
$$ color{red}{6)quadunderbrace{1000quad -150quad -80quad -200quad 1040}_{Total=1610}quad underbrace{-70}_{Total=-70}} $$
$$ color{red}{7)quadunderbrace{1000quad -150quad -80quad-200quad 1040quad -70}_{Total=1540}quad underbrace{}_{Total=0}} $$
Test Case
- Input:
1000 -150 -80 -200 1040 -70
Output:1
- Input:
100 -100
Output:2
- Input:
1 2 3
Output:1
- Input:
10 -20 15
Output:0
- Input:
15 -15 15 -15
Output:3
- Input:
1
Output:0
Notes
- You can assume there wont be any transaction of $0 dollars
- You can take input in any reasonable way
code-golf
$endgroup$
add a comment |
$begingroup$
Challenge Taken with permission from my University Code Challenge Contest
After finishing her studies a couple of months ago, Marie opened a bank account to start receiving the payment of her first job in town. Since then she has been performing a few transactions with it. Her first payment was $1000 dollars. With that money she paid for a dinner in which she invited her parents (The dinner cost $150 dollars), then, she did a purchase in a well-known supermarket ($80 dollars) and a hotel reservation for her vacations ($200). At the end of the month she received her payment again (1040 dollars, a little more than the previous month) and the day after she spent another $70 dollars at the supermarket.
Today, she realized that if after paying the first $80 dollars in the supermarket a second account had been created and the first one frozen, both accounts would have exactly the same balance:
$$ underbrace{1000quad -150quad -80}_{Total=770}quad underbrace{-200quad 1040quad -70}_{Total=770} $$
The event was so rare to her that she wants to continue ascertaining if the movements of her account and those of her friends have also this feature or not.
Challenge
Given a list of transactions, output the number of instants of time in which the owner of the bank account could have created a second account so that both had the same final balance.
Example: [1000, -150, -80, -200, 1040, -70]
$$ color{red}{1)quadunderbrace{}_{Total=0}quad underbrace{1000quad -150quad -80quad -200quad 1040quad -70}_{Total=1540}} $$
$$ color{red}{2)quadunderbrace{1000}_{Total=1000}quad underbrace{-150quad -80quad -200quad 1040quad -70}_{Total=540}} $$
$$ color{red}{3)quadunderbrace{1000quad -150}_{Total=850}quad underbrace{-80quad -200quad 1040quad -70}_{Total=690}} $$
$$ color{green}{4)quadunderbrace{1000quad -150quad -80}_{Total=770}quad underbrace{-200quad 1040quad -70}_{Total=770}} $$
$$ color{red}{5)quadunderbrace{1000quad -150quad -80quad-200}_{Total=570}quad underbrace{ 1040quad -70}_{Total=970}} $$
$$ color{red}{6)quadunderbrace{1000quad -150quad -80quad -200quad 1040}_{Total=1610}quad underbrace{-70}_{Total=-70}} $$
$$ color{red}{7)quadunderbrace{1000quad -150quad -80quad-200quad 1040quad -70}_{Total=1540}quad underbrace{}_{Total=0}} $$
Test Case
- Input:
1000 -150 -80 -200 1040 -70
Output:1
- Input:
100 -100
Output:2
- Input:
1 2 3
Output:1
- Input:
10 -20 15
Output:0
- Input:
15 -15 15 -15
Output:3
- Input:
1
Output:0
Notes
- You can assume there wont be any transaction of $0 dollars
- You can take input in any reasonable way
code-golf
$endgroup$
11
$begingroup$
After 6 months of frozen and newly created accounts, it is reported that Marie's banker is now interned in a sanitarium. "We are your friends. You need some rest", they said.
$endgroup$
– Arnauld
Feb 7 at 17:20
2
$begingroup$
Suggested test case of a single transaction
$endgroup$
– Veskah
Feb 8 at 4:53
add a comment |
$begingroup$
Challenge Taken with permission from my University Code Challenge Contest
After finishing her studies a couple of months ago, Marie opened a bank account to start receiving the payment of her first job in town. Since then she has been performing a few transactions with it. Her first payment was $1000 dollars. With that money she paid for a dinner in which she invited her parents (The dinner cost $150 dollars), then, she did a purchase in a well-known supermarket ($80 dollars) and a hotel reservation for her vacations ($200). At the end of the month she received her payment again (1040 dollars, a little more than the previous month) and the day after she spent another $70 dollars at the supermarket.
Today, she realized that if after paying the first $80 dollars in the supermarket a second account had been created and the first one frozen, both accounts would have exactly the same balance:
$$ underbrace{1000quad -150quad -80}_{Total=770}quad underbrace{-200quad 1040quad -70}_{Total=770} $$
The event was so rare to her that she wants to continue ascertaining if the movements of her account and those of her friends have also this feature or not.
Challenge
Given a list of transactions, output the number of instants of time in which the owner of the bank account could have created a second account so that both had the same final balance.
Example: [1000, -150, -80, -200, 1040, -70]
$$ color{red}{1)quadunderbrace{}_{Total=0}quad underbrace{1000quad -150quad -80quad -200quad 1040quad -70}_{Total=1540}} $$
$$ color{red}{2)quadunderbrace{1000}_{Total=1000}quad underbrace{-150quad -80quad -200quad 1040quad -70}_{Total=540}} $$
$$ color{red}{3)quadunderbrace{1000quad -150}_{Total=850}quad underbrace{-80quad -200quad 1040quad -70}_{Total=690}} $$
$$ color{green}{4)quadunderbrace{1000quad -150quad -80}_{Total=770}quad underbrace{-200quad 1040quad -70}_{Total=770}} $$
$$ color{red}{5)quadunderbrace{1000quad -150quad -80quad-200}_{Total=570}quad underbrace{ 1040quad -70}_{Total=970}} $$
$$ color{red}{6)quadunderbrace{1000quad -150quad -80quad -200quad 1040}_{Total=1610}quad underbrace{-70}_{Total=-70}} $$
$$ color{red}{7)quadunderbrace{1000quad -150quad -80quad-200quad 1040quad -70}_{Total=1540}quad underbrace{}_{Total=0}} $$
Test Case
- Input:
1000 -150 -80 -200 1040 -70
Output:1
- Input:
100 -100
Output:2
- Input:
1 2 3
Output:1
- Input:
10 -20 15
Output:0
- Input:
15 -15 15 -15
Output:3
- Input:
1
Output:0
Notes
- You can assume there wont be any transaction of $0 dollars
- You can take input in any reasonable way
code-golf
$endgroup$
Challenge Taken with permission from my University Code Challenge Contest
After finishing her studies a couple of months ago, Marie opened a bank account to start receiving the payment of her first job in town. Since then she has been performing a few transactions with it. Her first payment was $1000 dollars. With that money she paid for a dinner in which she invited her parents (The dinner cost $150 dollars), then, she did a purchase in a well-known supermarket ($80 dollars) and a hotel reservation for her vacations ($200). At the end of the month she received her payment again (1040 dollars, a little more than the previous month) and the day after she spent another $70 dollars at the supermarket.
Today, she realized that if after paying the first $80 dollars in the supermarket a second account had been created and the first one frozen, both accounts would have exactly the same balance:
$$ underbrace{1000quad -150quad -80}_{Total=770}quad underbrace{-200quad 1040quad -70}_{Total=770} $$
The event was so rare to her that she wants to continue ascertaining if the movements of her account and those of her friends have also this feature or not.
Challenge
Given a list of transactions, output the number of instants of time in which the owner of the bank account could have created a second account so that both had the same final balance.
Example: [1000, -150, -80, -200, 1040, -70]
$$ color{red}{1)quadunderbrace{}_{Total=0}quad underbrace{1000quad -150quad -80quad -200quad 1040quad -70}_{Total=1540}} $$
$$ color{red}{2)quadunderbrace{1000}_{Total=1000}quad underbrace{-150quad -80quad -200quad 1040quad -70}_{Total=540}} $$
$$ color{red}{3)quadunderbrace{1000quad -150}_{Total=850}quad underbrace{-80quad -200quad 1040quad -70}_{Total=690}} $$
$$ color{green}{4)quadunderbrace{1000quad -150quad -80}_{Total=770}quad underbrace{-200quad 1040quad -70}_{Total=770}} $$
$$ color{red}{5)quadunderbrace{1000quad -150quad -80quad-200}_{Total=570}quad underbrace{ 1040quad -70}_{Total=970}} $$
$$ color{red}{6)quadunderbrace{1000quad -150quad -80quad -200quad 1040}_{Total=1610}quad underbrace{-70}_{Total=-70}} $$
$$ color{red}{7)quadunderbrace{1000quad -150quad -80quad-200quad 1040quad -70}_{Total=1540}quad underbrace{}_{Total=0}} $$
Test Case
- Input:
1000 -150 -80 -200 1040 -70
Output:1
- Input:
100 -100
Output:2
- Input:
1 2 3
Output:1
- Input:
10 -20 15
Output:0
- Input:
15 -15 15 -15
Output:3
- Input:
1
Output:0
Notes
- You can assume there wont be any transaction of $0 dollars
- You can take input in any reasonable way
code-golf
code-golf
edited Feb 8 at 13:14
Luis felipe De jesus Munoz
asked Feb 7 at 14:45
Luis felipe De jesus MunozLuis felipe De jesus Munoz
4,69721464
4,69721464
11
$begingroup$
After 6 months of frozen and newly created accounts, it is reported that Marie's banker is now interned in a sanitarium. "We are your friends. You need some rest", they said.
$endgroup$
– Arnauld
Feb 7 at 17:20
2
$begingroup$
Suggested test case of a single transaction
$endgroup$
– Veskah
Feb 8 at 4:53
add a comment |
11
$begingroup$
After 6 months of frozen and newly created accounts, it is reported that Marie's banker is now interned in a sanitarium. "We are your friends. You need some rest", they said.
$endgroup$
– Arnauld
Feb 7 at 17:20
2
$begingroup$
Suggested test case of a single transaction
$endgroup$
– Veskah
Feb 8 at 4:53
11
11
$begingroup$
After 6 months of frozen and newly created accounts, it is reported that Marie's banker is now interned in a sanitarium. "We are your friends. You need some rest", they said.
$endgroup$
– Arnauld
Feb 7 at 17:20
$begingroup$
After 6 months of frozen and newly created accounts, it is reported that Marie's banker is now interned in a sanitarium. "We are your friends. You need some rest", they said.
$endgroup$
– Arnauld
Feb 7 at 17:20
2
2
$begingroup$
Suggested test case of a single transaction
$endgroup$
– Veskah
Feb 8 at 4:53
$begingroup$
Suggested test case of a single transaction
$endgroup$
– Veskah
Feb 8 at 4:53
add a comment |
20 Answers
20
active
oldest
votes
$begingroup$
C# (Visual C# Interactive Compiler), 63 bytes
n=>n.Append(0).Where((a,b)=>n.Take(b).Sum()*2==n.Sum()).Count()
Saved 6 bytes thanks to dana
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 6, 25 bytes
{+grep .sum/2,[+] 0,|$_}
Try it online!
Explanation
We just prepend a zero to the given list (0,|$_
), make a sequence of partial sums with [+]
(i. e. the sequence formed by the first element, the sum of first two, the sum of first three etc.), and look (grep
) for any elements that are exactly equal to the half of the final account state (sum of the given list). Finally, we count them with a +
.
$endgroup$
add a comment |
$begingroup$
05AB1E, 11 bytes
0.ø.œ2ùO€ËO
Try it online or verify all test cases.
Explanation:
0.ø # Surround the (implicit) input list with a leading and trailing 0
# i.e. [100,-100] → [0,100,-100,0]
.œ # Get all possible partitions to divide the list
# → [[[0],[100],[-100],[0]],[[0],[100],[-100,0]],[[0],[100,-100],[0]],[[0],[100,-100,0]],[[0,100],[-100],[0]],[[0,100],[-100,0]],[[0,100,-100],[0]],[[0,100,-100,0]]]
2ù # Only leave partitions consisting of 2 items
# → [[[0],[100,-100,0]],[[0,100],[-100,0]],[[0,100,-100],[0]]]
O # Take the sum of each
# → [[0,0],[100,-100],[0,0]]
€Ë # Check of each inner list if both sums are equal (1 if truthy; 0 if falsey)
# → [1,0,1]
O # Take the sum of that (and output as result)
# → 2
$endgroup$
add a comment |
$begingroup$
Jelly, 11 6 bytes
ŻÄḤ=SS
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (Node.js), 45 bytes
a=>!a.map(v=>o[s+=v]=-~o[s],s=0,o=[1])|o[s/2]
Try it online!
Save 4 bytes by using -~o[s]
. Thanks to Shaggy.
$endgroup$
$begingroup$
+1 for beating Arnauld :o
$endgroup$
– Luis felipe De jesus Munoz
Feb 8 at 12:22
$begingroup$
45 bytes
$endgroup$
– Shaggy
Feb 8 at 19:14
$begingroup$
@LuisfelipeDejesusMunoz, Arnauld ain't (always) unbeatable! ;)
$endgroup$
– Shaggy
Feb 8 at 23:45
$begingroup$
@Shaggy leading+
is changed to!
, so it could work for input[100]
.
$endgroup$
– tsh
Feb 9 at 12:00
$begingroup$
Ah, didn't realise we had to handle singleton arrays. Nicely fixed.
$endgroup$
– Shaggy
Feb 9 at 13:46
|
show 1 more comment
$begingroup$
Perl 5 -p
, 42 41 bytes
@NahuelFouilleul saves a byte
y/ /+/;$+=eval$'==eval$`while/^|$|+/g}{
Try it online!
$endgroup$
$begingroup$
y/ /+/;
saves 1 byte
$endgroup$
– Nahuel Fouilleul
Feb 7 at 16:56
$begingroup$
34 bytes using other approach
$endgroup$
– Nahuel Fouilleul
Feb 7 at 19:57
$begingroup$
30 bytes
$endgroup$
– Nahuel Fouilleul
Feb 7 at 20:03
add a comment |
$begingroup$
JavaScript (ES6), 52 bytes
a=>a.map(x=>n+=(s+=x)==eval(a.join`+`)-s,n=s=0)|n+!s
Try it online!
Commented
a => // a = input array
a.map(x => // for each element x in a:
n += // increment n if the following test is truthy:
(s += x) // update the left sum
== // and test whether it's equal to
eval(a.join`+`) - s, // the right sum
n = s =0 // start with n = s = 0
) // end of map()
| n // yield n; if the final sum is 0, it means that we could have
+ // created a balanced account at the beginning of the process;
!s // so, we increment n if it is
Recursive version, 54 53 bytes
f=(a,s=0)=>a+a?(s==eval(a.join`+`))+f(a,s+a.pop()):!s
Try it online!
$endgroup$
$begingroup$
I was just about to suggest that 52-byte version!
$endgroup$
– Shaggy
Feb 7 at 16:00
$begingroup$
@Shaggy Yeah, I discarded the non-recursive version too soon because I thought the recursive one could be shorter.
$endgroup$
– Arnauld
Feb 7 at 16:03
add a comment |
$begingroup$
APL (Dyalog Unicode), 21 bytesSBCS
Anonymous tacit prefix function
+/⊂((+/↑)=1⊥↓)¨⍨0,⍳∘≢
Try it online!
⍳
ɩndices
∘
of≢
the tally of transactions
0,
prepend zero
⊂(
…)¨⍨
apply the following tacit function with each of those as left argument and the entire list of transactions as right argument (⍨
swaps argument
⊂
the entire list of transactions
(
…)
as left argument to the below function
¨
applied to each of the indices
⍨
with swapped arguments (i.e. list on right, indices on left:
↓
drop that many from the left
1⊥
sum (lit. evaluate in base-1)
(
…)=
is it (0/1) equal to…
↑
take that many transactions from the left
+/
sum them
+/
sum that Boolean list to get the count of truths
$endgroup$
add a comment |
$begingroup$
Batch, 84 bytes
@set s=%*
@set/as=%s: =+%,c=0
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
@echo %c%
Takes input as command-line arguments. Explanation:
@set s=%*
Join the arguments with spaces.
@set/as=%s: =+%,c=0
Replace the spaces with +
s and evaluate the result. Also clear the count.
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
For each amount, subtract double that from the sum. If the result is zero, then this is a valid match, so increment the count. The extra zero at the beginning allows for a match before any amounts.
@echo %c%
Print the result.
$endgroup$
add a comment |
$begingroup$
Charcoal, 15 bytes
⊞θ⁰IΣEθ⁼Σθ⊗Σ✂θκ
Try it online! Link is to verbose version of code. Explanation:
θ Input list
⁰ Literal 0
⊞ Push to list
θ Augmented list
E Mapped to
θ Augmented list
✂ Sliced from
κ Current index
Σ Summed
⊗ Doubled
⁼ Equals
θ (Augmented) list
Σ Summed
Σ Sum of booleans
I Cast to string
Implicitly print
Unfortunately in Charcoal Sum()
is not 0
so I have to ensure that there is always at least one element to sum.
$endgroup$
add a comment |
$begingroup$
Python 3, 67 58 bytes
lambda l:sum(sum(l[:x])*2==sum(l)for x in range(len(l)+1))
Try it online!
-9 bytes thanks to @Don't be a x-triple dot
$endgroup$
1
$begingroup$
Summing instead of filtering will save you 7 bytes:lambda l:sum(sum(l[:x])==sum(l[x:])for x in range(len(l)+1))
.
$endgroup$
– Don't be a x-triple dot
Feb 7 at 18:43
$begingroup$
sum(l[:x])*2==sum(l)
saves you another 2 bytes.
$endgroup$
– Neil
Feb 7 at 20:49
add a comment |
$begingroup$
R, 50 37 bytes
sum(c(0,cumsum(x<-scan()))==sum(x)/2)
Try it online!
$endgroup$
add a comment |
$begingroup$
MATL, 9 bytes
s0GhYsE=s
Try it online!
Same approach as some other answers: prepend a zero, and check how often half the cumulative sum is equal to the total sum.
s % Total sum of (implicit) input
0Gh % Prepend 0 to another copy of the input
Ys % Cumulative sum
E= % Check element-wise equality of 2*cumulative sum with total sum
s % Sum number of `true` values
$endgroup$
add a comment |
$begingroup$
Japt -x
, 14 11 bytes
iT å+ ®¥nUx
Try it
iT å+ ®¥nUx :Implicit input of array U
i :Prepend
T : Zero
å+ :Cumulatively reduce by addition
® :Map each Z
¥ : Test for equality with
n : Z subtracted from
Ux : U reduced by addition
:Implicitly reduce by addition and output
$endgroup$
add a comment |
$begingroup$
PowerShell, 88 82 bytes
-6 Bytes thanks to mazzy
param($n)0..($x=$n.length)|%{$i+=+$z-eq($n[$_..$x]|measure -Su).sum;$z+=$n[$_]};$i
Try it online!
This seems like a very clumsy method but it got the job done. I'll try and revamp it in the future.
$endgroup$
1
$begingroup$
you can write$i+=<predicate>
insteadif(<predicate>){$i++}
$endgroup$
– mazzy
Feb 8 at 6:15
add a comment |
$begingroup$
bash, 52 bytes
IFS=+;for i in 0 $@;{((c+=2*(x+=i)=="$*"));};echo $c
TIO
The trick: setting IFS=+
, "$*"
expands to a string where arguments are delimited by +
, in arithmetic expression eval to the sum
$endgroup$
add a comment |
$begingroup$
PowerShell, 49 45 36 bytes
(($args+0|%{2*($s+=$_)})-eq$s).count
Try it online!
$endgroup$
add a comment |
$begingroup$
Brachylog, 9 bytes
Not as good as day 1. This one loses to Jelly
{~c₂+ᵐ=}ᶜ
Explanation
{ }ᶜ # Count the ways to:
~c₂ # Split the input array in 2 ...
+ᵐ # so that their sums ...
= # are equal
Test suite:
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 46 35 bytes
f x=sum[1|a<-scanl(+)0x,a==sum x/2]
Try it online!
$endgroup$
add a comment |
$begingroup$
J, 19 bytes
1#.[:(={:-])0+/@,]
Try it online!
explanation
1 #. [: (= ({: - ])) 0 +/@, ]
0 , ] NB. prepend 0 to input...
+/@ NB. and take the prefix sums...
[: ({: - ]) NB. then subtract that list
NB. from its final elm
NB. (`{:`), giving the list
NB. of suffix sums...
[: (= ( )) NB. create a 1-0 list showing
NB. where the prefix sums
NB. equal the suffix sums
1 #. NB. and take the sum.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179631%2fparallel-accounts-day-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
20 Answers
20
active
oldest
votes
20 Answers
20
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
C# (Visual C# Interactive Compiler), 63 bytes
n=>n.Append(0).Where((a,b)=>n.Take(b).Sum()*2==n.Sum()).Count()
Saved 6 bytes thanks to dana
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 63 bytes
n=>n.Append(0).Where((a,b)=>n.Take(b).Sum()*2==n.Sum()).Count()
Saved 6 bytes thanks to dana
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 63 bytes
n=>n.Append(0).Where((a,b)=>n.Take(b).Sum()*2==n.Sum()).Count()
Saved 6 bytes thanks to dana
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 63 bytes
n=>n.Append(0).Where((a,b)=>n.Take(b).Sum()*2==n.Sum()).Count()
Saved 6 bytes thanks to dana
Try it online!
edited 2 days ago
answered Feb 7 at 20:55
Embodiment of IgnoranceEmbodiment of Ignorance
980119
980119
add a comment |
add a comment |
$begingroup$
Perl 6, 25 bytes
{+grep .sum/2,[+] 0,|$_}
Try it online!
Explanation
We just prepend a zero to the given list (0,|$_
), make a sequence of partial sums with [+]
(i. e. the sequence formed by the first element, the sum of first two, the sum of first three etc.), and look (grep
) for any elements that are exactly equal to the half of the final account state (sum of the given list). Finally, we count them with a +
.
$endgroup$
add a comment |
$begingroup$
Perl 6, 25 bytes
{+grep .sum/2,[+] 0,|$_}
Try it online!
Explanation
We just prepend a zero to the given list (0,|$_
), make a sequence of partial sums with [+]
(i. e. the sequence formed by the first element, the sum of first two, the sum of first three etc.), and look (grep
) for any elements that are exactly equal to the half of the final account state (sum of the given list). Finally, we count them with a +
.
$endgroup$
add a comment |
$begingroup$
Perl 6, 25 bytes
{+grep .sum/2,[+] 0,|$_}
Try it online!
Explanation
We just prepend a zero to the given list (0,|$_
), make a sequence of partial sums with [+]
(i. e. the sequence formed by the first element, the sum of first two, the sum of first three etc.), and look (grep
) for any elements that are exactly equal to the half of the final account state (sum of the given list). Finally, we count them with a +
.
$endgroup$
Perl 6, 25 bytes
{+grep .sum/2,[+] 0,|$_}
Try it online!
Explanation
We just prepend a zero to the given list (0,|$_
), make a sequence of partial sums with [+]
(i. e. the sequence formed by the first element, the sum of first two, the sum of first three etc.), and look (grep
) for any elements that are exactly equal to the half of the final account state (sum of the given list). Finally, we count them with a +
.
answered Feb 7 at 17:19
RamilliesRamillies
1,641515
1,641515
add a comment |
add a comment |
$begingroup$
05AB1E, 11 bytes
0.ø.œ2ùO€ËO
Try it online or verify all test cases.
Explanation:
0.ø # Surround the (implicit) input list with a leading and trailing 0
# i.e. [100,-100] → [0,100,-100,0]
.œ # Get all possible partitions to divide the list
# → [[[0],[100],[-100],[0]],[[0],[100],[-100,0]],[[0],[100,-100],[0]],[[0],[100,-100,0]],[[0,100],[-100],[0]],[[0,100],[-100,0]],[[0,100,-100],[0]],[[0,100,-100,0]]]
2ù # Only leave partitions consisting of 2 items
# → [[[0],[100,-100,0]],[[0,100],[-100,0]],[[0,100,-100],[0]]]
O # Take the sum of each
# → [[0,0],[100,-100],[0,0]]
€Ë # Check of each inner list if both sums are equal (1 if truthy; 0 if falsey)
# → [1,0,1]
O # Take the sum of that (and output as result)
# → 2
$endgroup$
add a comment |
$begingroup$
05AB1E, 11 bytes
0.ø.œ2ùO€ËO
Try it online or verify all test cases.
Explanation:
0.ø # Surround the (implicit) input list with a leading and trailing 0
# i.e. [100,-100] → [0,100,-100,0]
.œ # Get all possible partitions to divide the list
# → [[[0],[100],[-100],[0]],[[0],[100],[-100,0]],[[0],[100,-100],[0]],[[0],[100,-100,0]],[[0,100],[-100],[0]],[[0,100],[-100,0]],[[0,100,-100],[0]],[[0,100,-100,0]]]
2ù # Only leave partitions consisting of 2 items
# → [[[0],[100,-100,0]],[[0,100],[-100,0]],[[0,100,-100],[0]]]
O # Take the sum of each
# → [[0,0],[100,-100],[0,0]]
€Ë # Check of each inner list if both sums are equal (1 if truthy; 0 if falsey)
# → [1,0,1]
O # Take the sum of that (and output as result)
# → 2
$endgroup$
add a comment |
$begingroup$
05AB1E, 11 bytes
0.ø.œ2ùO€ËO
Try it online or verify all test cases.
Explanation:
0.ø # Surround the (implicit) input list with a leading and trailing 0
# i.e. [100,-100] → [0,100,-100,0]
.œ # Get all possible partitions to divide the list
# → [[[0],[100],[-100],[0]],[[0],[100],[-100,0]],[[0],[100,-100],[0]],[[0],[100,-100,0]],[[0,100],[-100],[0]],[[0,100],[-100,0]],[[0,100,-100],[0]],[[0,100,-100,0]]]
2ù # Only leave partitions consisting of 2 items
# → [[[0],[100,-100,0]],[[0,100],[-100,0]],[[0,100,-100],[0]]]
O # Take the sum of each
# → [[0,0],[100,-100],[0,0]]
€Ë # Check of each inner list if both sums are equal (1 if truthy; 0 if falsey)
# → [1,0,1]
O # Take the sum of that (and output as result)
# → 2
$endgroup$
05AB1E, 11 bytes
0.ø.œ2ùO€ËO
Try it online or verify all test cases.
Explanation:
0.ø # Surround the (implicit) input list with a leading and trailing 0
# i.e. [100,-100] → [0,100,-100,0]
.œ # Get all possible partitions to divide the list
# → [[[0],[100],[-100],[0]],[[0],[100],[-100,0]],[[0],[100,-100],[0]],[[0],[100,-100,0]],[[0,100],[-100],[0]],[[0,100],[-100,0]],[[0,100,-100],[0]],[[0,100,-100,0]]]
2ù # Only leave partitions consisting of 2 items
# → [[[0],[100,-100,0]],[[0,100],[-100,0]],[[0,100,-100],[0]]]
O # Take the sum of each
# → [[0,0],[100,-100],[0,0]]
€Ë # Check of each inner list if both sums are equal (1 if truthy; 0 if falsey)
# → [1,0,1]
O # Take the sum of that (and output as result)
# → 2
edited Feb 7 at 15:08
answered Feb 7 at 15:00
Kevin CruijssenKevin Cruijssen
38.1k557195
38.1k557195
add a comment |
add a comment |
$begingroup$
Jelly, 11 6 bytes
ŻÄḤ=SS
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 11 6 bytes
ŻÄḤ=SS
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 11 6 bytes
ŻÄḤ=SS
Try it online!
$endgroup$
Jelly, 11 6 bytes
ŻÄḤ=SS
Try it online!
edited Feb 8 at 21:05
answered Feb 7 at 15:32
Don't be a x-triple dotDon't be a x-triple dot
32k759199
32k759199
add a comment |
add a comment |
$begingroup$
JavaScript (Node.js), 45 bytes
a=>!a.map(v=>o[s+=v]=-~o[s],s=0,o=[1])|o[s/2]
Try it online!
Save 4 bytes by using -~o[s]
. Thanks to Shaggy.
$endgroup$
$begingroup$
+1 for beating Arnauld :o
$endgroup$
– Luis felipe De jesus Munoz
Feb 8 at 12:22
$begingroup$
45 bytes
$endgroup$
– Shaggy
Feb 8 at 19:14
$begingroup$
@LuisfelipeDejesusMunoz, Arnauld ain't (always) unbeatable! ;)
$endgroup$
– Shaggy
Feb 8 at 23:45
$begingroup$
@Shaggy leading+
is changed to!
, so it could work for input[100]
.
$endgroup$
– tsh
Feb 9 at 12:00
$begingroup$
Ah, didn't realise we had to handle singleton arrays. Nicely fixed.
$endgroup$
– Shaggy
Feb 9 at 13:46
|
show 1 more comment
$begingroup$
JavaScript (Node.js), 45 bytes
a=>!a.map(v=>o[s+=v]=-~o[s],s=0,o=[1])|o[s/2]
Try it online!
Save 4 bytes by using -~o[s]
. Thanks to Shaggy.
$endgroup$
$begingroup$
+1 for beating Arnauld :o
$endgroup$
– Luis felipe De jesus Munoz
Feb 8 at 12:22
$begingroup$
45 bytes
$endgroup$
– Shaggy
Feb 8 at 19:14
$begingroup$
@LuisfelipeDejesusMunoz, Arnauld ain't (always) unbeatable! ;)
$endgroup$
– Shaggy
Feb 8 at 23:45
$begingroup$
@Shaggy leading+
is changed to!
, so it could work for input[100]
.
$endgroup$
– tsh
Feb 9 at 12:00
$begingroup$
Ah, didn't realise we had to handle singleton arrays. Nicely fixed.
$endgroup$
– Shaggy
Feb 9 at 13:46
|
show 1 more comment
$begingroup$
JavaScript (Node.js), 45 bytes
a=>!a.map(v=>o[s+=v]=-~o[s],s=0,o=[1])|o[s/2]
Try it online!
Save 4 bytes by using -~o[s]
. Thanks to Shaggy.
$endgroup$
JavaScript (Node.js), 45 bytes
a=>!a.map(v=>o[s+=v]=-~o[s],s=0,o=[1])|o[s/2]
Try it online!
Save 4 bytes by using -~o[s]
. Thanks to Shaggy.
edited Feb 9 at 11:59
answered Feb 8 at 10:28
tshtsh
9,01511650
9,01511650
$begingroup$
+1 for beating Arnauld :o
$endgroup$
– Luis felipe De jesus Munoz
Feb 8 at 12:22
$begingroup$
45 bytes
$endgroup$
– Shaggy
Feb 8 at 19:14
$begingroup$
@LuisfelipeDejesusMunoz, Arnauld ain't (always) unbeatable! ;)
$endgroup$
– Shaggy
Feb 8 at 23:45
$begingroup$
@Shaggy leading+
is changed to!
, so it could work for input[100]
.
$endgroup$
– tsh
Feb 9 at 12:00
$begingroup$
Ah, didn't realise we had to handle singleton arrays. Nicely fixed.
$endgroup$
– Shaggy
Feb 9 at 13:46
|
show 1 more comment
$begingroup$
+1 for beating Arnauld :o
$endgroup$
– Luis felipe De jesus Munoz
Feb 8 at 12:22
$begingroup$
45 bytes
$endgroup$
– Shaggy
Feb 8 at 19:14
$begingroup$
@LuisfelipeDejesusMunoz, Arnauld ain't (always) unbeatable! ;)
$endgroup$
– Shaggy
Feb 8 at 23:45
$begingroup$
@Shaggy leading+
is changed to!
, so it could work for input[100]
.
$endgroup$
– tsh
Feb 9 at 12:00
$begingroup$
Ah, didn't realise we had to handle singleton arrays. Nicely fixed.
$endgroup$
– Shaggy
Feb 9 at 13:46
$begingroup$
+1 for beating Arnauld :o
$endgroup$
– Luis felipe De jesus Munoz
Feb 8 at 12:22
$begingroup$
+1 for beating Arnauld :o
$endgroup$
– Luis felipe De jesus Munoz
Feb 8 at 12:22
$begingroup$
45 bytes
$endgroup$
– Shaggy
Feb 8 at 19:14
$begingroup$
45 bytes
$endgroup$
– Shaggy
Feb 8 at 19:14
$begingroup$
@LuisfelipeDejesusMunoz, Arnauld ain't (always) unbeatable! ;)
$endgroup$
– Shaggy
Feb 8 at 23:45
$begingroup$
@LuisfelipeDejesusMunoz, Arnauld ain't (always) unbeatable! ;)
$endgroup$
– Shaggy
Feb 8 at 23:45
$begingroup$
@Shaggy leading
+
is changed to !
, so it could work for input [100]
.$endgroup$
– tsh
Feb 9 at 12:00
$begingroup$
@Shaggy leading
+
is changed to !
, so it could work for input [100]
.$endgroup$
– tsh
Feb 9 at 12:00
$begingroup$
Ah, didn't realise we had to handle singleton arrays. Nicely fixed.
$endgroup$
– Shaggy
Feb 9 at 13:46
$begingroup$
Ah, didn't realise we had to handle singleton arrays. Nicely fixed.
$endgroup$
– Shaggy
Feb 9 at 13:46
|
show 1 more comment
$begingroup$
Perl 5 -p
, 42 41 bytes
@NahuelFouilleul saves a byte
y/ /+/;$+=eval$'==eval$`while/^|$|+/g}{
Try it online!
$endgroup$
$begingroup$
y/ /+/;
saves 1 byte
$endgroup$
– Nahuel Fouilleul
Feb 7 at 16:56
$begingroup$
34 bytes using other approach
$endgroup$
– Nahuel Fouilleul
Feb 7 at 19:57
$begingroup$
30 bytes
$endgroup$
– Nahuel Fouilleul
Feb 7 at 20:03
add a comment |
$begingroup$
Perl 5 -p
, 42 41 bytes
@NahuelFouilleul saves a byte
y/ /+/;$+=eval$'==eval$`while/^|$|+/g}{
Try it online!
$endgroup$
$begingroup$
y/ /+/;
saves 1 byte
$endgroup$
– Nahuel Fouilleul
Feb 7 at 16:56
$begingroup$
34 bytes using other approach
$endgroup$
– Nahuel Fouilleul
Feb 7 at 19:57
$begingroup$
30 bytes
$endgroup$
– Nahuel Fouilleul
Feb 7 at 20:03
add a comment |
$begingroup$
Perl 5 -p
, 42 41 bytes
@NahuelFouilleul saves a byte
y/ /+/;$+=eval$'==eval$`while/^|$|+/g}{
Try it online!
$endgroup$
Perl 5 -p
, 42 41 bytes
@NahuelFouilleul saves a byte
y/ /+/;$+=eval$'==eval$`while/^|$|+/g}{
Try it online!
edited Feb 7 at 16:58
answered Feb 7 at 15:51
XcaliXcali
5,335520
5,335520
$begingroup$
y/ /+/;
saves 1 byte
$endgroup$
– Nahuel Fouilleul
Feb 7 at 16:56
$begingroup$
34 bytes using other approach
$endgroup$
– Nahuel Fouilleul
Feb 7 at 19:57
$begingroup$
30 bytes
$endgroup$
– Nahuel Fouilleul
Feb 7 at 20:03
add a comment |
$begingroup$
y/ /+/;
saves 1 byte
$endgroup$
– Nahuel Fouilleul
Feb 7 at 16:56
$begingroup$
34 bytes using other approach
$endgroup$
– Nahuel Fouilleul
Feb 7 at 19:57
$begingroup$
30 bytes
$endgroup$
– Nahuel Fouilleul
Feb 7 at 20:03
$begingroup$
y/ /+/;
saves 1 byte$endgroup$
– Nahuel Fouilleul
Feb 7 at 16:56
$begingroup$
y/ /+/;
saves 1 byte$endgroup$
– Nahuel Fouilleul
Feb 7 at 16:56
$begingroup$
34 bytes using other approach
$endgroup$
– Nahuel Fouilleul
Feb 7 at 19:57
$begingroup$
34 bytes using other approach
$endgroup$
– Nahuel Fouilleul
Feb 7 at 19:57
$begingroup$
30 bytes
$endgroup$
– Nahuel Fouilleul
Feb 7 at 20:03
$begingroup$
30 bytes
$endgroup$
– Nahuel Fouilleul
Feb 7 at 20:03
add a comment |
$begingroup$
JavaScript (ES6), 52 bytes
a=>a.map(x=>n+=(s+=x)==eval(a.join`+`)-s,n=s=0)|n+!s
Try it online!
Commented
a => // a = input array
a.map(x => // for each element x in a:
n += // increment n if the following test is truthy:
(s += x) // update the left sum
== // and test whether it's equal to
eval(a.join`+`) - s, // the right sum
n = s =0 // start with n = s = 0
) // end of map()
| n // yield n; if the final sum is 0, it means that we could have
+ // created a balanced account at the beginning of the process;
!s // so, we increment n if it is
Recursive version, 54 53 bytes
f=(a,s=0)=>a+a?(s==eval(a.join`+`))+f(a,s+a.pop()):!s
Try it online!
$endgroup$
$begingroup$
I was just about to suggest that 52-byte version!
$endgroup$
– Shaggy
Feb 7 at 16:00
$begingroup$
@Shaggy Yeah, I discarded the non-recursive version too soon because I thought the recursive one could be shorter.
$endgroup$
– Arnauld
Feb 7 at 16:03
add a comment |
$begingroup$
JavaScript (ES6), 52 bytes
a=>a.map(x=>n+=(s+=x)==eval(a.join`+`)-s,n=s=0)|n+!s
Try it online!
Commented
a => // a = input array
a.map(x => // for each element x in a:
n += // increment n if the following test is truthy:
(s += x) // update the left sum
== // and test whether it's equal to
eval(a.join`+`) - s, // the right sum
n = s =0 // start with n = s = 0
) // end of map()
| n // yield n; if the final sum is 0, it means that we could have
+ // created a balanced account at the beginning of the process;
!s // so, we increment n if it is
Recursive version, 54 53 bytes
f=(a,s=0)=>a+a?(s==eval(a.join`+`))+f(a,s+a.pop()):!s
Try it online!
$endgroup$
$begingroup$
I was just about to suggest that 52-byte version!
$endgroup$
– Shaggy
Feb 7 at 16:00
$begingroup$
@Shaggy Yeah, I discarded the non-recursive version too soon because I thought the recursive one could be shorter.
$endgroup$
– Arnauld
Feb 7 at 16:03
add a comment |
$begingroup$
JavaScript (ES6), 52 bytes
a=>a.map(x=>n+=(s+=x)==eval(a.join`+`)-s,n=s=0)|n+!s
Try it online!
Commented
a => // a = input array
a.map(x => // for each element x in a:
n += // increment n if the following test is truthy:
(s += x) // update the left sum
== // and test whether it's equal to
eval(a.join`+`) - s, // the right sum
n = s =0 // start with n = s = 0
) // end of map()
| n // yield n; if the final sum is 0, it means that we could have
+ // created a balanced account at the beginning of the process;
!s // so, we increment n if it is
Recursive version, 54 53 bytes
f=(a,s=0)=>a+a?(s==eval(a.join`+`))+f(a,s+a.pop()):!s
Try it online!
$endgroup$
JavaScript (ES6), 52 bytes
a=>a.map(x=>n+=(s+=x)==eval(a.join`+`)-s,n=s=0)|n+!s
Try it online!
Commented
a => // a = input array
a.map(x => // for each element x in a:
n += // increment n if the following test is truthy:
(s += x) // update the left sum
== // and test whether it's equal to
eval(a.join`+`) - s, // the right sum
n = s =0 // start with n = s = 0
) // end of map()
| n // yield n; if the final sum is 0, it means that we could have
+ // created a balanced account at the beginning of the process;
!s // so, we increment n if it is
Recursive version, 54 53 bytes
f=(a,s=0)=>a+a?(s==eval(a.join`+`))+f(a,s+a.pop()):!s
Try it online!
edited Feb 7 at 17:12
answered Feb 7 at 14:57
ArnauldArnauld
76.1k693320
76.1k693320
$begingroup$
I was just about to suggest that 52-byte version!
$endgroup$
– Shaggy
Feb 7 at 16:00
$begingroup$
@Shaggy Yeah, I discarded the non-recursive version too soon because I thought the recursive one could be shorter.
$endgroup$
– Arnauld
Feb 7 at 16:03
add a comment |
$begingroup$
I was just about to suggest that 52-byte version!
$endgroup$
– Shaggy
Feb 7 at 16:00
$begingroup$
@Shaggy Yeah, I discarded the non-recursive version too soon because I thought the recursive one could be shorter.
$endgroup$
– Arnauld
Feb 7 at 16:03
$begingroup$
I was just about to suggest that 52-byte version!
$endgroup$
– Shaggy
Feb 7 at 16:00
$begingroup$
I was just about to suggest that 52-byte version!
$endgroup$
– Shaggy
Feb 7 at 16:00
$begingroup$
@Shaggy Yeah, I discarded the non-recursive version too soon because I thought the recursive one could be shorter.
$endgroup$
– Arnauld
Feb 7 at 16:03
$begingroup$
@Shaggy Yeah, I discarded the non-recursive version too soon because I thought the recursive one could be shorter.
$endgroup$
– Arnauld
Feb 7 at 16:03
add a comment |
$begingroup$
APL (Dyalog Unicode), 21 bytesSBCS
Anonymous tacit prefix function
+/⊂((+/↑)=1⊥↓)¨⍨0,⍳∘≢
Try it online!
⍳
ɩndices
∘
of≢
the tally of transactions
0,
prepend zero
⊂(
…)¨⍨
apply the following tacit function with each of those as left argument and the entire list of transactions as right argument (⍨
swaps argument
⊂
the entire list of transactions
(
…)
as left argument to the below function
¨
applied to each of the indices
⍨
with swapped arguments (i.e. list on right, indices on left:
↓
drop that many from the left
1⊥
sum (lit. evaluate in base-1)
(
…)=
is it (0/1) equal to…
↑
take that many transactions from the left
+/
sum them
+/
sum that Boolean list to get the count of truths
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Unicode), 21 bytesSBCS
Anonymous tacit prefix function
+/⊂((+/↑)=1⊥↓)¨⍨0,⍳∘≢
Try it online!
⍳
ɩndices
∘
of≢
the tally of transactions
0,
prepend zero
⊂(
…)¨⍨
apply the following tacit function with each of those as left argument and the entire list of transactions as right argument (⍨
swaps argument
⊂
the entire list of transactions
(
…)
as left argument to the below function
¨
applied to each of the indices
⍨
with swapped arguments (i.e. list on right, indices on left:
↓
drop that many from the left
1⊥
sum (lit. evaluate in base-1)
(
…)=
is it (0/1) equal to…
↑
take that many transactions from the left
+/
sum them
+/
sum that Boolean list to get the count of truths
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Unicode), 21 bytesSBCS
Anonymous tacit prefix function
+/⊂((+/↑)=1⊥↓)¨⍨0,⍳∘≢
Try it online!
⍳
ɩndices
∘
of≢
the tally of transactions
0,
prepend zero
⊂(
…)¨⍨
apply the following tacit function with each of those as left argument and the entire list of transactions as right argument (⍨
swaps argument
⊂
the entire list of transactions
(
…)
as left argument to the below function
¨
applied to each of the indices
⍨
with swapped arguments (i.e. list on right, indices on left:
↓
drop that many from the left
1⊥
sum (lit. evaluate in base-1)
(
…)=
is it (0/1) equal to…
↑
take that many transactions from the left
+/
sum them
+/
sum that Boolean list to get the count of truths
$endgroup$
APL (Dyalog Unicode), 21 bytesSBCS
Anonymous tacit prefix function
+/⊂((+/↑)=1⊥↓)¨⍨0,⍳∘≢
Try it online!
⍳
ɩndices
∘
of≢
the tally of transactions
0,
prepend zero
⊂(
…)¨⍨
apply the following tacit function with each of those as left argument and the entire list of transactions as right argument (⍨
swaps argument
⊂
the entire list of transactions
(
…)
as left argument to the below function
¨
applied to each of the indices
⍨
with swapped arguments (i.e. list on right, indices on left:
↓
drop that many from the left
1⊥
sum (lit. evaluate in base-1)
(
…)=
is it (0/1) equal to…
↑
take that many transactions from the left
+/
sum them
+/
sum that Boolean list to get the count of truths
edited Feb 7 at 17:58
answered Feb 7 at 15:08
AdámAdám
28k273199
28k273199
add a comment |
add a comment |
$begingroup$
Batch, 84 bytes
@set s=%*
@set/as=%s: =+%,c=0
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
@echo %c%
Takes input as command-line arguments. Explanation:
@set s=%*
Join the arguments with spaces.
@set/as=%s: =+%,c=0
Replace the spaces with +
s and evaluate the result. Also clear the count.
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
For each amount, subtract double that from the sum. If the result is zero, then this is a valid match, so increment the count. The extra zero at the beginning allows for a match before any amounts.
@echo %c%
Print the result.
$endgroup$
add a comment |
$begingroup$
Batch, 84 bytes
@set s=%*
@set/as=%s: =+%,c=0
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
@echo %c%
Takes input as command-line arguments. Explanation:
@set s=%*
Join the arguments with spaces.
@set/as=%s: =+%,c=0
Replace the spaces with +
s and evaluate the result. Also clear the count.
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
For each amount, subtract double that from the sum. If the result is zero, then this is a valid match, so increment the count. The extra zero at the beginning allows for a match before any amounts.
@echo %c%
Print the result.
$endgroup$
add a comment |
$begingroup$
Batch, 84 bytes
@set s=%*
@set/as=%s: =+%,c=0
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
@echo %c%
Takes input as command-line arguments. Explanation:
@set s=%*
Join the arguments with spaces.
@set/as=%s: =+%,c=0
Replace the spaces with +
s and evaluate the result. Also clear the count.
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
For each amount, subtract double that from the sum. If the result is zero, then this is a valid match, so increment the count. The extra zero at the beginning allows for a match before any amounts.
@echo %c%
Print the result.
$endgroup$
Batch, 84 bytes
@set s=%*
@set/as=%s: =+%,c=0
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
@echo %c%
Takes input as command-line arguments. Explanation:
@set s=%*
Join the arguments with spaces.
@set/as=%s: =+%,c=0
Replace the spaces with +
s and evaluate the result. Also clear the count.
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
For each amount, subtract double that from the sum. If the result is zero, then this is a valid match, so increment the count. The extra zero at the beginning allows for a match before any amounts.
@echo %c%
Print the result.
answered Feb 7 at 20:58
NeilNeil
80.7k744178
80.7k744178
add a comment |
add a comment |
$begingroup$
Charcoal, 15 bytes
⊞θ⁰IΣEθ⁼Σθ⊗Σ✂θκ
Try it online! Link is to verbose version of code. Explanation:
θ Input list
⁰ Literal 0
⊞ Push to list
θ Augmented list
E Mapped to
θ Augmented list
✂ Sliced from
κ Current index
Σ Summed
⊗ Doubled
⁼ Equals
θ (Augmented) list
Σ Summed
Σ Sum of booleans
I Cast to string
Implicitly print
Unfortunately in Charcoal Sum()
is not 0
so I have to ensure that there is always at least one element to sum.
$endgroup$
add a comment |
$begingroup$
Charcoal, 15 bytes
⊞θ⁰IΣEθ⁼Σθ⊗Σ✂θκ
Try it online! Link is to verbose version of code. Explanation:
θ Input list
⁰ Literal 0
⊞ Push to list
θ Augmented list
E Mapped to
θ Augmented list
✂ Sliced from
κ Current index
Σ Summed
⊗ Doubled
⁼ Equals
θ (Augmented) list
Σ Summed
Σ Sum of booleans
I Cast to string
Implicitly print
Unfortunately in Charcoal Sum()
is not 0
so I have to ensure that there is always at least one element to sum.
$endgroup$
add a comment |
$begingroup$
Charcoal, 15 bytes
⊞θ⁰IΣEθ⁼Σθ⊗Σ✂θκ
Try it online! Link is to verbose version of code. Explanation:
θ Input list
⁰ Literal 0
⊞ Push to list
θ Augmented list
E Mapped to
θ Augmented list
✂ Sliced from
κ Current index
Σ Summed
⊗ Doubled
⁼ Equals
θ (Augmented) list
Σ Summed
Σ Sum of booleans
I Cast to string
Implicitly print
Unfortunately in Charcoal Sum()
is not 0
so I have to ensure that there is always at least one element to sum.
$endgroup$
Charcoal, 15 bytes
⊞θ⁰IΣEθ⁼Σθ⊗Σ✂θκ
Try it online! Link is to verbose version of code. Explanation:
θ Input list
⁰ Literal 0
⊞ Push to list
θ Augmented list
E Mapped to
θ Augmented list
✂ Sliced from
κ Current index
Σ Summed
⊗ Doubled
⁼ Equals
θ (Augmented) list
Σ Summed
Σ Sum of booleans
I Cast to string
Implicitly print
Unfortunately in Charcoal Sum()
is not 0
so I have to ensure that there is always at least one element to sum.
answered Feb 7 at 21:11
NeilNeil
80.7k744178
80.7k744178
add a comment |
add a comment |
$begingroup$
Python 3, 67 58 bytes
lambda l:sum(sum(l[:x])*2==sum(l)for x in range(len(l)+1))
Try it online!
-9 bytes thanks to @Don't be a x-triple dot
$endgroup$
1
$begingroup$
Summing instead of filtering will save you 7 bytes:lambda l:sum(sum(l[:x])==sum(l[x:])for x in range(len(l)+1))
.
$endgroup$
– Don't be a x-triple dot
Feb 7 at 18:43
$begingroup$
sum(l[:x])*2==sum(l)
saves you another 2 bytes.
$endgroup$
– Neil
Feb 7 at 20:49
add a comment |
$begingroup$
Python 3, 67 58 bytes
lambda l:sum(sum(l[:x])*2==sum(l)for x in range(len(l)+1))
Try it online!
-9 bytes thanks to @Don't be a x-triple dot
$endgroup$
1
$begingroup$
Summing instead of filtering will save you 7 bytes:lambda l:sum(sum(l[:x])==sum(l[x:])for x in range(len(l)+1))
.
$endgroup$
– Don't be a x-triple dot
Feb 7 at 18:43
$begingroup$
sum(l[:x])*2==sum(l)
saves you another 2 bytes.
$endgroup$
– Neil
Feb 7 at 20:49
add a comment |
$begingroup$
Python 3, 67 58 bytes
lambda l:sum(sum(l[:x])*2==sum(l)for x in range(len(l)+1))
Try it online!
-9 bytes thanks to @Don't be a x-triple dot
$endgroup$
Python 3, 67 58 bytes
lambda l:sum(sum(l[:x])*2==sum(l)for x in range(len(l)+1))
Try it online!
-9 bytes thanks to @Don't be a x-triple dot
edited Feb 7 at 21:23
answered Feb 7 at 15:43
Black Owl KaiBlack Owl Kai
69011
69011
1
$begingroup$
Summing instead of filtering will save you 7 bytes:lambda l:sum(sum(l[:x])==sum(l[x:])for x in range(len(l)+1))
.
$endgroup$
– Don't be a x-triple dot
Feb 7 at 18:43
$begingroup$
sum(l[:x])*2==sum(l)
saves you another 2 bytes.
$endgroup$
– Neil
Feb 7 at 20:49
add a comment |
1
$begingroup$
Summing instead of filtering will save you 7 bytes:lambda l:sum(sum(l[:x])==sum(l[x:])for x in range(len(l)+1))
.
$endgroup$
– Don't be a x-triple dot
Feb 7 at 18:43
$begingroup$
sum(l[:x])*2==sum(l)
saves you another 2 bytes.
$endgroup$
– Neil
Feb 7 at 20:49
1
1
$begingroup$
Summing instead of filtering will save you 7 bytes:
lambda l:sum(sum(l[:x])==sum(l[x:])for x in range(len(l)+1))
.$endgroup$
– Don't be a x-triple dot
Feb 7 at 18:43
$begingroup$
Summing instead of filtering will save you 7 bytes:
lambda l:sum(sum(l[:x])==sum(l[x:])for x in range(len(l)+1))
.$endgroup$
– Don't be a x-triple dot
Feb 7 at 18:43
$begingroup$
sum(l[:x])*2==sum(l)
saves you another 2 bytes.$endgroup$
– Neil
Feb 7 at 20:49
$begingroup$
sum(l[:x])*2==sum(l)
saves you another 2 bytes.$endgroup$
– Neil
Feb 7 at 20:49
add a comment |
$begingroup$
R, 50 37 bytes
sum(c(0,cumsum(x<-scan()))==sum(x)/2)
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 50 37 bytes
sum(c(0,cumsum(x<-scan()))==sum(x)/2)
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 50 37 bytes
sum(c(0,cumsum(x<-scan()))==sum(x)/2)
Try it online!
$endgroup$
R, 50 37 bytes
sum(c(0,cumsum(x<-scan()))==sum(x)/2)
Try it online!
edited Feb 8 at 8:36
answered Feb 7 at 15:47
Kirill L.Kirill L.
4,3351422
4,3351422
add a comment |
add a comment |
$begingroup$
MATL, 9 bytes
s0GhYsE=s
Try it online!
Same approach as some other answers: prepend a zero, and check how often half the cumulative sum is equal to the total sum.
s % Total sum of (implicit) input
0Gh % Prepend 0 to another copy of the input
Ys % Cumulative sum
E= % Check element-wise equality of 2*cumulative sum with total sum
s % Sum number of `true` values
$endgroup$
add a comment |
$begingroup$
MATL, 9 bytes
s0GhYsE=s
Try it online!
Same approach as some other answers: prepend a zero, and check how often half the cumulative sum is equal to the total sum.
s % Total sum of (implicit) input
0Gh % Prepend 0 to another copy of the input
Ys % Cumulative sum
E= % Check element-wise equality of 2*cumulative sum with total sum
s % Sum number of `true` values
$endgroup$
add a comment |
$begingroup$
MATL, 9 bytes
s0GhYsE=s
Try it online!
Same approach as some other answers: prepend a zero, and check how often half the cumulative sum is equal to the total sum.
s % Total sum of (implicit) input
0Gh % Prepend 0 to another copy of the input
Ys % Cumulative sum
E= % Check element-wise equality of 2*cumulative sum with total sum
s % Sum number of `true` values
$endgroup$
MATL, 9 bytes
s0GhYsE=s
Try it online!
Same approach as some other answers: prepend a zero, and check how often half the cumulative sum is equal to the total sum.
s % Total sum of (implicit) input
0Gh % Prepend 0 to another copy of the input
Ys % Cumulative sum
E= % Check element-wise equality of 2*cumulative sum with total sum
s % Sum number of `true` values
answered Feb 8 at 13:39
SanchisesSanchises
5,89212351
5,89212351
add a comment |
add a comment |
$begingroup$
Japt -x
, 14 11 bytes
iT å+ ®¥nUx
Try it
iT å+ ®¥nUx :Implicit input of array U
i :Prepend
T : Zero
å+ :Cumulatively reduce by addition
® :Map each Z
¥ : Test for equality with
n : Z subtracted from
Ux : U reduced by addition
:Implicitly reduce by addition and output
$endgroup$
add a comment |
$begingroup$
Japt -x
, 14 11 bytes
iT å+ ®¥nUx
Try it
iT å+ ®¥nUx :Implicit input of array U
i :Prepend
T : Zero
å+ :Cumulatively reduce by addition
® :Map each Z
¥ : Test for equality with
n : Z subtracted from
Ux : U reduced by addition
:Implicitly reduce by addition and output
$endgroup$
add a comment |
$begingroup$
Japt -x
, 14 11 bytes
iT å+ ®¥nUx
Try it
iT å+ ®¥nUx :Implicit input of array U
i :Prepend
T : Zero
å+ :Cumulatively reduce by addition
® :Map each Z
¥ : Test for equality with
n : Z subtracted from
Ux : U reduced by addition
:Implicitly reduce by addition and output
$endgroup$
Japt -x
, 14 11 bytes
iT å+ ®¥nUx
Try it
iT å+ ®¥nUx :Implicit input of array U
i :Prepend
T : Zero
å+ :Cumulatively reduce by addition
® :Map each Z
¥ : Test for equality with
n : Z subtracted from
Ux : U reduced by addition
:Implicitly reduce by addition and output
edited Feb 8 at 16:46
answered Feb 7 at 15:14
ShaggyShaggy
19.9k21667
19.9k21667
add a comment |
add a comment |
$begingroup$
PowerShell, 88 82 bytes
-6 Bytes thanks to mazzy
param($n)0..($x=$n.length)|%{$i+=+$z-eq($n[$_..$x]|measure -Su).sum;$z+=$n[$_]};$i
Try it online!
This seems like a very clumsy method but it got the job done. I'll try and revamp it in the future.
$endgroup$
1
$begingroup$
you can write$i+=<predicate>
insteadif(<predicate>){$i++}
$endgroup$
– mazzy
Feb 8 at 6:15
add a comment |
$begingroup$
PowerShell, 88 82 bytes
-6 Bytes thanks to mazzy
param($n)0..($x=$n.length)|%{$i+=+$z-eq($n[$_..$x]|measure -Su).sum;$z+=$n[$_]};$i
Try it online!
This seems like a very clumsy method but it got the job done. I'll try and revamp it in the future.
$endgroup$
1
$begingroup$
you can write$i+=<predicate>
insteadif(<predicate>){$i++}
$endgroup$
– mazzy
Feb 8 at 6:15
add a comment |
$begingroup$
PowerShell, 88 82 bytes
-6 Bytes thanks to mazzy
param($n)0..($x=$n.length)|%{$i+=+$z-eq($n[$_..$x]|measure -Su).sum;$z+=$n[$_]};$i
Try it online!
This seems like a very clumsy method but it got the job done. I'll try and revamp it in the future.
$endgroup$
PowerShell, 88 82 bytes
-6 Bytes thanks to mazzy
param($n)0..($x=$n.length)|%{$i+=+$z-eq($n[$_..$x]|measure -Su).sum;$z+=$n[$_]};$i
Try it online!
This seems like a very clumsy method but it got the job done. I'll try and revamp it in the future.
edited Feb 8 at 21:18
answered Feb 8 at 4:46
VeskahVeskah
93814
93814
1
$begingroup$
you can write$i+=<predicate>
insteadif(<predicate>){$i++}
$endgroup$
– mazzy
Feb 8 at 6:15
add a comment |
1
$begingroup$
you can write$i+=<predicate>
insteadif(<predicate>){$i++}
$endgroup$
– mazzy
Feb 8 at 6:15
1
1
$begingroup$
you can write
$i+=<predicate>
instead if(<predicate>){$i++}
$endgroup$
– mazzy
Feb 8 at 6:15
$begingroup$
you can write
$i+=<predicate>
instead if(<predicate>){$i++}
$endgroup$
– mazzy
Feb 8 at 6:15
add a comment |
$begingroup$
bash, 52 bytes
IFS=+;for i in 0 $@;{((c+=2*(x+=i)=="$*"));};echo $c
TIO
The trick: setting IFS=+
, "$*"
expands to a string where arguments are delimited by +
, in arithmetic expression eval to the sum
$endgroup$
add a comment |
$begingroup$
bash, 52 bytes
IFS=+;for i in 0 $@;{((c+=2*(x+=i)=="$*"));};echo $c
TIO
The trick: setting IFS=+
, "$*"
expands to a string where arguments are delimited by +
, in arithmetic expression eval to the sum
$endgroup$
add a comment |
$begingroup$
bash, 52 bytes
IFS=+;for i in 0 $@;{((c+=2*(x+=i)=="$*"));};echo $c
TIO
The trick: setting IFS=+
, "$*"
expands to a string where arguments are delimited by +
, in arithmetic expression eval to the sum
$endgroup$
bash, 52 bytes
IFS=+;for i in 0 $@;{((c+=2*(x+=i)=="$*"));};echo $c
TIO
The trick: setting IFS=+
, "$*"
expands to a string where arguments are delimited by +
, in arithmetic expression eval to the sum
edited Feb 8 at 10:30
answered Feb 7 at 20:25
Nahuel FouilleulNahuel Fouilleul
2,30529
2,30529
add a comment |
add a comment |
$begingroup$
PowerShell, 49 45 36 bytes
(($args+0|%{2*($s+=$_)})-eq$s).count
Try it online!
$endgroup$
add a comment |
$begingroup$
PowerShell, 49 45 36 bytes
(($args+0|%{2*($s+=$_)})-eq$s).count
Try it online!
$endgroup$
add a comment |
$begingroup$
PowerShell, 49 45 36 bytes
(($args+0|%{2*($s+=$_)})-eq$s).count
Try it online!
$endgroup$
PowerShell, 49 45 36 bytes
(($args+0|%{2*($s+=$_)})-eq$s).count
Try it online!
edited Feb 9 at 10:35
answered Feb 8 at 17:24
mazzymazzy
2,4651316
2,4651316
add a comment |
add a comment |
$begingroup$
Brachylog, 9 bytes
Not as good as day 1. This one loses to Jelly
{~c₂+ᵐ=}ᶜ
Explanation
{ }ᶜ # Count the ways to:
~c₂ # Split the input array in 2 ...
+ᵐ # so that their sums ...
= # are equal
Test suite:
Try it online!
$endgroup$
add a comment |
$begingroup$
Brachylog, 9 bytes
Not as good as day 1. This one loses to Jelly
{~c₂+ᵐ=}ᶜ
Explanation
{ }ᶜ # Count the ways to:
~c₂ # Split the input array in 2 ...
+ᵐ # so that their sums ...
= # are equal
Test suite:
Try it online!
$endgroup$
add a comment |
$begingroup$
Brachylog, 9 bytes
Not as good as day 1. This one loses to Jelly
{~c₂+ᵐ=}ᶜ
Explanation
{ }ᶜ # Count the ways to:
~c₂ # Split the input array in 2 ...
+ᵐ # so that their sums ...
= # are equal
Test suite:
Try it online!
$endgroup$
Brachylog, 9 bytes
Not as good as day 1. This one loses to Jelly
{~c₂+ᵐ=}ᶜ
Explanation
{ }ᶜ # Count the ways to:
~c₂ # Split the input array in 2 ...
+ᵐ # so that their sums ...
= # are equal
Test suite:
Try it online!
answered Feb 10 at 12:47
KroppebKroppeb
1,206210
1,206210
add a comment |
add a comment |
$begingroup$
Haskell, 46 35 bytes
f x=sum[1|a<-scanl(+)0x,a==sum x/2]
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 46 35 bytes
f x=sum[1|a<-scanl(+)0x,a==sum x/2]
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 46 35 bytes
f x=sum[1|a<-scanl(+)0x,a==sum x/2]
Try it online!
$endgroup$
Haskell, 46 35 bytes
f x=sum[1|a<-scanl(+)0x,a==sum x/2]
Try it online!
edited Feb 10 at 18:37
answered Feb 10 at 16:09
niminimi
31.8k32285
31.8k32285
add a comment |
add a comment |
$begingroup$
J, 19 bytes
1#.[:(={:-])0+/@,]
Try it online!
explanation
1 #. [: (= ({: - ])) 0 +/@, ]
0 , ] NB. prepend 0 to input...
+/@ NB. and take the prefix sums...
[: ({: - ]) NB. then subtract that list
NB. from its final elm
NB. (`{:`), giving the list
NB. of suffix sums...
[: (= ( )) NB. create a 1-0 list showing
NB. where the prefix sums
NB. equal the suffix sums
1 #. NB. and take the sum.
$endgroup$
add a comment |
$begingroup$
J, 19 bytes
1#.[:(={:-])0+/@,]
Try it online!
explanation
1 #. [: (= ({: - ])) 0 +/@, ]
0 , ] NB. prepend 0 to input...
+/@ NB. and take the prefix sums...
[: ({: - ]) NB. then subtract that list
NB. from its final elm
NB. (`{:`), giving the list
NB. of suffix sums...
[: (= ( )) NB. create a 1-0 list showing
NB. where the prefix sums
NB. equal the suffix sums
1 #. NB. and take the sum.
$endgroup$
add a comment |
$begingroup$
J, 19 bytes
1#.[:(={:-])0+/@,]
Try it online!
explanation
1 #. [: (= ({: - ])) 0 +/@, ]
0 , ] NB. prepend 0 to input...
+/@ NB. and take the prefix sums...
[: ({: - ]) NB. then subtract that list
NB. from its final elm
NB. (`{:`), giving the list
NB. of suffix sums...
[: (= ( )) NB. create a 1-0 list showing
NB. where the prefix sums
NB. equal the suffix sums
1 #. NB. and take the sum.
$endgroup$
J, 19 bytes
1#.[:(={:-])0+/@,]
Try it online!
explanation
1 #. [: (= ({: - ])) 0 +/@, ]
0 , ] NB. prepend 0 to input...
+/@ NB. and take the prefix sums...
[: ({: - ]) NB. then subtract that list
NB. from its final elm
NB. (`{:`), giving the list
NB. of suffix sums...
[: (= ( )) NB. create a 1-0 list showing
NB. where the prefix sums
NB. equal the suffix sums
1 #. NB. and take the sum.
edited yesterday
answered 2 days ago
JonahJonah
2,121916
2,121916
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179631%2fparallel-accounts-day-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
11
$begingroup$
After 6 months of frozen and newly created accounts, it is reported that Marie's banker is now interned in a sanitarium. "We are your friends. You need some rest", they said.
$endgroup$
– Arnauld
Feb 7 at 17:20
2
$begingroup$
Suggested test case of a single transaction
$endgroup$
– Veskah
Feb 8 at 4:53