创建百分号


24

给定的整数Ñ ≥1,输出2D表示宽度的百分比符号的Ñ。结构如下:

  1. 创建一个由零填充的n × n矩阵(或列表列表)。
  2. 在左上角和右下角插入一个。
  3. 将它们放在从左下角到右上角的对角线上。

对于输入n = 4,此构造如下所示:

1. 4x4 matrix of 0s
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
2. 1s in TL and BR corners
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 1
3. 1s across BL-TR diagonal
1 0 0 1
0 0 1 0
0 1 0 0
1 0 0 1

这是一个,因此以字节为单位的最短程序获胜。

我使用1和0的矩阵,但是也可以使用任何非空白字符和空格的字符串。因此,上面的示例可能类似于:

#  #
  # 
 #  
#  #

要么

#     #
    #
  # 
#     #

测试用例

n
output

1
1

2
1 1
1 1

3
1 0 1
0 1 0
1 0 1

4
1 0 0 1
0 0 1 0
0 1 0 0
1 0 0 1

10
1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1

最后说明

添加一个解释将不胜感激。


我们的解决方案可以零索引吗?
Kritixi Lithos'7

5
@Cowsquack我不会。您收到的是宽度,而不是索引。
科纳·奥布莱恩

我们可以输出列表清单吗?
xnor

@xnor是的;列表列表和矩阵在我的帖子中是同义词。我将其添加到问题中
Conor O'Brien

请注意,这是'1'+'0'*(n-2)插入了空格
CalculatorFeline

Answers:


8

果冻,6个字节

²Rm’Ṭs

在线尝试!

怎么运行的

²Rm’Ṭs  Main link. Argument: n

²       Square; yield n².
 R      Range; yield [1, ..., n²].
   ’    Decrement; yield n-1.
  m     Modular; yield every (n-1)-th element of the range, staring with the first.
    Ṭ   Untruth; yield a Boolean array with 1's at the specified indices.
     s  Split the resulting array into chunks of length n, creating a matrix.

另外,²Ḷ%’¬s或者+þ%’=2
ETHproductions'Jul

²Ḷọ’s太近了……
丹尼斯

如果只有一个1字节的“ x可以被y整除”链接...
ETHproductions

@ETHproductions有,ḍ@但这是两个字节。
暴民埃里克(Erik the Outgolfer)'17年

我以为我很聪明⁼þµ+1¦Ṫṁ³UG……直到丹尼斯²出现了某种解决方案。
暴民埃里克(Erik the Outgolfer)'17年

11

JavaScript(ES6),52个字节

n=>[...Array(n)].map((_,y,a)=>a.map(_=>y++%~-n<1|0))

7

V,15字节

Àé ÀÄ|r#L.|ò.kl

在线尝试!

说明

Àé<space>        " Argument times insert a space
ÀÄ               " Argument times duplicate this line
                 " This gives an arg-by-arg matrix of spaces
                 "  and brings the cursor to the end of the first line
|r#              " Go to the beginning of this line and replace the first character with #
L.               " Go to the end of this matrix (bottom-right corner) and replace that character with a #
|                " Go to the beginning of the last line
ò                " Recursively do:
 .               "  Repeat the last action, r#, replace the character under the cursor with #
 kl              "  Go 1 up and 1 right


5

GNU APL,17个 15字节

{1=⍵∨⍵⍵⍴1=⍳⍵-1}

这是不可思议的一天... GNU实际上击败了Dyalog APL ...哇。

TIO不支持GNU APL ...

说明(输入为):

1=⍳⍵-1 - 1 followed by ⍵-2 0's
⍵⍵⍴    - fit into a square
⍵∨     - gcd ⍵ (0 gcd n = n)
1=     - test each element for equality with 1


那里...
扎卡里

真不敢相信我实际上必须破解我的旧GNU APL,哇。
扎卡里

并接受!
扎卡里

噢,我将从1=⍵∨
汲取

5

Python 2,46个字节

lambda n:zip(*[iter(`10L**n`[:-3]*-~n+'1')]*n)

在线尝试!

输出像

[('1', '0', '0', '1'), ('0', '0', '1', '0'), ('0', '1', '0', '0'), ('1', '0', '0', '1')]

Python 2,48个字节

lambda n:zip(*[iter([1]+(n*[0]+[1])[2:]*-~n)]*n)

在线尝试!

输出像

[(1, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), (1, 0, 0, 1)]

Python 3,48个字节

lambda n:('%d'*n+'\n')*n%(1,*(*[0]*n,1)[2:]*-~n)

在线尝试!

Python 3中完全不同的字符串替换方法。输出如下:

1001
0010
0100
1001

你不能做10L 10吗?
扎卡里

@Zacharý我依靠的L是末尾总是有一个,因此我可以从大号和小号的末尾切掉相同数量的字符。
xnor

哦,对不起,我误以为您只使用它作为电话号码。我从不知道10并且10L与众不同。
扎卡里

4

果冻,9个字节

=þ¹UF1Q¦s

在线尝试!

怎么运行的

=þ¹UF1Q¦s  Main link. Argument: n

  ¹        Identity; yield n.
=þ         Equals table; compare each i in [1, ..., n] with each j in [1, ..., n].
           This yields the n×n identity matrix.
   U       Upend; reverse each row.
    F      Flatten the matrix.
       ¦   Sparse application:
      Q        Unique; yield the unique elements of the constructed array, i.e.,
               [1] if n = 1 and [0, 1] if n > 1.
     1         Yield 1.
           This replaces the elements at indices 0 (last) and 1 (first) with 1.
        s  Split the resulting array into chunks of length n.

4

APL(Dyalog),18字节

{⍵=1:⍵⋄⍵ ⍵⍴1=⍳⍵-1}

在线尝试!

使输入1正常工作已增加了6个字节。

查看测试用例4,我们看到的输出是

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

这基本上是整个矩阵重复的1 0 0。换句话说,1 0 0以4×4矩阵整形。因此,在此解决方案中,我们首先使用生成带有1且尾随0的矢量,1=⍳⍵-1然后使用对其进行整形⍵ ⍵⍴。但这对于输入1来说很麻烦,因此我们需要创建一个条件并获取6个字节...

{⍵=1:⍵⋄⍵ ⍵⍴1=⍳⍵-1}    The right argument is 
 ⍵=1:⍵                 If  is 1 return itself
                      Otherwise
 ⍳⍵-1                   Create a range 1 .. ⍵-1
 1=                     Equals 1; 1 0 0 {⍵-2 0's} ...
 ⍵ ⍵⍴                   Shape in a ⍵-by-⍵ matrix

4

Haskell,55个字节

最初,我的方法是递归地生成转置的单位矩阵,但随后固定第一行和最后一行需要一些难看/冗长的区分大小写。因此,我寻找了另一种生成身份矩阵的方法,这就是我找到这个想法的方法。

f n=[[sum[1|x+y`elem`[2,n+1,2*n]]|y<-[1..n]]|x<-[1..n]]

在线尝试!

说明

[[x+y|y<-[1..n]]|x<-[1..n]]

生成此矩阵(用于n=4):

[2,3,4,5]
[3,4,5,6]
[4,5,6,7]
[5,6,7,8]

如您所见,左上角的元素是2(通常),所有对角线元素是5(通常n+1),右下角元素是8(通常2*n)。因此,我们需要做的只是检查是否x+y是的元素[2,n+1,2*n]


4

R54 42字节

-12个字节感谢Jarko Dubbeldam

n=scan();m=diag(n)[,n:1];m[1,1]=m[n,n]=1;m

返回一个矩阵;从标准输入读取。创建一个单位矩阵diag(n),将其上下翻转[,n:1],设置左上角和右下角的宽度1,然后''用width 写入控制台()n

在线尝试!


您可以输出矩阵,因此可以通过将其转换为函数(pryr::f)来节省一些字节。
JAD

@JarkoDubbeldam我可以,但是后来我认为我必须将语言更改为,R+pryr所以我认为这是一种独立的语言。您可以自由提交!然后,您可以使用Cows quack的答案中的想法,我认为在这种情况下,该想法甚至会比这短(1线)。
朱塞佩

嗯,我不确定老实说该划清界限。您会考虑使用任何其他语言的图书馆吗?
JAD

1
此外,使用function(n)很可能仍然是短
JAD

1
它比您引用的oneliner实现要短:function(n)matrix(rep(c(1,rep(0,n-2)),n+1),n,n)
JAD

4

MATL,7个字节

XyPl5L(

MATL在线上尝试一下

说明

创建单位矩阵(Xy),垂直翻转(P),将(值1(l)写到第一个和最后一个条目(5L),即左上角和右下角。


4

Dyalog APL,12 11 10字节

,⍨⍴×,2↓⊢↑×

在线尝试

-1个字节感谢lstefano。

怎么样?

,⍨⍴×,2↓⊢↑×
       ⊢↑× - argument-length extension of the sign of the argument (1)
     2↓    - Drop the first two elements
   ×,      - Prepend a one
,⍨⍴        - Shape into a square array with dimensions of input x input

我真的不认为这可以打高尔夫球了...哇。
扎卡里

它可以:,⍨⍴×,2↓⊢↑×(10个字节)。我很想补充一点:不要使用太多的通勤... :-P
lstefano


你一定是在跟我开玩笑,哇。很好滥用信号。
扎卡里

3

C#(.NET Core)121 91 88字节

-30个字节,因为旧方法很愚蠢。

通过变量初始化移动-3个字节

n=>{int i=0,k=n-1;int[,]b=new int[n,n];b[0,0]=b[k,k]=1;for(;i<n;)b[i++,k--]=1;return b;}

在线尝试!

循环向下遍历数组以填充1。返回1和0的数组。


声明bvar保存一些字节。
TheLethalCoder

3

05AB1E14 11 7字节

n<ÝI<Öô

在线尝试!

说明

n<Ý      # push range [0 ... n^2-1]
   I<Ö   # check each for equality to 0 when modulus with n-1 is taken
      ô  # split in pieces of size n

3

木炭14 12 7字节

-5字节感谢尼尔

↗N⸿/‖O↘

在线尝试!


我认为这不会再短了……
暴民埃里克(Erik the Outgolfer)'17

1
好吧,首先我将其修剪为Nν◨/ν←↙ν‖O↘,但是后来我想到了↗N⸿/‖O↘
尼尔

@Neil哇,我什至不知道该怎么办⸿。是否重置为原始位置?
notjagan

不,⸿就像这样,它向下移动了一行,但是它总是到达列0(按衡量),而不是字符串开头的列,因此J⁵¦⁵⸿与相同J⁰¦⁶
尼尔

3

C ++,144个字节

#include<string>
#define S std::string
S p(int n){S r;for(int i=0;i<n;++i){r+=S(n,32);r[r.size()-1-i]=35;r+=10;}r[0]=r[r.size()-2]=35;return r;}

它利用#和35之间的一个字节差异


其中究竟你的代码利用的一个字节的差异'#'35
扎卡里

@Zacharý似乎是在我的IDE中x)
HatsuPointerKun

2

Mathematica,72个字节

(s=Table[0,#,#];s[[1,1]]=s[[#,#]]=1;Table[s[[#+1-i,i]]=1,{i,#}];Grid@s)&

输入

[5]

输出

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


1
该问题不会要求您打印/显示它,因此可以替换Grid@ss以节省5个字节。
Mark S.


2

PowerShell,67字节

param($n)0..--$n|%{-join(("1"+"0"*(($n-1),0)[!$n])*3)[$_..($_+$n)]}

在线尝试!

接受输入$n并从循环0--$n(即$n递减)。每次迭代时,我们构造一个字符串,1后跟$n-1 0s,然后将其相乘3(例如,100010001000输入5)。然后,我们从0到轮流索引到该索引0 + $n。这些字符被-join编入字符串,并留在管道中。输出是隐式的。


(注意-这需要额外的9个字节来处理的特殊情况n=1。如果可以保证,下面是58个字节的代码n>1

param($n)0..--$n|%{-join(("1"+"0"*($n-1))*3)[$_..($_+$n)]}

2

Dyalog APL v16,23个字节

{(1@(1 1)(⍵ ⍵))⌽∘.=⍨⍳⍵}

在线尝试!

说明:

{(1@(1 1)(⍵ ⍵))⌽∘.=⍨⍳⍵} -(input ⍵) 
                ∘.=⍨⍳⍵  - identity matrix with size ⍵×⍵
               ⌽        - flip that
 (1@(1 1)(⍵ ⍵))         - place 1 into the corners using the v16 operator @ (At)

2

Lua,117个字节

m=arg[1]+0 for y=m,1,-1 do s=""for x=1,m do s=s..((x==1 and y==m or x==m and y==1 or x==y)and"#"or" ")end print(s)end

试试吧

代码很简单。它将m设置为第一个参数,然后向其添加0以将其转换为数字,然后向后迭代以获取Y坐标,向前遍历X坐标,如果x == y或其他角则将放置#。

该程序从不使用关键字“ if”。



2

Japt,12个字节

²ovUÉ hT1 òU

返回二维数组/矩阵。

在线尝试!使用该-Q标志显示数组格式的输出。

说明

²ovUÉ hT1 òU

隐式:U=输入整数

²o

平方U²),创建数组[0, U*U)o),然后通过...映射每个项目

vUÉ

1如果它可以被(v)整除U-1),否则为0

hT1

h索引为0(T)的项目()设置为1

òU

将数组拆分ò为length的slices()U


我不认为您实际上需要hT1,因为0从技术上讲U,每个人都可以将其整除U。除此之外,做得还不错:-)
ETHproductions'Jul

@ETHproductions添加来处理的输入1。没有它,它会返回,[[0]]因为显然零不能被零整除。
贾斯汀·马里纳

啊,晃来晃去 我不知道我是否应该解决这个问题……
ETHproductions

2

PHP,53字节

for(;$i<$l*$l;)echo($i++%($l-1)?0:1).($i%$l?'':"\n");

矩阵边的长度是 $l。这段代码有PHP声明,甚至有PHP警告,当时被0 $l=0除。


似乎您希望将输入存储在预定义变量(-> $l)中。不幸的是,这不是我们接受输入的方法之一。在链接的元文章中,您将找到替代方法,例如,使用ricdesi的answer所示的命令行参数。
nimi

完成并打高尔夫球:while($i**.5<$n=$argn)echo$i++%~-$n?0:1,"\n"[$i%$n];while($i**.5<$n=$argn)echo+!($i++%~-$n),"\n"[$i%$n];(每个52字节)
Titus

<?一开始就需要。
manassehkatz-恢复莫妮卡


2

Ruby,47个字节

->n{([1]+[0]*(n-2)).cycle.each_slice(n).take n}

它返回一个数组数组。

该代码非常简单。

  • 它创建一个n-11作为第一个元素,其余以0s 填充的数组(例如[1, 0, 0, 0]
  • 重复一遍
  • 它需要n切片的n元素

在线尝试!



2

Python 3,97个字节

def f(n):
    m=[[0+(j==n-i-1)for j in range(n)]for i in range(n)]
    m[0][0]=1
    m[-1]=m[0]
    return m

说明

m=[[0+(j==n-i-1)for j in range(n)]for i in range(n)]

这是一个列表理解,它0+(j==n-i-1)是转换j==n-i-1为int 的较短方法(与int函数相对),然后m[-1]=m[0]比使右下1短,因为顶部和底部的行是相同的。


2

第四名273(无评论) 170(高尔夫)

: % 2 base ! cr dup 1- 1 swap lshift 1 or . cr 2 over 2 - dup 0< 0= if
0 ?do 2dup s>d rot <# 0 ?do # loop #> type cr 2*  loop
1 or . else drop drop then cr drop decimal ;

(273版本以澄清评论版本:)

: newbase
 base @ swap base ! ;
: 0u.r
 swap s>d rot <# 0 ?do # loop #> type ;
: frame
 1- 1 swap lshift 1 or ;
: %
 2 newbase swap
 cr dup frame . cr
 2 over 2 -
 dup 0< 0= if
  0 ?do
   2dup swap 0u.r cr
   2* 
  loop
  1 or .
 else
  drop drop
 then
cr
drop base ! ;

(请注意,由于空格是Forth中的主要定界符,因此删除每个回车符不会有任何区别。缩进当然可以。)

(评论:)

( Uses bit array, max 64 width on AMD64 with gforth. )

( Could shave an extra thirty or so bytes by not restoring )
( the numeric base, )
( and a few more by pulling frame and 0u.r into the definition. )

: newbase ( n -- oldbase )  ( swap base with n )
 base @ swap base ! ;

: 0u.r ( u width -- )  ( unsigned numeric output, no leading zero suppression )
 swap s>d rot <# 0 ?do # loop #> type ;

: frame ( n -- f )  ( frame )
 1- 1 swap lshift 1 or ;

: %  ( n -- )  ( Make the % sign )
 2 newbase swap ( Use binary output. )
 cr dup frame . cr ( Frame the first line. )
 2 over 2 -
 dup 0< 0= if ( Are we already done? )
  0 ?do ( Loop doesn't do the first or last. )
   2dup swap 0u.r cr ( Zero fill, right justify. )
   2* 
  loop
  1 or . ( Put the second frame out. )
 else
  drop drop
 then
cr
drop base ! ;

(执行示例:)

1 % 
1 

 ok
2 % 
11 
11 
 ok
3 % 
101 
010
101 
 ok
10 % 
1000000001 
0000000010
0000000100
0000001000
0000010000
0000100000
0001000000
0010000000
0100000000
1000000001 
 ok
40 % 
1000000000000000000000000000000000000001 
0000000000000000000000000000000000000010
0000000000000000000000000000000000000100
0000000000000000000000000000000000001000
0000000000000000000000000000000000010000
0000000000000000000000000000000000100000
0000000000000000000000000000000001000000
0000000000000000000000000000000010000000
0000000000000000000000000000000100000000
0000000000000000000000000000001000000000
0000000000000000000000000000010000000000
0000000000000000000000000000100000000000
0000000000000000000000000001000000000000
0000000000000000000000000010000000000000
0000000000000000000000000100000000000000
0000000000000000000000001000000000000000
0000000000000000000000010000000000000000
0000000000000000000000100000000000000000
0000000000000000000001000000000000000000
0000000000000000000010000000000000000000
0000000000000000000100000000000000000000
0000000000000000001000000000000000000000
0000000000000000010000000000000000000000
0000000000000000100000000000000000000000
0000000000000001000000000000000000000000
0000000000000010000000000000000000000000
0000000000000100000000000000000000000000
0000000000001000000000000000000000000000
0000000000010000000000000000000000000000
0000000000100000000000000000000000000000
0000000001000000000000000000000000000000
0000000010000000000000000000000000000000
0000000100000000000000000000000000000000
0000001000000000000000000000000000000000
0000010000000000000000000000000000000000
0000100000000000000000000000000000000000
0001000000000000000000000000000000000000
0010000000000000000000000000000000000000
0100000000000000000000000000000000000000
1000000000000000000000000000000000000001 
 ok

(最后的注释:比Forth解释器的位宽小一。我在gforth的AMD64上运行了上述内容。一个古老的16位Forth只能达到15位宽,并且需要进行一些修改。)


如果您想在答案中添加注释的代码,那很好,但是您也确实需要在某个地方打高尔夫球。
帕维尔

@凤凰谢谢。做完了
乔尔·里斯

2

C#(.NET Core),65字节

w=>{var l=new int[w*w];for(int i=0;i<w*w;i+=w-1)l[i]=1;return l;}

在线尝试!

该算法与其他C#答案明显不同,因此我决定将其单独发布,而不是作为改进。实际上,受到最高评价的Jelly答案的启发,我之前所做的事情并不那么紧凑。输出是线性数组,因此需要一些逻辑将其按原样包装到2D外部。备用版本需要6个额外的字节才能输出为真正的2D数组:

w=>{var l=new int[w,w];for(int i=0;i<w*w;i+=w-1)l[i/w,i%w]=1;return l;}

我也有一个有趣的非竞争版本。

using System.Linq;w=>new int[w*w].Select((_,i)=>i%(w-1)<1)

最终得到几乎正确的输出,结果是IEnumerable<bool>true / false而不是1/0,它是线性而不是2D结构,尽管对于该确切的代码行并不需要,但是using System.Collections.Generic对输出。就像我说的,这非常接近有效,但还不完全有效。


对于第二种在?1:0工作中使用三元数的方法,我相信结果的数组应该很好。使用该代码的集合也不是必需的。
TheLethalCoder

首先,设置w*w为变量并将int声明移出循环是否可以为您节省任何费用?
TheLethalCoder

@TheLethalCoder w*w用单个字符变量替换两个实例节省4个字节,int i=0在循环外移动需要一个分号(占1个字节),然后添加,s=w*w到声明中占6个字节,因此它实际上占+3个字节。
卡米尔·德拉科里

您应该使用完整的2D表示解决方案的字节数。较短的解决方案返回的数组至少需要包含某种分隔符才能有效。
雅各布
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.