三重平衡数


13

描述

如果将三部分中的每一部分中的数字总和为相同的数字,我们认为该整数具有至少三位数的三重平衡。我们按如下方式拆分数字:

abcdefghi - Standard case: the number of digits is divisable through 3:
abc def ghi

abcdefgh - Number % 3 == 2: The outer groups are both assigned another digit
abc de fgh (the inner group will have one digit less than both outer groups)

abcdefghij - Number % 3 == 1: The inner group is assigned the extra digit
abc defg hij (the inner group will have one digit more than the outer groups)

挑战

您的任务是编写一个程序,给定一个至少包含3个数字的整数,该程序确定给定的数字是否是三重平衡的,并根据其结果输出真值或假值。

测试用例

333 -> True
343 -> False
3123 -> True
34725 -> True
456456 -> False
123222321 -> True

这是,因此存在标准漏洞,并且可能以字节为单位的最短答案胜出!


1
据我了解,如果可以将其平均分配,则应该这样做。
totallyhuman

@ Mr.Xcoder将其分为三部分(按照@MagicOctopusUnr的评论,它仍然不起作用:when split in three parts,
Stephen

4
考虑使用Sandbox,以避免将来出现这种情况。
Xcoder先生

2
哎呀!对于测试用例的困惑,我感到很抱歉,显然我头脑有些扭曲。现在,他们已得到纠正,希望您投票重新提出我的挑战。
racer290

5
默认情况下,允许将输入作为字符串。可以将其视为数字数组吗?
路易斯·门多

Answers:


7

Python 2中93个 88 86字节

@LeakyNun -4字节@@ Mr.Xcoder
-2字节

def g(i):l=-~len(i)/3;print f(i[:l])==f(i[l:-l])==f(i[-l:])
f=lambda a:sum(map(int,a))

在线尝试!


3

果冻,23字节

DµL‘:3‘Ṭ©œṗ⁸U®œṗЀS€€FE

在线尝试!

一定有一种更短的方法可以以某种方式飞过我的头...


看起来很简单,但实际上并非如此……由于非默认拆分,在05AB1E中甚至很难。
魔术章鱼缸

3

视网膜,89字节

^|$
¶
{`¶(.)(.*)(.)¶
$1¶$2¶$3
}`^((.)*.)(.)¶((?<-2>.)*)¶(.)
$1¶$3$4$5¶
\d
$*
^(1+)¶\1¶\1$

在线尝试!链接包括测试用例。说明:第一阶段在输入的开头和结尾添加换行符。然后,第二阶段尝试在换行符之间成对移动数字,但是如果中间没有足够的数字,则第三阶段可以将它们移回原位,从而导致循环停止。然后,第四阶段将每个数字分别转换为一进制,从而将它们求和,而最后一个阶段仅检查和是否相等。


2

Mathematica,142个字节

(s=IntegerDigits@#;c=Floor;If[Mod[t=Length@s,3]==2,a=-1;c=Ceiling,a=Mod[t,3]];Length@Union[Tr/@FoldPairList[TakeDrop,s,{z=c[t/3],z+a,z}]]==1)&

2

果冻,20字节

DµL‘:3x2jSạ¥¥LRṁ@ḅ1E

在线尝试!

怎么运行的

DµL‘:3x2jSạ¥¥LRṁ@ḅ1E  Main link. Argument: n

D                     Decimal; convert n to base 10, yielding a digits array A.
 µ                    Begin a new chain with argument A.
  L                   Compute the length of A.
   ‘                  Increment; add 1 to the length.
    :3                Divide the result by 3.
                      This yields the lengths of the outer chunks.
      x2              Repeat the result twice, creating an array C.
             L        Compute l, the length of A.
            ¥         Combine the two links to the left into a dyadic chain.
                      This chain will be called with arguments C and l. 
           ¥              Combine the two links to the left into a dyadic chain.
         S                    Take the sum of C.
          ạ                   Compute the absolute difference of the sum and l.
        j                 Join C, using the result to the right as separator.
                      We now have an array of the lengths of all three chunks the
                      digits of n have to be split in.
              R       Range; map each chunk length k to [1, ..., k].
               ṁ@     Mold swapped; takes the elements of A and give them the shape
                      of the array to the right, splitting A into chunks of the
                      computed lengths.
                 ḅ1   Convert each chunk from unary to integer, computing the sum
                      of its elements.
                   E  Test if the resulting sums are all equal.

1
我想读一个解释
Felix Dombek

@FelixDombek我添加了一个解释。
丹尼斯


0

Javascript,178个字节

(a)=>{b=a.split(/(\d)/).filter((v)=>v);s=Math.round(b.length/3);f=(m,v)=>m+parseInt(v);y=b.slice(s,-s).reduce(f,0);return b.slice(0,s).reduce(f,0)==y&&y==b.slice(-s).reduce(f,0)}

欢迎来到PPCG!您已阅读提示 页面吗?寻找答案的范围很大。
尼尔

如果您仍然感兴趣,我可以将您的答案减少到106个字节:(([...b],s=~b.length/3|0,f=(m,v)=>+m+ +v,y=b.splice(s).reduce(f))=>b.splice(-s).reduce(f)==y&y==b.reduce(f)在注释中复制时要小心,因为Stack Exchange会插入不可见的字符)。
尼尔

美丽!我在那里可以学到很多东西。
cdm

0

Java 8,149字节

q->{int l=q.length,s=(l+1)/3,a=0,b=0,c=0,i=0;for(;i<s;a+=q[i++]);for(i=s,s=l/3*2+(l%3<1?0:1);i<s;b+=q[i++]);for(i=s;i<l;c+=q[i++]);return a==b&b==c;}

将输入作为int[]

说明:

在这里尝试。

q->{                 // Method with int-array parameter and boolean return-type
  int l=q.length,    //  Length of the input-array
      s=(l+1)/3,     //  (Length + 1) divided by 3
      a=0,b=0,c=0,   //  Three sums starting at 0
      i=0;           //  Index-integer
  for(;i<s;          //  Loop (1) from 0 to `s1` (exclusive)
    a+=q[i++]        //   And increase `a` with the next digit
  );                 //  End of loop (1)
  for(i=s,s=l/3*2+(l%3<1?0:1);i<s;
                     //  Loop (2) from `s1` to `s2` (exclusive)
    b+=q[i++]        //   And increase `b` with the next digit
  );                 //  End of loop (2)
  for(i=s;i<l;       //  Loop (3) from `s2` to `l` (exclusive)
    c+=q[i++]        //   And increase `c` with the next digit
  );                 //  End of loop (3)
  return a==b&b==c;  //  Return if `a`, `b` and `c` are equal
}                    // End of method

以下是每种长度的0索引(专用)零件的概述:

Length:  Parts:    0-indexed (exclusive) parts:

 3       1,1,1     0,1 & 1,2 & 2,3
 4       1,2,1     0,1 & 1,3 & 3,4
 5       2,1,2     0,2 & 2,3 & 3,5
 6       2,2,2     0,2 & 2,4 & 4,6
 7       2,3,2     0,2 & 2,5 & 5,7
 8       3,2,3     0,3 & 3,5 & 5,8
 9       3,3,3     0,3 & 3,6 & 6,9
10       3,4,3     0,3 & 3,7 & 7,10
...
  • 因为a我们从循环0(length + 1) / 3)(此值现在存储在中s);
  • 因为b我们从s到循环length / 3 * 2 +0如果length modulo-3为0;1如果length modulo-3为1或2)(此值现在存储在中s);
  • 因为c我们从循环slength

(所有三个循环都是0索引排他的)


0

Röda,82字节

f s{n=((#s+1)//3)[s[:n],s[n:#s-n],s[#s-n:]]|[chars(_)|[ord(_)-48]|sum]|[_=_,_=_1]}

在线尝试!

说明:

f s{ /* function declaration */
    n=((#s+1)//3)
    [s[:n],s[n:#s-n],s[#s-n:]]| /* split the string */
    [ /* for each of the three strings: */
        chars(_)|    /* push characters to the stream */
        [ord(_)-48]| /* convert characters to integers */
        sum          /* sum the integers, push the result to the stream */
    ]|
    [_=_,_=_1] /* take three values from the stream and compare equality */
}

0

JavaScript,129,104字节

([...n],l=n.length/3+.5|0,r=(b,e=b*-4,z=0)=>n.slice(b,e).map(x=>z-=x)&&z)=>r(0,l)==r(-l)&&r(l,-l)==r(-l)

函数r根据参数b和e对字符串进行切片,然后对数字求和并返回值。

为了切成正确的大小,我们将长度除以3并四舍五入。调用slice(0,result)给我们第一个块,slice(result,-result)给我们第二个,slice(result)给我们最后一个。由于我调用切片的方式,我使用了slice(result,4 * result)而不是最后一个,但是它给出了相同的结果。

最后,我比较结果显示值相等。

编辑:相同的原则,更好的打高尔夫球


是否可以在JavaScript中更改&&&?二手检查(&&z&&y[1]==y[2])似乎都没有修改值,因此,如果有可能,就不会影响我所看到的结果。
凯文·克鲁伊森

我会看看。&是位运算,而&&是逻辑运算,因此它将输出更改为1或0,而不是true或false,但是在这种情况下效果很好。
Grax32
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.