平行帐户(第2天)


10

挑战经我的大学代码挑战竞赛许可


几个月前完成学业后,玛丽(Marie)开了一个银行帐户,开始在镇上领取她的第一份工作。从那时起,她一直在与它进行一些交易。她的第一笔付款是1000美元。她用这笔钱支付了一次晚餐,邀请她的父母参加(晚餐费用为150美元),然后,她在一家知名的超市购物(80美元),并为度假预订了旅馆(200美元)。月底,她又收到了付款(1040美元,比前一个月略多),第二天又在超市花了70美元。

如今,她意识到,如果在超市中支付了第一笔80美元之后,已经创建了第二个帐户并冻结了第一个帐户,则两个帐户的余额将完全相同:

100015080Total=770200104070Total=770

该事件对她来说非常罕见,以至于她想继续确定她的帐户和她的朋友的活动是否也具有此功能。

挑战

给定交易列表,输出银行帐户所有者可以创建第二个帐户,以便两者都具有相同的最终余额的瞬时时间数。

例如: [1000, -150, -80, -200, 1040, -70]

1)Total=0100015080200104070Total=1540
2)1000Total=100015080200104070Total=540
3)1000150Total=85080200104070Total=690
4)100015080Total=770200104070Total=770
5)100015080200Total=570104070Total=970
6)1000150802001040Total=161070Total=70
7)100015080200104070Total=1540Total=0

测试用例

  • 输入:1000 -150 -80 -200 1040 -70输出:1
  • 输入:100 -100输出:2
  • 输入:1 2 3输出:1
  • 输入:10 -20 15输出:0
  • 输入:15 -15 15 -15输出:3
  • 输入:1输出:0

笔记


11
据报道,经过6个月的冻结和新创建的帐户,玛丽的银行家现在被拘留在一家疗养院。他们说:“我们是你的朋友。你需要休息。”
阿纳尔德

2
建议的单笔交易测试案例
Veskah,

Answers:



4

Perl 6,25个字节

{+grep .sum/2,[\+] 0,|$_}

在线尝试!

说明

我们只是在给定的列表(0,|$_)前面加上一个零,用组成一个部分和[\+]的序列(即,第一个元素形成的序列,前两个元素的和,前三个元素的和等),然后在(grep)中查找任何元素等于最终帐户状态的一半(给定列表的总和)。最后,我们用来计数+


3

05AB1E,11 个字节

0.ø.œ2ùO€ËO

在线尝试验证所有测试用例

说明:

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


3

JavaScript(Node.js),45字节

a=>!a.map(v=>o[s+=v]=-~o[s],s=0,o=[1])|o[s/2]

在线尝试!

使用保存4个字节-~o[s]。感谢Shaggy。




@ LuisfelipeDejesusMunoz,Arnauld不会(永远)无与伦比!;)
Shaggy

@Shaggy Leading +已更改为!,因此可以用于输入[100]
tsh

啊,没有意识到我们必须处理单例数组。很好地解决了。
毛茸茸的


2

JavaScript(ES6),52个字节

a=>a.map(x=>n+=(s+=x)==eval(a.join`+`)-s,n=s=0)|n+!s

在线尝试!

已评论

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

递归版本, 54  53字节

f=(a,s=0)=>a+a?(s==eval(a.join`+`))+f(a,s+a.pop()):!s

在线尝试!


我只是建议使用52字节版本!
毛茸茸的

@Shaggy是的,我太早放弃了非递归版本,因为我认为递归版本会更短。
Arnauld

2

APL(Dyalog Unicode)的,21 字节SBCS

匿名默认前缀功能

+/⊂((+/↑)=1⊥↓)¨⍨0,⍳∘≢

在线尝试!

ɩ ndices
 的
 交易帐簿

0, 前置零

⊂()¨⍨ 应用以下默认函数,将每个默认函数用作左参数,将整个事务列表作为右参数(swaps参数

 整个交易清单
(... ) 作为以下函数的左参数,
  ¨ 应用于
    具有交换参数的每个索引(即,列表在右侧,索引在左侧:

   从左边掉那么多

  1⊥ 总和(以1为基数评估)

  ()= (0/1)是否等于…

    从左边接那么多交易

   +/ 总结一下

+/ 将该布尔值列表求和以得出真值


2

批处理,84字节

@set s=%*
@set/as=%s: =+%,c=0
@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s
@echo %c%

将输入作为命令行参数。说明:

@set s=%*

用空格连接参数。

@set/as=%s: =+%,c=0

+s 替换空格并评估结果。同时清除计数。

@for %%n in (0 %*)do @set/as-=%%n*2,c+=!s

对于每个数量,从总和中减去两倍。如果结果为零,则这是有效的匹配项,因此请增加计数。开头的额外零允许在任何金额之前进行匹配。

@echo %c%

打印结果。


2

木炭,15字节

⊞θ⁰IΣEθ⁼Σθ⊗Σ✂θκ

在线尝试!链接是详细版本的代码。说明:

 θ              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

不幸的是,在木炭Sum([])中并非0如此,因此我必须确保始终至少要包含一个元素。


2

Python 3中67 58个字节

lambda l:sum(sum(l[:x])*2==sum(l)for x in range(len(l)+1))

在线尝试!

-9个字节,感谢@不要是一个三重点


1
汇总而不是过滤将为您节省7个字节:lambda l:sum(sum(l[:x])==sum(l[x:])for x in range(len(l)+1))
Xcoder先生,19年

sum(l[:x])*2==sum(l)为您节省了另外2个字节。
尼尔,


2

MATL,9字节

s0GhYsE=s

在线尝试!

与其他答案相同的方法:加上零,然后检查累积总和的一半等于总和的频率。

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

2

Japt -x14 11字节

iT å+ ®¥nUx

尝试一下

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

2

PowerShell的88 82字节

-6字节感谢mazzy

param($n)0..($x=$n.length)|%{$i+=+$z-eq($n[$_..$x]|measure -Su).sum;$z+=$n[$_]};$i

在线尝试!

这似乎是一个非常笨拙的方法,但它完成了工作。以后我会尝试对其进行修改。


1
你可以写$i+=<predicate>,而不是if(<predicate>){$i++}
MAZZY


2

Brachylog,9个字节

不如第一天。这个输给了果冻

{~c₂+ᵐ=}ᶜ

说明

{      }ᶜ   # Count the ways to:
 ~c₂        #   Split the input array in 2 ...
    +ᵐ      #   so that their sums ...
      =     #   are equal

测试套件: 在线试用!




0

J,19个字节

1#.[:(={:-])0+/\@,]

在线尝试!

说明

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.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.