打印块对角矩阵


27

这是一个简单的,咬合大小(字节大小?)的代码高尔夫球:给定一个小于10的正整数的非空列表,打印一个块对角矩阵,该列表按顺序指定块的大小。块必须由小于10的正整数组成。因此,如果给出输入

[5 1 1 2 3 1]

例如,您的输出可能是

1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 1

要么

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

或类似的东西。矩阵中的元素必须由(单个)空格分隔,行由(单个)换行分隔。任何行上都不能有前导或尾随空格。您可能会或可能不会打印尾随换行符。

您可以编写函数或程序,通过STDIN(或最接近的替代方法),命令行参数或函数参数以任何方便的字符串或列表格式(只要不经过预处理)输入。但是,与从函数返回的结果相反,必须将结果打印到STDOUT(或最接近的替代品)。

您不得使用旨在创建块对角矩阵的任何内置函数。

这是代码高尔夫球,因此最短的提交(以字节为单位)获胜。

排行榜

我希望基于数组的语言(例如J和APL)在这里能占优势,但是我不希望这样会阻止人们尝试以他们选择的语言来尽力而为。因此,这里有一个堆栈代码片段,可按语言生成常规排行榜和获胜者概述。那么,为什么不尝试在后者上抢占一席呢?

为确保您的答案显示出来,请使用以下Markdown模板以标题开头。

# Language Name, N bytes

N您提交的文件大小在哪里。如果您提高了分数,则可以通过打败旧分数保持标题。例如:

# Ruby, <s>104</s> <s>101</s> 96 bytes

Answers:


20

J,7个字节

感谢FUZxxl对2字节的改进。

基于数组的语言具有巨大的优势,因此应该在另一场比赛中算作它们。:)

   =/~@#<\

   (=/~@#<\) 3 1 1 2
1 1 1 0 0 0 0
1 1 1 0 0 0 0
1 1 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 1
0 0 0 0 0 1 1

另一种7字节方法:

#]=@##\

旧版本的说明([:=/~]#<\)

第一步是n为每个列表元素生成类似的东西(例如数字)n。这些应该不同于其他要素。例如,使用自然数3 1 1 2变为0 0 0 1 2 3 3

为了节省字节,我们使用列表的带框前缀:

   ]#<\ 3 1 1 2
┌─┬─┬─┬───┬─────┬───────┬───────┐
│3│3│3│3 1│3 1 1│3 1 1 2│3 1 1 2│
└─┴─┴─┴───┴─────┴───────┴───────┘

使用=/~动词,我们创建这些带框前缀的笛卡尔乘积表,1如果两个条目相等,0则每个单元格为。


2
我不认为括号会影响[:=/~]#<\ 您的得分。同样,=/~@#<\ 要减少两个额外的字节。
FUZxxl 2015年

“但是,必须将结果打印到STDOUT(或最接近的替代品)上,而不是从函数中返回,例如。” 您可能需要显式输入(因此它不仅仅是一个函数)或显式输出。
marinus'2

如果表达式未绑定到变量,则@marinus J将表达式的结果打印到stdout。
FUZxxl 2015年

@FUZxxl:是的,但(=/~@#<\)仅仅是一个功能。您实际上必须将其应用于某个对象以获取表达式,因此您需要显式输入(".1!:1[1),或者如果您要提交一个函数,则该函数应实际打印该值,而不仅仅是返回它(如like echo@或类似的东西) )。
marinus

=/~&I.­­­­­­­
ngn

11

APL,10

∘.=⍨∆/⍋∆←⎕

例:

      ∘.=⍨∆/⍋∆←⎕
⎕:
      5 1 1 2 3 1 
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 1

说明:

  • ∆←⎕:读取输入,存储在中
  • ⍋∆:查找排序的排列(这为输入中的每个值提供唯一的值)
  • ∆/:对于每个唯一值,重复N一次,其中N输入中的对应值是
  • ∘.=⍨:创建一个矩阵,将列表中的每个值与其他值进行比较。

要在上进行测试http://tryapl.org,您可能需要使用dfn {∘.=⍨⍵/⍋⍵},因为该站点会过滤的所有使用⎕IO和之类的东西除外)。
FUZxxl 2015年

1
@FUZxxl:它说:“但是,必须将结果打印到STDOUT(或最接近的替代品),而不是从函数返回,例如。”,因此{∘.=⍨⍵/⍋⍵}无效。您需要{⎕←∘.=⍨⍵/⍋⍵},它不仅需要花费两个字符,而且在TryAPL上仍然无法使用。(通常来说,TryAPL太受限制而无法使用。)
marinus

如果必须打印出结果⎕←,即使没有dfn,您是否也仍然需要?
FUZxxl 2015年

@FUZxxl:否,如果不执行其他操作,则自动输出表达式的输出。
marinus

知道了 我的印象是,只有当您以交互方式使用解释器时,才会发生这种情况。
FUZxxl 2015年

8

R,69 63

function(x)write(+outer(i<-rep(1:length(x),x),i,"=="),1,sum(x))

测试用例:

(function(x)write(+outer(i<-rep(1:length(x),x),i,"=="),1,sum(x)))(c(5,1,1,3,1))
1 1 1 1 1 0 0 0 0 0 0 
1 1 1 1 1 0 0 0 0 0 0 
1 1 1 1 1 0 0 0 0 0 0 
1 1 1 1 1 0 0 0 0 0 0 
1 1 1 1 1 0 0 0 0 0 0 
0 0 0 0 0 1 0 0 0 0 0 
0 0 0 0 0 0 1 0 0 0 0 
0 0 0 0 0 0 0 1 1 1 0 
0 0 0 0 0 0 0 1 1 1 0 
0 0 0 0 0 0 0 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 1

外部函数完成了大部分工作,然后只是使输出看起来正确的一种情况-感谢@Vlo的帮助


非常好的解决方案
MickyT,2015年

很好的解决方案。从来没有想过-/+要强迫逻辑。保存一些字节function(x)write(+outer(i<-rep(1:length(x),x),i,"=="),1,sum(x))63
Vlo 2015年

6

Python 3,103 97 82 78 76字节

def P(L,n=0):k,*L=L;exec("print(*[0]*n+[1]*k+[0]*sum(L));"*k);L and P(L,n+k)

使用splat可以利用的空间分隔性质print,并进行一些递归。


6

Ruby,86 90 83个字节

我第一次打高尔夫球!

->l{n=l.reduce :+;s=0;l.map{|x|x.times{puts ([0]*s+[1]*x+[0]*(n-x-s))*" "};s+=x}}

接收一个包含整数的数组,输出预期结果:

$ (->l{n=l.reduce :+;s=0;l.map{|x|x.times{puts ([0]*s+[1]*x+[0]*(n-x-s))*" "};s+=x}}).call([5, 1, 1, 2, 3, 1])
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 1

编辑:

感谢MartinBüttner帮助我缩短了一些事情!


1
您可以保存更多字符:->(l)可以是->lmap比短each.join(" ")可以缩短为*" "
马丁·恩德

6

Matlab,60 54字节

这将是Matlab的专业IF我们可以使用内置的功能...

感谢@sanchises解决了我错过的错误。

c=0;a=input('');for A=a;v=c+1:c+A;d(v,v)=1;c=c+A;end;d

哇,直到现在,我才注意到这个js片段实际上产生了排行榜!!!我怎么从未注意到这一点???感谢您指出这一点=)
更加糟糕的

我知道,对吧?这真的很酷!
Alex A.

1
我将要发布几乎相同的答案:-)
路易斯·门多

完全相同还是有些不同?=)(来自变量名井隔开)。
flawr

对我来说太相似了,无法发布它:-)
路易斯·门多

6

Matlab,53个字节

尽管它只比另一个Matlab片段短一个字符,但我认为代码足够不同,可以提供一个新的答案:

d=[];a=input('');for A=a;v=1:A;d(end+v,end+v)=1;end;d

主要技巧当然是越界索引,但这与end用作变量相结合以使其更紧凑。


1
该死的-我花了半个小时尝试打高尔夫球,end+1:end+v以摆脱“计数器”变量,但我没有想到这种解决方案。
桑契斯2015年

确实,正如@Geobits提到的那样,匿名用户尝试进行的编辑blkdiag将违反要求。仅供参考,我无论如何都将其核心放在此处:blkdiag(A,ones(i))
Dennis Jaheruddin

4

果酱21岁

q~{T):Ta*~}%_f{f=S*N}

http://cjam.aditsu.net/上尝试

说明:

q~          read and evaluate the input array
{…}%        transform each number using the block
    T):T    increment T (initially 0)
    a*      wrap T in an array and repeat it <number> times
    ~       dump the repeated numbers so they end up in a flat array
_           duplicate the array
f{…}        for each array item and the array
    f=      compare the current item with each item, resulting in an array of 1 and 0
    S*      join with spaces
    N       add a newline

4

Python 3、79

def f(l,s=0):
 for x in l:r=[0]*sum(l);r[s:s+x]=[1]*x;s+=x;exec("print(*r);"*x)

跟踪块的最左索引as,s并使其x后面的条目为1,其中x是当前块的大小。然后打印该行x时间。需要使用Python 3 print(*r)


只是将它表示r[0]*s+[1]*x+[0]*(sum(l)-s-x),它只短了一个字符,但我仍在寻找一种更好的方法。
xnor 2015年

4

Haskell,118116字节

(#)=replicate
f i=putStr$[e#(unwords$sum h#"0"++e#"1"++sum t#"0")|(h,e:t)<-map(`splitAt`i)[0..length i-1]]>>=unlines

用法: f [2,1,1,3]

输出:

1 1 0 0 0 0 0
1 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 1 1
0 0 0 0 1 1 1
0 0 0 0 1 1 1

怎么运行的:

[0..length i-1]           for each index n of the input list i
(h,e:t)<-map(`splitAt`i)  split i at n and
                            let e be the element at index n
                            let h be the list of elements to the left of e
                            let t be the list of elements to the right of e
                          foreach triple h, e, t make a list of
sum h # "0" ++              sh copies of "0" (sh = the sum of the elements of h) followed by
e # "1" ++                  e copies of "1" followed by
sum t # "0"                 st copies of "0" (st = the sum of the elements of t)
unwords                   join those list elements with spaces inbetween
e #                       make e copies
>>=unlines                join those lists with newlines inbetween
putStr                    print

您可以通过执行来保存两个字节(h,e:t)<-map(`splitAt`i)[0..length i-1],因为nlet绑定之外没有使用它。
Zgarb 2015年

@Zgarb:很高兴找到。谢谢!
nimi

3

Pyth,23 21字节

Pyth的GitHub存储库

Ju+G*]GHQYFNJjdmsqdNJ

输入是一个整数列表,例如[3, 1, 1, 2]。在线尝试:Pyth编译器/执行器

使用与randomra的J代码非常相似的想法。代码的第一部分Ju+G*]GHQY生成n了类似的东西。对于示例输入[3, 1, 1, 2],结果如下所示:

[
 [], 
 [], 
 [], 
 [[], [], []], 
 [[], [], [], [[], [], []]], 
 [[], [], [], [[], [], []], [[], [], [], [[], [], []]]], 
 [[], [], [], [[], [], []], [[], [], [], [[], [], []]]]
]

首先是三个相同的元素,而不是一个元素,然后是一个元素,最后是两个相同的元素。

Ju+G*]GHQY
 u      QY  reduce the input Q, start with empty list G=[]
            for each H in input, replace the value of G by:
  +G*]GH       G+[G]*H
J           store the result in J

代码的第二部分是比较笛卡尔积的元素并进行打印。

FNJjdmsqdNJ
FNJ          for N in J:
     m    J     map each element d of J to
       qdN          the boolean value of d == N
      s             and convert it to an integer (0 = False, 1 = True)
   jd           print the resulting list seperated by d (=space)

3

C ++,294字节

使用的编译器-GCC 4.9.2

#include<bits/stdc++.h>
using namespace std;
#define F(a,b) for(a=0;a<b;a++)
#define V vector<int>
int n,i,j,s,o;
main(){V v;while(cin>>n)v.push_back(n),s+=n;vector<V> m(s,V(s,0));F(i,v.size()){F(j,v[i])F(n,v[i])m[j+o][n+o]=1;o+=v[i];}F(j,s){F(n,s)cout<<m[j][n]<<((n==s-1)?"":" ");cout<<"\n";}}

说明-:

#include<bits/stdc++.h>
using namespace std;
#define F(a,b) for(a=0;a<b;a++)
#define V vector<int>
int n, i, j, s, o;
/*
 n = Used to take inputs, and as an iterator after that
 i, j = Iterators
 s = sum of all the inputs
 o = offset ( used to find the location of the starting cell of the next matrix of 1's )
*/

main()
{
    V v;
    while ( cin >> n )  // Take input
    {
        v.push_back( n ), s += n;
    }

    vector<V> m( s, V( s, 0 ) ); // m is a matrix of size (s*s) with all elements initialized to 0
    F( i, v.size() )
    {
        F( j, v[i] )F( n, v[i] )m[j + o][n + o] = 1; // Assign 1 to the required cells
        o += v[i]; // Add the value of the current element to the offset
    }

    F( j, s )  // Output the matrix
    {
        F( n, s )cout << m[j][n] << ( ( n == s - 1 ) ? "" : " " ); // Prevent any trailing whitespace
        cout << "\n";
    }
}

3

K,30个字节

{"i"$,/x#',:',/'g=\:\:x#'g:<x}

基本上偷走了马里努斯的答案

k){"i"$,/x#',:',/' g=\:\:x#'g:<x}5 1 1 2 3 1
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 1

2

爪哇163

a->{int n=a.stream().mapToInt(x->x).sum(),t=0,j,k;for(int i:a){for(j=0;j++<i;System.out.println("\b"))for(k=0;k<n;)System.out.print(k>=t&k++<t+i?"1 ":"0 ");t+=i;}}

接受整数列表的使用者。

可读版本,带有样板代码:

java.util.function.Consumer<java.util.List<Integer>> c = a -> {
    int n = a.stream().mapToInt(x -> x).sum(), t = 0, j, k;
    for (int i : a) {
        for (j = 0; j++ < i; System.out.println("\b")) {
            for (k = 0; k < n;) {
                System.out.print(k >= t & k++ < t + i ? "1 " : "0 ");
            }
        }
        t += i;
    }
};

调用使用:

List list = Arrays.asList(5, 1, 1, 2, 3, 1);
c.accept(list);

2

Python 2中,163个 114字节

ni狼打了一堆。

h=input()
r=range
l=len(h)
for i in r(l):
 for k in r(h[i]):print" ".join("01"[i==j]for j in r(l)for x in r(h[j]))

3
怎么样print" ".join("01"[i==j]for j in r(l(h))for x in r(h[j]))
gnibbler 2015年

!我以为我可以做这样的事情。
KSFT 2015年

这似乎只打印每个块的一行。
xnor 2015年

@xnor你是对的;我修好了它。
KSFT 2015年

2

Python 3、74

def f(a,p=0):n=a.pop(0);exec("print(*'0'*p+'1'*n+'0'*sum(a));"*n);f(a,p+n)

这不是因为错误而终止吗?
xnor 2015年

@xnor是的!
feersum'2

1
@feersum允许吗?我没有看到有关此问题的任何元信息。马丁,您如何看待?如果允许,Sp3000通过消除and短路可以节省6个字符。
xnor 2015年

@xnor错误会终止程序还是仅终止函数调用?如果这终止了程序,那么不,我认为它是不允许的。我已经在meta上对此发表了自己的看法。我还认为,如果feersum认为这是完全合法的,那么他会在回答中说明这一点,而不是希望没人注意到。
马丁·恩德

@MartinBüttner如果我理解您的要求,请终止该程序。如果要这样做f([1,2,3]);print("Done"),则错误将在打印块矩阵后终止程序,并且无法打印“完成”。
xnor 2015年

2

的JavaScript(ES6),103 107

103个字节作为匿名函数,不计算在内F=(但您需要对此进行测试)

F=l=>alert(l.map((n,y)=>(l.map((n,x)=>Array(n).fill(x==y|0))+'\n').repeat(n))
.join('').replace(/,/g,' '))

在Firefox / FireBug控制台中测试

F([5,1,1,2,3,1])

输出量

1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 1


1

珀斯,31 30

FbQVbjd++*]0Z*b]b*]0--sQbZ)~Zb

一个非常幼稚的程序,在stdin上接受输入。这可能可以打更多;)

感谢@Jakube指出浪费的字符

在这里尝试


1

Perl,69岁

#!perl -na
$j=s/./0 x$&/ger;print+($j|$i.1x$_)=~s/\B/ /gr x($i.=0 x$_,$_)for@F

使用标准输入:

$ perl a.pl <<<"1 2 3"
1 0 0 0 0 0
0 1 1 0 0 0
0 1 1 0 0 0
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1

奇怪的是,这并没有在ideone上为我打印换行符。另外,根据这篇文章,您应该将自定义shebang编码为4个字节,而不是2个字节。您可以通过不使用shebag而是使用解释器上的标志并使用-e而不是从文件中调用代码来解决此问题(请参阅该元文章中的示例)。另外,我认为您不需要该n标志-根据perldoc,它是隐式a设置的n
马丁·恩德

该程序将重用标准输入中的eol。添加您的ideone设置后即可使用。至于字符数,我在所有答案中都采用这种方式,并且我也看到其他人也采用这种方式。我之前看过您链接的帖子,但是“将差异计算为perl -nl file.pl” 却无法理解它的含义。原始的perlgolf规则将计算连字符,但不计算空格,因此在这种情况下为3个字符。
nutki'2

@MartinBüttner,无论如何都无济于事,因为randomra的方法给出了更短的解决方案:-lna //,print join$",map$'==$_|0,@,for@,=map{(++$i)x$_}@F。顺便说一句我的Perl的版本不设置-n-a,那一定是最近才加入。
nutki'2

1

R,117 144 137 137 133 129 123字节

此刻比较详细。应该可以剃掉一些。获得了一些正确格式化字节的字节,但是节省了一些矩阵交换矩阵。

感谢Alex提供了用s替换sep和删除函数名称的提示。

完全删除阵列,并使用一系列的代表来构建每一行。

尽管被Miff击败,但他的解决方案使我意识到我可以完全放弃s =''。

function(i){s=sum(i);i=cumsum(i);b=0;for(n in 1:s){d=i[i>=n][1];cat(c(rep(0,b),rep(1,d-b),rep(0,s-d)),fill=T);if(d==n)b=d}}

和测试

> (function(i){s=sum(i);i=cumsum(i);b=0;for(n in 1:s){d=i[i>=n][1];cat(c(rep(0,b),rep(1,d-b),rep(0,s-d)),fill=T,s=' ');if(d==n)b=d}})(c(5,1,1,3,1))
1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 1
> 

使用功能参数的部分匹配可以节省2个字节。在中cat(),更改sep=为,s=因为该功能没有其他可识别的参数以“ s”开头。
Alex A.

@Alex谢谢,没有意识到这一点。仍处于学习曲线上
MickyT

您可以删除来​​另存2个f=。这为您提供了一个功能对象。如果这样做,您只需要指定f=在运行之前使用类似的方式对其进行分配。直到我看到马丁·布特纳(MartinBüttner)和露比(Ruby)合作,我才意识到在这样的比赛中这是合法的。
Alex A.

1

批次-226字节

@echo off&setLocal enableDelayedExpansion&set c=0&for %%a in (%*)do set/ac+=1&for /l %%b in (1,1,%%a)do (set l=&set d=0&for %%c in (%*)do (set/ad+=1&for /l %%d in (1,1,%%c)do if !d!==!c! (set l=!l!1)else set l=!l!0)
echo !l!)

从stdin(C:\>script.bat 5 1 1 2 3 1)中获取输入,并生成echo的输出。不幸的是,我无法在同一行上获得最后的回声,否则我可能会在整行中调用该行,cmd/von/c以避免必须长时间启用延迟扩展。

干净利落-除了烦人的工作之外,别无其他:

@echo off
setLocal enableDelayedExpansion
set c=0
for %%a in (%*) do (
    set /a c+=1
    for /l %%b in (1,1,%%a) do (
        set l=
        set d=0
        for %%c in (%*) do (
            set /a d+=1
            for /l %%d in (1,1,%%c) do if !d!==!c! (set l=!l!1) else set l=!l!0
        )
        echo !l!
    )
)

1

哈斯克尔124

(%)=replicate
d l=fst$foldr(\x(m,n)->(m>>mapM_(\_->putStrLn$unwords$n%"0"++x%"1"++(sum l-n-x)%"0")[1..x],n+x))(return(),0)l

通过mapM_和组合IO操作来产生输出foldrd应该给函数一个整数列表。


1

K(ngn / k),10个字节

{x=\:x:&x}

在线尝试!

-19感谢ngn ...让我的提交保持在哈哈以下


K(ngn / k),29个字节

{,/d#',:'-':+\[d:x,0]>\:!+/x}

在线尝试!

编辑:中断1元素输入的情况下,需要工作

edit1:现在已修复。+4个字节。嘘声


1
{x=\:x:&x}­­­
ngn

@ngn拜托...
乱写

这个挑战已经在apl室中讨论过了,我从那里知道了解决方案:) k和j在这里有一个优势,因为它们的“ where” -s(&以k或I.j表示)可以与int向量一起工作,而apl 仅可以用于布尔值。 。
ngn

1

APL(Dyalog扩展),5字节

∘.=⍨⍸

在线尝试!

APL胜利反对J和K回来了扩展域

怎么运行的

∘.=⍨⍸
       Where; convert to an array that,
          for each element n at index i, has n copies of i
∘.=⍨    Outer product by element-wise equality on self

0

STATA,155个字节

di _r(a)
forv x=1/wordcount($a){
gl b=word($a,`x')
gl c=_N+1
set ob _N+$b
forv y=$c/_N{
g d`y'=_n>=$c
}
}
forv z=1/_N{
replace d`z'=0 if d`z'>1
}
l,noo noh

我可以在某处在线测试吗?
马丁·恩德

@MartinBüttner:据我所知,由于Stata是专有的,因此没有在线编译器。SAS等语言也是如此。
Alex A.

0

果冻,7个字节

ĖŒṙ⁼þ`G

在线尝试!

与J答案相同的方法。

      G    Grid format
   ⁼þ      a table of whether or not pairs of elements are equal, from
 Œṙ        the run-length decoded
Ė          enumeration of the input,
     `     compared with itself.
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.