产生一个XOR表


19

介绍

XOR是实现异或的数字逻辑门。大多数情况下,显示为^。二进制可能出现的四种结果:

0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

这也可以看作二进制的加模2。在十进制中,我们需要将十进制转换为二进制,35 = 100011然后25 = 11001。为了计算XOR值,我们将它们彼此叠加:

100011
 11001 ^
--------
111010  = 58 in decimal

任务:给定大于1的整数N时,输出大小为N + 1的XOR表。例如,N = 5:

 0 1 2 3 4 5
 1 0 3 2 5 4
 2 3 0 1 6 7
 3 2 1 0 7 6
 4 5 6 7 0 1
 5 4 7 6 1 0

您会看到每个数字前面都有一个空格,因为表中的最高数量长度为1。但是,如果我们使N = 9,则会得到以下网格:

  0  1  2  3  4  5  6  7  8  9
  1  0  3  2  5  4  7  6  9  8
  2  3  0  1  6  7  4  5 10 11
  3  2  1  0  7  6  5  4 11 10
  4  5  6  7  0  1  2  3 12 13
  5  4  7  6  1  0  3  2 13 12
  6  7  4  5  2  3  0  1 14 15
  7  6  5  4  3  2  1  0 15 14
  8  9 10 11 12 13 14 15  0  1
  9  8 11 10 13 12 15 14  1  0

最高值的长度为2,因此该值与长度3右对齐(最高长度+ 1)。

规则:

  • 仅在一致(或不连续)使用的情况下,前导空格不是强制性的
  • 您必须以上面显示的形式输出表格。
  • 列之间的填充应尽可能小
  • 这是,因此以最少的字节提交为准!

列之间允许多少填充?只有最小的可能量?
马丁·恩德

@MartinBüttner是的,可能的最低金额。我将其添加到描述中。
阿德南(Adnan)2015年


查看示例output an XOR table with the size N+1
edc65

N的最大值是多少?...(对于N == 1000000,表的大小将接近10 TB)
edc65

Answers:


12

MATL,10字节

0i2$:tXgZ~

尽管仍然需要改进,但编译器(尤其是该程序)现在似乎可以在Octave中工作。您可以临时使用此GitHub提交

编辑(16年3月30日)在线尝试!

>> matl 0i2$:tXgZ~
> 9
0  1  2  3  4  5  6  7  8  9
1  0  3  2  5  4  7  6  9  8
2  3  0  1  6  7  4  5 10 11
3  2  1  0  7  6  5  4 11 10
4  5  6  7  0  1  2  3 12 13
5  4  7  6  1  0  3  2 13 12
6  7  4  5  2  3  0  1 14 15
7  6  5  4  3  2  1  0 15 14
8  9 10 11 12 13 14 15  0  1
9  8 11 10 13 12 15 14  1  0

说明

0i2$:       % vector 0, 1, 2, ... up to input number
t           % duplicate
Xg          % nd-grid
Z~          % bitxor

即将在Octave中运作的编译器即将推出...
Luis Mendo

……希望如此。对这个有任何想法吗?
路易斯·门多

5
绝对是工作的正确工具。+1
意大利面条2015年

1
经过一些调整后,编译器似乎可以在Octave(4.0.0)中工作。我需要做更多的测试,但是我尝试过的所有程序(包括此程序)都可以工作。当我发布新版本时,这是指向当前GitHub提交链接,以在Octave中运行此代码。语言没有更改(仍为5.0.0版),因此它早于这项挑战
Luis Mendo

5

Bash + BSD实用工具,45

eval echo \$[{0..$1}^{0..$1}]|rs -jg1 $[$1+1]

我已经等了很长时间才能找到的用途rs。这似乎是一个很好的选择。 rs可能需要在Linux系统上安装。但是它可以在OS X上直接使用。

  • $1扩展为N,从而echo \$[{0..$1}^{0..$1}]扩展为echo $[{0..N}^{0..N}]
  • 然后eval编辑:
  • 支撑扩展到 $[0^0] $[0^1] $[0^2] ... $[0^N] ... $[N^N]
  • 这是一系列xor算术展开,扩展为一行
  • rs(重塑)将这条线重塑为N + 1行。 -j右对齐,并-g1给出1的装订线宽度。这确保最终输出表的列之间的宽度最小。

我已经测试了N = 1000,耗时3.8秒。从理论上讲,大N是可能的,尽管随着bash扩展的(N + 1)²内存使用量,bash有时会耗尽内存。


3

的JavaScript(ES6)120 122

编辑 2个字节保存的ETHproductions

匿名函数。注意:表格中的数字限制为7位数字,考虑到表格的整体大小允许更大的数字,这是合理的数字

现在,我应该找到一种较短的方法来获取最大列大小,避免对数

n=>(a=Array(n+1).fill(-~Math.log10(2<<Math.log2(n)))).map((m,i)=>a.map((z,j)=>`       ${i^j}`.slice(~m)).join``).join`
`

测试

f=n=>(a=Array(n+1).fill(-~Math.log10(2<<Math.log2(n)))).map((m,i)=>a.map((z,j)=>`       ${i^j}`.slice(~m)).join``).join`\n`

function update() {
  var v=+I.value
  O.textContent = f(v)
}  

update()
N: <input id=I type=number value=9 oninput='update()'>
<pre id=O></pre>


太棒了,喜欢使用~m来捕捉额外的空间。使用模板字符串可以节省两个字节:(z,j)=>`(7 spaces)${i^j}`.slice(~m)
ETHproductions 2015年

@ETHproductions 1)一个好的建议,谢谢2)您如何设法在2个char(96)里面放一个...(我什至不敢问)... char(96)?
edc65

啊哈,您只需使用多个反引号,如下所示:(ignore this padding) ``abc`def`` (ignore this too)出现如下:abc`def
ETHproductions 2015年

3

C,114128152

编辑受Khaled A Khunaifer工作启发而简化的空间计数

符合规格的交流功能。

T(n){int i=0,j,x=1,d=0;while(x<=n)x+=x,++d;for(;i<=n;i++)for(j=0;j<=n;j++)printf("%*d%c",d*3/10+1,i^j,j<n?32:10);}

尝试插入n作为输入,默认9

少打高尔夫球

T(n)
{
   int i=0, j, x=1,d=0;
   while(x<=n) x+=x,++d; // count the digits
   // each binary digit is approximately 0.3 decimal digit
   // this approximation is accurate enough for the task
   for(;i<=n;i++)
     for(j=0;j<=n;j++)
       printf("%*d%c",d*3/10+1,
              i^j,
              j < n ? 32:10); // space between columns, newline at end
}

3

C,103字节

Y(n){int i,j=n++;char*f="%2u";while(j/=8)++f[1];for(;j<n;++j,puts(""))for(i=0;i<n;++i)printf(f,i^j);}

1
欢迎来到编程难题和代码高尔夫球!对于我所看到的,很不幸,您的答案(与此处的许多其他答案一样)完全错过了列对齐点。
edc65

尝试使用输入5(列之间的空间过多)或输入65(过少)
edc65

直到512,最后在这里都是可以的……
Ros

3

果冻,无竞争

7个字节 该答案是非竞争性的,因为它使用了挑战后的功能。

0r©^'®G

在线尝试!

怎么运行的

0r©^'®G  Main link. Input: n (integer)

0r       Range; yield [0, ..., n].
  ©      Save the range in the register.

     ®   Yield the range from the register.
   ^'    XOR each integer in the left argument with each integer in the right one.
      G  Grid; separate rows by newlines, columns by spaces, with a fixed width for
         all columns. Since the entries are numeric, align columns to the right.

2
恭喜您获得第1000个答案:)
Adnan

3

R,38个字节

通常R仅需要很多字节即可格式化输出。在这种情况下,情况恰恰相反。outer通常指两个数组的外部乘积,当提供函数时,可以跨向量的边界执行此操作。在这种情况下,我们应用按位XOR函数bitwXor

names(x)=x=1:scan();outer(x,x,bitwXor)

2

CJam,29 27字节

ri:X),_ff{^s2X2b,#s,)Se[}N*

在这里测试。

说明

ri      e# Read input and convert to integer.
:X      e# Store in X.
),      e# Get range [0 1 ... X].
_ff{    e# Nested map over all repeated pairs from that range...
  ^     e#   XOR.
  s     e#   Convert to string.
  2     e#   Push 2.
  X2b,  e#   Get the length of the base-2 representation of X. This is the same as getting
        e#   getting the base-2 integer logarithm and incrementing it.
  #     e#   Raise 2 to that power. This rounds X up to the next power of 2.
  s,    e#   Convert to string and get length to determine column width.
  )     e#   Increment for additional padding.
  Se[   e#   Pad string of current cell from the left with spaces.
}
N*      e# Join with linefeeds.

好吧,那是很快的哈哈
Adnan 2015年

2

MathCAD,187字节

在此处输入图片说明

MathCAD可以轻松处理内置表-但绝对没有按位Xor,十进制到二进制或二进制到十进制转换器。for函数迭代可能的值。i,a2,Xa和Xb占位符。while循环会主动转换为二进制,而转换为二进制时还会执行xor功能(带有圆圈的小十字)。它将二进制数存储在以10为底的数字中,该数字由0和1组成。然后将其转换,然后再通过求和函数存储在M矩阵中。

这很容易实现(如果仅通过将占位符换成较短的占位符即可),但是我认为我会发布它,看看是否有人可以将二进制转换为十进制转换器,而不是其他任何东西。


2

k4,50个字节

{-1@" "/:'(-|//#:''x)$x:$2/:''~x=/:\:x:0b\:'!1+x;}

例如:

  {-1@" "/:'(-|//#:''x)$x:$2/:''~x=/:\:x:0b\:'!1+x;}9
 0  1  2  3  4  5  6  7  8  9
 1  0  3  2  5  4  7  6  9  8
 2  3  0  1  6  7  4  5 10 11
 3  2  1  0  7  6  5  4 11 10
 4  5  6  7  0  1  2  3 12 13
 5  4  7  6  1  0  3  2 13 12
 6  7  4  5  2  3  0  1 14 15
 7  6  5  4  3  2  1  0 15 14
 8  9 10 11 12 13 14 15  0  1
 9  8 11 10 13 12 15 14  1  0

2

C,149字节

int i=0,j,n,k=2;char b[256];scanf("%d",&n);while((k*=2)<=n);k^=k-1;while(i++<=n&&putchar(10))for(j=0;j<=n;j++)printf(" %*d",sprintf(b,"%d",k),i-1^j);
---------

详细

#include <stdio.h>

int main()
{
    int i=0, j, n, k=2;
    char b[256] = { 0 };

    scanf("%d", &n);

    // find max xor value in the table
    while((k*=2)<=n); k^=k-1;

    printf("> %d ~ %d", k, sprintf(b,"%d",k));

    while(i++ <= n && putchar(10))
    {
        for(j = 0; j <= n;j++)
        {
            printf(" %*d", sprintf(b,"%d",k), (i-1)^j);
        }
    }

    return 0;
}

1
就像您的Java帖子一样,没有尝试正确对齐列。那是挑战的唯一困难部分。此代码给出错误的输出时的输入<8或> 64
edc65

@ edc65我发现了如何对齐,最大xor值是11..1输入值中有效位的二进制二进制数n,可以通过首先找到最接近的2的幂,然后将其与前一个数字进行异或,来完成0001 xor 1110 = 1111
Khaled.K

很好,您现在处在正确的轨道上。不过,您应该进行更多测试:k的循环在n = 8时给出了错误的结果(您的简短回答并未将局部变量i初始化为0)
edc65

这个简单的循环应该做的:for(k=1;k<=n;)k*=2;k--;。现在,我看到它比我的C尝试要短得多(我的C尝试会更好,但性能在此挑战中并不重要)
edc65

@ edc65您需要2^k xor 2^k -1max{2^k<=n}2^k -1min{2^k>=n}。让所有11..1在那里
Khaled.K

2

Python 3中,133个 131字节

import math
n=int(input())
r,p=range(n+1),print
for y in r:[p(end='%%%dd '%len(str(2**int(math.log2(n)+1)-1))%(x^y))for x in r];p()

1

Mathematica,108个字节

StringRiffle[Thread[Map[ToString,a=Array[BitXor,{#,#}+1,0],{2}]~StringPadLeft~IntegerLength@Max@a],"
"," "]&

忽略该错误,只是Thread不知道它在做什么。


1

Emacs Lisp,193个字节

(defun x(n)(set'a 0)(set'n(1+ n))(while(< a n)(set'b 0)(while(< b n)(princ(format(format"%%%ss "(ceiling(log(expt 2(ceiling(log n 2)))10)))(logxor a b)))(set'b(1+ b)))(set'a(1+ a))(message"")))

取消高尔夫:

(defun x(n)
  (set'a 0)
  (set'n(1+ n))
  (while(< a n)
    (set'b 0)
    (while(< b n)
      (princ
        (format
          ;; some format string magic to get the length of the longest
          ;; possible string as a format string
          (format "%%%ss " (ceiling(log(expt 2(ceiling(log n 2)))10)))
          (logxor a b)))
      (set'b(1+ b)))
    (set'a(1+ a))
    ;; new line
    (message"")))

输出将发送到*Message*缓冲区,stdout如果x要在脚本内使用则为缓冲区。

(x 9)
 0  1  2  3  4  5  6  7  8  9 
 1  0  3  2  5  4  7  6  9  8 
 2  3  0  1  6  7  4  5 10 11 
 3  2  1  0  7  6  5  4 11 10 
 4  5  6  7  0  1  2  3 12 13 
 5  4  7  6  1  0  3  2 13 12 
 6  7  4  5  2  3  0  1 14 15 
 7  6  5  4  3  2  1  0 15 14 
 8  9 10 11 12 13 14 15  0  1 
 9  8 11 10 13 12 15 14  1  0

1

Python 2,114字节

我们花了一些时间寻找一种方法来进行宽度可变的填充.format()(有些,不是很多),并对其进行正确的调整,但是我想我现在已经掌握了所有这些。尽管可以在该宽度计算中使用更多的打高尔夫球。

N=input()+1
for i in range(N):print''.join(' {n:>{w}}'.format(w=len(`2**(len(bin(N))-2)`),n=i^r)for r in range(N))

1

CachéObjectScript,127个字节

x(n)s w=$L(2**$bitfind($factor(n),1,100,-1))+1 f i=0:1:n { w $j(i,w) } f i=1:1:n { w !,$j(i,w) f j=1:1:n { w $j($zb(i,j,6),w) } }

详细:

x(n)
 set w=$Length(2**$bitfind($factor(n),1,100,-1))+1
 for i=0:1:n {
     write $justify(i,w)
 }
 for i=1:1:n {
     write !,$justify(i,w)
     for j=1:1:n {
         write $justify($zboolean(i,j,6),w)
     }

 }

1

Pyke,8个字节(非竞争)

hD]UA.&P

说明:

         - auto-add eval_or_not_input() to the stack
h        - increment the input
 D]      - Create a list containing [inp+1, inp+1]
   U     - Create a 2d range 
    A.^  - Deeply apply the XOR function to the range
       P - print it out prettily

在这里尝试


0

Python 2,77个字节

n=input()+1
t=range(n)
for i in t: print "%3d"*n % tuple([x^i for x in t])

3
欢迎来到编程难题和代码高尔夫球!据我所知,您的答案(很遗憾,这里的许多其他答案)完全错过了列对齐点。


0

Excel VBA,95个字节

匿名VBE立即窗口功能,可接收范围输入[A1]并将其输出到控制台。

For y=0To[A1]:For x=0To[A1]:z=x Xor y:?Spc([Len(2^-Int(-Log(A1,2)))]-Len(z))Str(z);:Next:?:Next

0

小基础,499字节

一个脚本,它从TextWindow对象中获取输入并输出到相同的对象

a=TextWindow.Read()
For y=0To a
For x=0To a
n=x
b()
z1=z
n=y
b()
l=Math.Max(Array.GetItemCount(z),Array.GetItemCount(z1))
o=0
For i=1To l
If z1[i]<>z[i]And(z[i]=1Or z1[i]=1)Then
z2=1
Else 
z2=0
EndIf
o=o+z2*Math.Power(2,i-1)
EndFor
TextWindow.Write(Text.GetSubText("      ",1,Text.GetLength(Math.Power(2,Math.Ceiling(Math.Log(a)/Math.Log(2))))-Text.GetLength(o))+o+" ")
EndFor
TextWindow.WriteLine("")
EndFor
Sub b
z=0
c=0  
While n>0
c=c+1
z[c]=Math.Remainder(n,2)
n=Math.Floor(n/2)
EndWhile
EndSub

在SmallBasic.com上尝试 使用Silverlight,因此必须在IE或Edge中运行

选择黑色控制台,然后输入input integer并按Enter

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.