其他元素之和的奇偶校验


23

任务

给定正整数数组,请用其他元素之和的奇偶校验替换每个元素。该数组保证至少包含 2个元素。

定义

  • 奇偶校验:数字是奇数还是偶数。

对于数组[1,2,3,1]

  • 替换1为的奇偶校验2+3+1,即even
  • 替换21+3+1,即odd
  • 替换31+2+1,即even
  • 替换11+2+3,即even

输出: [even, odd, even, even]

输入项

正整数数组。

您可以将其作为一个适当的数组,或者作为一个由换行分隔的正整数字符串。

您可以假定数组和其中的值在您语言的处理能力之内。

输出量

两个一致值的数组,一个代表odd一个,一个代表even

您可以将其输出为两个值之间以换行符分隔的字符串。

测试用例

输入:

[1, 2, 3, 1]
[1, 2, 3, 2, 1]
[2, 2]
[100, 1001]

输出:

[even, odd, even, even]
[even, odd, even, odd, even]
[even, even]
[odd, even]

注意:您可以选择比其他其他一致的价值观oddeven

计分

这是。以字节为单位的最短答案将获胜。

适用标准漏洞

Answers:


16

果冻,3个字节

+SḂ

在线尝试!

怎么运行的

+SḂ  Main link. Argument: A (array)

 S   Compute the sum of A.
+    Add the sum to each element of A.
     Using _ (subtraction) or ^ (bitwise XOR) would also work.
  Ḃ  Bit; compute the parity of each resulting integer.

这是一个聪明的方法。
Leaky Nun

1
@LeakyNun这是其他所有人都在使用的相同方法,只是更短:P
ETHproductions'5

@ETHproductions差不多,是的。Thete仅是计算奇偶校验的许多方法……
Dennis

@ETHproductions使用加法而不是减法……
Leaky Nun

@LeakyNun是的,我的Japt回答也是如此。在果冻它会仅仅是_SḂ
ETHproductions

8

JavaScript(ES6),38 36 32字节

a=>a.map(b=>eval(a.join`+`)-b&1)

用途0偶数和1奇数。

测试

f=
a=>a.map(b=>eval(a.join`+`)-b&1)
console.log(f([1, 2, 3, 1]))
console.log(f([1, 2, 3, 2, 1]))
console.log(f([100, 1001]))


2字节折扣:c-b&1代替(c-b)%2
Leaky Nun

呸! 你打败我了!
毛茸茸的

1
我必须记住要使用eval(a.join`+`)over a.reduce((x,y)=>x+y)。那很聪明
Cyoce

8

Haskell,20个字节

f x=odd.(sum x-)<$>x

用途True为奇数值和False对偶数值。

在线尝试!

从列表的总和中减去每个元素并测试其是否为奇数。

f变为pointfree也有20个字节:map=<<(odd.).(-).sum


6

MATL5,4个字节

ts-o

在线尝试!

感谢丹尼斯,节省了一个字节!

奇数为“ 1”,偶数为“ 0”。说明:

t       % Duplicate the input
 s      % Get the sum of the input
  -     % Subtract it from the original input
   o    % Get the parity of each element

6

爱丽丝31 28字节

/O.HQ\d$K@
\i#\ /d2-&+!w?+2%

在线尝试!

只要将整数分开,输入格式就无关紧要。输出格式以换行分隔。

布局可能仍然不是最佳的,但是我还没有找到进一步缩短布局的方法。

说明

/     Reflect to SE. Switch to Ordinal.
i     Read all input as a string.
      Reflect off bottom boundary. Move to NE.
.     Duplicate the input string.
      Reflect off top boundary. Move to SE.
\     Reflect to N. Switch to Cardinal.
H     Implicitly convert the top copy of the input to the integers it
      contains and take the absolute value of the top-most one. (Taking
      the absolute value doesn't do anything, but we need the implicit
      conversion to integers.)
      The IP wraps back to the second line.
\     Reflect to SE. Switch to Ordinal.
      Immediately reflect off bottom boundary. Move to NE.
Q     Reverse the stack (this converts the integers back to strings but
      that's irrelevant). After this, we end up with all the individual
      integers on the bottom of the stack in reverse order and the other
      copy of the input string with all integers on top.
      Reflect off top boundary. Move to SE.
/     Reflect to E. Switch to Cardinal.
d     Push the stack depth, which is one more than the number of list
      elements (due to the other input string on the stack).
2-    Subtract 2.
&+    Run + that many times, which implicitly converts the second string
      to the integers it contains and then adds up all the list elements.
!     Store the sum on the tape.
w     Push the current IP position to the return address stack. This
      lets us return here repeatedly to implement a loop.

  ?+    Retrieve the input sum from the tape and add it to the current
        element.
  2%    Compute its parity. Other ways to do this are 1A and 0x. 2F would
        also work but it gives 0/2 instead of 0/1.
        The IP wraps the first column of the grid.
  \     Reflect to NE. Switch to Ordinal. The IP bounces diaginally up
        and down until it hits the next \.
  O     Implicitly convert the current parity to a string and print it
        with a trailing linefeed.
  #     Skip the next command (the H).
  \     Reflect to E. Switch to Cardinal.
  d     Push the stack depth. This is zero when we're done.
  $     Skip the next command if the stack depth is indeed zero, which
        exits the loop.

K     Jump to the return address on top of the return address stack without
      popping it (so that the next K will jump there again).

@     Terminate the program.

6

Pyth,7个 6字节

mi2-sQ

-1字节感谢@KZhang

输出1表示奇数,输出2表示偶数。

试试吧!

说明

m%-sQd2
m      Q    # For each element in the (implicit) input list
   sQ       # Take the sum of all the elements
  -  d      # subtract that element, so that we now have the sum of the other elements
 %    2     # modulo 2; 1=off, 0=even

通过将模数%_2更改为GCD i2_,您甚至可以d隐式将代码更改为mi2-sQ,从而节省一个字节。输出更改为偶数为2,奇数为1。
K张



4

R,21个字节

(sum(n<-scan())-n)%%2

从stdin读取列表,并为偶数返回0,为奇数返回1。将输入绑定到n调用内部的变量,sum而不是在外部调用,即,n=scan();(sum(n)-n)%%2

在线尝试!



3

Clojure,30个字节

#(for[i %](odd?(apply - i %)))

从每个值中依次减去所有值,例如,输入[a b c d]第二个计算值b - a - b - c - d= -(a + c + d)。输出false为偶数和true奇数。

但是您不妨使用+并计算每个后续项两次,以免影响奇偶校验。


3

CJam,10个字节

{_:+f+1f&}

这是一个匿名块(函数),它从堆栈中获取输入并将其替换为输出。

在线尝试!

说明

考虑输入[1 2 3 1]

{         e# Begin block
          e#   STACK: [1 2 3 1]
  _       e#   Duplicate
          e#   STACK: [1 2 3 1], [1 2 3 1]
  :+      e#   Fold addition over the array: compute its sum
          e#   STACK: [1 2 3 1], 7 
  f+      e#   Map addition over the array with extra parameter
          e#   STACK: [8 10 11 8]
  1       e#   Push 1
          e#   STACK: [8 10 11 8], 1
  f&      e#   Map bit-wise "and" over the array with extra parameter
          e#   STACK: [0 0 1 0]
}         e# End block



2

Japt,7个字节

£x +X&1

在线尝试!

说明

 £   x +X&1
 mX{ x +X&1}  // Ungolfed
UmX{Ux +X&1}  // Variable introduction

UmX{       }  // Replace each item in the input array with
         &1   //   the parity of
    Ux        //   the sum of the input array
       +X     //   plus X.
              // Implicit: output result of last expression

2

Perl 5,31个字节

sub{map$x+=$_,@_;map$x-$_&1,@_}

输出1为奇数和0偶数。


+1,很好。我认为这是28个字节:perldoc perlsub说:“签名是子例程主体的一部分。通常,子例程主体只是一段支撑代码。”
msh210 '17

@ msh210谢谢!我不认为这是如何工作的,尽管-子例程的主体只有28个字节,但是您sub不能不破坏它而忽略它。
克里斯(Chris)

但有时一个子程序没有工作sub后,如sortgrep或作为参数传递给另一个子程序。这可能值得在Code Golf Meta上询问。
msh210 '17

@ msh210子程序工作而不会sub仅当它是在原型函数使用(sortgrep或多或少原型)。但否则,sub是必需的。无论如何,通过省略打高尔夫球3个字节sub并不是很有趣。
Dada's

2

Clojure(脚本),36字节

输出是true奇数和false偶数。输出和输入都是序列。

(fn[l](map #(odd?(-(apply + l)%))l))

2

PHP,50字节

在线版本

1为奇数,0为偶数

输出为字符串,用 _

<?foreach($_GET as$v)echo array_sum($_GET)-$v&1,_;

PHP,72字节

输出为数组使用 array_map

<?print_r(array_map(function($v){return array_sum($_GET)-$v&1;},$_GET));

1
您可以删除?:0它不执行任何操作。foreach($_GET as$v)echo array_sum($_GET)-$v&1,_;
克里斯多夫(Christoph)

2

C,68 62字节

i;s;f(c,l)int*l;{while(i<c)s+=l[i++];while(i)l[--i]=s-l[i]&1;}

1为奇数,0为偶数

详细 在线试用

f(int c, int * l)
{
    int i = 0, s = 0;

    while(i < c)
    {
        s = s + l[i];
        i = i + 1;
    }

    // assert(i == c)

    while(i > 0)
    {
        i = i - 1;
        l[i] = s - l[i]&1;
    }
}

2

视网膜40 38字节

\d+
¶$` $'
^¶

\d+
$*
+` |11

%M`1
¶
 

在线尝试!输出1表示奇数,0表示偶数。说明:前两行为输入中的每个数字重复一次输入,但没有元素本身。这将创建一个额外的空白行,然后将其删除。然后将输入从十进制转换为一进制,删除空格并计算奇偶校验。然后将偶数奇偶校验转换为零,并将结果重新合并为一行。编辑:由于@FryAmTheEggman,节省了2个字节。我尝试了其他一些版本,这些版本在概念上更令人愉悦,但要表达的字节太多:

\d\B

T`2468O`00001
T`d`10`^([^1]*1[^1]*1)*[^1]*1[^1]*$

将所有输入更改为奇偶校验,如果总输入具有奇偶校验,则翻转所有奇偶校验。

\d+
$*
^.*
$&¶$&
 (?=.*$)

11

\B
0
T`d`10`.*¶1
¶0

对输入的副本求和,然后对所有内容进行奇偶校验,如果和为奇数,则反转奇偶校验,然后再次删除和。


我尝试了一下想到的方法,但解决方案略短一些,尽管我仍然认为这并不是最佳选择。特别是我不喜欢如何处理最后得到的零。
FryAmTheEggman'5

@FryAmTheEggman您的节省来自于将;s 转换回空格的一种不太明显的方式。如果将放在;开头,则可以通过立即删除而不是将其转换为0来保存字节
Neil

其实,再看一遍,为什么您的最后一个阶段不只是将换行符替换为空格?那会节省2个字节吗?
FryAmTheEggman'5

@FryAmTheEggman是的;我想我最初在上一个迭代中要进行多个替换。
尼尔


1

k,9个字节

{2!x-+/x}

输出为1for odd,为0for。在线尝试。

转换为伪代码,它将是:

for number in x:
    yield (number - sum(x)) % 2


1

Brain-Flak94 68 66字节

({({}<>)<>})<>{<>(({})<({}<>{}<>(())){({}[()]<([{}])>)}{}>)<>}<>{}

在线尝试!

这个任务似乎有点长。可能会有更方便的方法来执行此操作。

说明

首先,我们使用以下公式计算堆栈的总和:

({({}<>)<>})

我们遍历整个堆栈,将结果添加到每个元素并确定对

<>{<>(({})<({}<>{}<>(())){({}[()]<([{}])>)}{}>)<>}<>{}

这使用了一个很酷的mod 2算法,我针对此挑战提出了该算法。

({}(())){({}[()]<([{}]())>)}{}

这会将1递减到输入减量之下,直到每次执行1-n到我们之前放置的1 时输入都达到零为止,然后删除输入。


最后您可以修改2。您无需修改​​2的总和。
Leaky Nun

@LeakyNun谢谢!我才意识到这一点,并进行了修复。
小麦巫师

1

明智54 52字节

::^:??[:!^:?^:!^:?^?]|!::^??[!:?^:><^!:?^:!^:?^?]!&|

在线尝试!

说明

如果不花费太多字节来交换前两个元素,则此代码将短很多。当前记录是

:?^:!^:?^!

不幸的是,这构成了大部分代码。


首先我们取栈的XOR和

::^:??[:!^:?^:!^:?^?]|!

然后,我们将其与每个元素进行异或,并将最后一位清零

::^??[!:?^:><^!:?^:!^:?^?]!&|


1

AWK,64字节

{for(i=0;++i<=NF;print s[i]%2)for(j=0;++j<=NF;)if(i!=j)s[i]+=$j}

在线尝试!

输出0为偶数的总和和1奇数的款项分离由换行。唯一甚至有些开箱即用的想法就是将print命令放在for “增量”步骤中。我尝试了几种“聪明”的打印方法,但是它们没有保存字节。

如果您不想要换行符,只为咯咯地笑:

{for(i=0;++i<=NF;m=m,s[i]%2)for(j=0;++j<=NF;)if(i!=j)s[i]+=$j}1

它具有与上述相同的字节数,但是有点钝。


1

斯威夫特-55字节

终于击败了C! 另外,偶数为0,奇数为1

func g(a:[Int]){for i in a{print((a.reduce(0,+)-i)%2)}}

一个函数,用法: g(a: [1,2,3,2,1] // => 0 1 0 1 0

看看这个!


不熟悉的斯威夫特,但在许多语言中,你可以替换(x-y)%2使用x-y&1
Cyoce

@Cyoce对我来说,经过测试后,它不起作用。按位操作不是Swift的
专长

1

公理,45字节

f(a)==[(reduce(+,a)-a.j)rem 2 for j in 1..#a]

不检查输入类型,可能重新计算每个元素的总和“ a” ...测试

(27) -> [[a,f(a)] for a in [[1,2,3,1], [1,2,3,2,1], [2,2], [100, 1001] ]]
   (27)
   [[[1,2,3,1],[0,1,0,0]], [[1,2,3,2,1],[0,1,0,1,0]], [[2,2],[0,0]],
    [[100,1001],[1,0]]]

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.