Language agnostic 高尔夫代码:之字形模式扫描 挑战
按字符计数的最短代码,它接受单个输入整数Language agnostic 高尔夫代码:之字形模式扫描 挑战,language-agnostic,code-golf,rosetta-stone,Language Agnostic,Code Golf,Rosetta Stone,按字符计数的最短代码,它接受单个输入整数N(N>=3),并返回一个索引数组,该数组在迭代时将根据JPEG“之字形”扫描模式遍历NxN矩阵。以下是8x8矩阵的遍历示例: 例子 (中间矩阵不是输入或输出的一部分,只是输入表示的NxN矩阵的表示形式。) 笔记 结果数组的基应该适合你的语言(例如,MATLAB数组是基于1的,C++数组是基于0的)。 这是有关的 奖金 扩展您的答案,获取两个输入N和M(N,M>=3),并在NxM矩阵上执行相同的扫描。(在这种情况下,N是列数,M是行数。) 额外示例
N
(N>=3),并返回一个索引数组,该数组在迭代时将根据JPEG“之字形”扫描模式遍历N
xN
矩阵。以下是8x8矩阵的遍历示例:
例子
(中间矩阵不是输入或输出的一部分,只是输入表示的NxN矩阵的表示形式。)
笔记
- 结果数组的基应该适合你的语言(例如,MATLAB数组是基于1的,C++数组是基于0的)。
- 这是有关的
N
和M
(N,M>=3),并在N
xM
矩阵上执行相同的扫描。(在这种情况下,N
是列数,M
是行数。)
额外示例
F#,126个字符
Python,92,95,110,111,114,120,122,162,164个字符
测试:
$ echo 3 | python ./code-golf.py
0 1 3 6 4 2 5 7 8
$ echo 4 | python ./code-golf.py
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15
$ echo 4 3 | python ./code-golf.py
0 1 4 8 5 2 3 6 9 10 7 11
3
ans =
1 2 4 7 5 3 6 8 9
此解决方案很容易推广到N
xM
电路板:调整输入处理并将N*N
替换为N*M
:
N,M=map(int,raw_input().split())
for a in sorted((p%N+p/N,(p%N,p/N)[(p%N-p/N)%2],p)for p in range(N*M)):print a[2],
我想有更简单/更简短的方法来阅读两个数字
测试:
$ echo 3 | python ./code-golf.py
0 1 3 6 4 2 5 7 8
$ echo 4 | python ./code-golf.py
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15
$ echo 4 3 | python ./code-golf.py
0 1 4 8 5 2 3 6 9 10 7 11
3
ans =
1 2 4 7 5 3 6 8 9
鲁比,69,89个字符
89查尔
跑
排序方法归功于doublep。MATLAB,101/116个字符
它基本上是给定答案的浓缩版本,直接在命令提示符下运行:
N=input('');i=fliplr(spdiags(fliplr(reshape(1:N*N,N,N)')));i(:,1:2:end)=flipud(i(:,1:2:end));i(i~=0)'
以及从用户处读取两个值的扩展值:
S=str2num(input('','s'));i=fliplr(spdiags(fliplr(reshape(1:prod(S),S)')));i(:,1:2:end)=flipud(i(:,1:2:end));i(i~=0)'
测试:
$ echo 3 | python ./code-golf.py
0 1 3 6 4 2 5 7 8
$ echo 4 | python ./code-golf.py
0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15
$ echo 4 3 | python ./code-golf.py
0 1 4 8 5 2 3 6 9 10 7 11
3
ans =
1 2 4 7 5 3 6 8 9
及
Ruby 137 130 138个字符
J、 13 15个字符
J、 奖金,13个字符
;Golfscript,26/30 32/36 45 59个字符
迄今为止最短的非J型解决方案:
更新排序(不要告诉其他人!)-30个字符:
~:1.*,{..1/\1%+.2%.+(@*]}$' '* #solution 1
#~:\.*,{.\/1$\%+.1&@.~if]}$' '* #solution 2
#~\:1*,{..1/\1%+.2%.+(@*]}$' '* #(bonus)
#~\:\*,{.\/1$\%+.1&@.~if]}$' '* #(bonus)
直接实现-36个字符:
~:@.*,{[.@%:|\@/:^+|^- 2%^|if]}$' '*
#~\:@*,{[.@%:|\@/:^+|^- 2%^|if]}$' '* #(bonus)
如果可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,则可以删除最后4个字符
doublep的分拣技术值得称赞
说明:
~\:@* #read input, store first number into @, multiply the two
, #put range(@^2) on the stack
{...}$ #sort array using the key in ...
" "* #join array w/ spaces
关键是:
[ #put into an array whatever is left on the stack until ]
.@%:| #store @%n on the stack, also save it as |
\@/:^ #store @/n on the stack, also save it as ^
+ #add them together. this remains on the stack.
|^- 2%^|if #if (| - ^) % 2 == 1, then put ^ on stack, else put | on stack.
] #collect them into an array
哈斯克尔117个字符
矩形变体稍长,有120个字符:
j(w,h)=concatMap(\q->d q++(reverse.d$q+1))[0,2..w+h]
where d r=[x+w*(r-x)|x<-[0..r],x<w&&(r-x)<h]
main=readLn>>=print.j
答案都是基于0的,并作为列表返回(Haskell的自然形式)。Perl 102个字符
<>=~/ /;print map{$_%1e6," "}sort map{($x=($%=$_%$`)+($/=int$_/$`))*1e9+($x%2?$/:$%)*1e6+$_}0..$'*$`-1
C89(280字节)
我想这仍然可以优化-我使用四个数组来存储可能的移动
撞击墙壁时的矢量。我想这是可以做到的,在定义上节省了一些字符,但我认为进一步实现逻辑的成本会更高。不管怎样,给你:
t,l,b,r,i,v,n;main(int c,char**a){n=atoi(*++a);b=n%2;int T[]={n-1,1},L[]={1-n,n}
,B[]={1-n,1},R[]={n-1,n};for(c=n*n;c--;){printf("%d%c",i,c?32:10);if(i>=n*(n-1))
v=B[b=!b];else if(i%n>n-2){if(!(n%2)&&i<n)goto g;v=R[r=!r];}else if(i<n)g:v=T[t=
!t];else if(!(i%n))v=L[l=!l];i+=v;}}
t,l,b,r,i,v,n;main(intc,char**a){n=atoi(*++a);b=n%2;intt[]={n-1,1},L[]={1-n,n}
,B[]={1-n,1},R[]={n-1,n};对于(c=n*n;c--){printf(“%d%c”,i,c?32:10);如果(i>=n*(n-1))
v=B[B=!B];else if(i%n>n-2){if(!(n%2)&&i[x;y].[i%2]
而不是如果我%2=0,那么x或者y
-很好,必须记住一个-道具到@doublepOooh,这是一个很好的技巧。我认为一个也可以在PowerShell中工作。我将以…开始J池,少于7个字符。@Mark:到目前为止,我最好的是15个字符。我很确定7对于这个问题是不可能的,但我很想被证明是wrong.为什么我花了一天的时间编写代码,然后又花了3个多小时编写代码?=PI我不熟悉Python语法,但这是如何打印以空格分隔的数字的?@Adam逗号会这样做,并且还会在末尾写一个“\n”字符,除非print语句以逗号结尾“您可以使用‘split()’而不是‘replace())“@David:不,他不能。他会留下一个两个字符串的数组。他需要一个两个整数的数组,这就是他使用eval+replace的原因。你可以使用split,然后将每个数组元素强制转换为一个整数,但我想这样会使用更多字符。@wallacolo:NN,M=input()
(python 2.x)怎么样?n=eval*$@matyr:p
打印[0,1,3,…]
而不是0,1,3…
@matyr,谢谢。使其成为69。eval*$<在我的系统上不起作用。@Sebastian“索引数组”不表示空格分隔。@Adamputs
->$>你应该使用puts[array]*''
以匹配问题中的输出。我喜欢用两个字符来完成'应用于的交替对角线'。是的,内置的'交替对角线'使这成为可能。我正在研究一个不使用该选项的goflscript解决方案。将看到它有多短be@Nabb:你确定更新后的排序有效吗?我尝试过,但它提供di对于我不熟悉Golfscript的测试值,Different outputoutput对于平方问题和红利问题都是完全相同的,但是(a+b)%2
等于(a-b)%2
。如果它有助于保存一些字符,您可能可以将
和^
的和加成2。
;<@|.`</.i.|.
;<@|.`</.i.|. 3 4
0 1 3 6 4 2 5 7 9 10 8 11
;<@|.`</.i.|. 9 6
0 1 9 18 10 2 3 11 19 27 36 28 20 12 4 5 13 21 29 37 45 46 38 30 22 14 6 7 15 23 31 39 47 48 40 32 24 16 8 17 25 33 41 49 50 42 34 26 35 43 51 52 44 53
~:1.*,{..1/\1%+.2%.+(@*]}$' '* #solution 1
#~:\.*,{.\/1$\%+.1&@.~if]}$' '* #solution 2
#~\:1*,{..1/\1%+.2%.+(@*]}$' '* #(bonus)
#~\:\*,{.\/1$\%+.1&@.~if]}$' '* #(bonus)
~:@.*,{[.@%:|\@/:^+|^- 2%^|if]}$' '*
#~\:@*,{[.@%:|\@/:^+|^- 2%^|if]}$' '* #(bonus)
~\:@* #read input, store first number into @, multiply the two
, #put range(@^2) on the stack
{...}$ #sort array using the key in ...
" "* #join array w/ spaces
[ #put into an array whatever is left on the stack until ]
.@%:| #store @%n on the stack, also save it as |
\@/:^ #store @/n on the stack, also save it as ^
+ #add them together. this remains on the stack.
|^- 2%^|if #if (| - ^) % 2 == 1, then put ^ on stack, else put | on stack.
] #collect them into an array
i s=concatMap(\q->d q++(reverse.d$q+1))[0,2..s+s]
where d r=[x+s*(r-x)|x<-[0..r],x<s&&(r-x)<s]
main=readLn>>=print.i
$ echo 3 | ./Diagonals
[0,1,3,6,4,2,5,7,8]
$ echo 4 | ./Diagonals
[0,1,4,8,5,2,3,6,9,12,13,10,7,11,14,15]
j(w,h)=concatMap(\q->d q++(reverse.d$q+1))[0,2..w+h]
where d r=[x+w*(r-x)|x<-[0..r],x<w&&(r-x)<h]
main=readLn>>=print.j
$ echo '(4,3)' | ./Diagonals
[0,1,4,8,5,2,3,6,9,10,7,11]
$ echo '(3,4)' | ./Diagonals
[0,1,3,6,4,2,5,7,9,10,8,11]
<>=~/ /;print map{$_%1e6," "}sort map{($x=($%=$_%$`)+($/=int$_/$`))*1e9+($x%2?$/:$%)*1e6+$_}0..$'*$`-1
echo 3 4 | perl zigzag.pl
0 1 3 6 4 2 5 7 9 10 8 11
t,l,b,r,i,v,n;main(int c,char**a){n=atoi(*++a);b=n%2;int T[]={n-1,1},L[]={1-n,n}
,B[]={1-n,1},R[]={n-1,n};for(c=n*n;c--;){printf("%d%c",i,c?32:10);if(i>=n*(n-1))
v=B[b=!b];else if(i%n>n-2){if(!(n%2)&&i<n)goto g;v=R[r=!r];}else if(i<n)g:v=T[t=
!t];else if(!(i%n))v=L[l=!l];i+=v;}}