旋转反对角线


32

背景

在大多数合理的编程语言中,旋转2D数组的行或列非常容易。在此挑战中,您的任务是改为旋转对角线。回想一下,二维数组的反对角线是在东北方向↗上获取的一维切片。

输入值

非空矩形2D数组,以任何合理格式显示。请注意,该数组可能不是正方形。

输出量

相同的阵列,但每个反对角线向右旋转了一步。

考虑3x4输入数组

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

该数组的反对角线是

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

它们的旋转版本是

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

因此正确的输出是

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

规则和计分

您可以编写完整的程序或函数。如果您的语言允许,编写一个可以修改输入数组的函数也是可以接受的。最低字节数获胜,并且不允许出现标准漏洞。

排行榜

这是一个堆栈片段,用于按语言生成常规排行榜和获胜者概述。

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

## Language Name, N bytes

N您提交的文件大小在哪里。如果您想在标头中包含多个数字(例如,因为您的分数是两个文件的总和,或者您想分别列出解释器标志的处罚,或者您想显示您改进的旧分数),请确保实际分数是标题中的最后一个数字。

其他测试用例

Input:
4
Output:
4

Input:
5 6 1
Output:
5 6 1

Input:
0 1
6 2
Output:
0 6
1 2

Input:
6 3 0 8
4 6 1 4
6 2 3 3
9 3 0 3
Output:
6 4 6 1
3 6 2 3
0 9 3 0
8 4 3 3

Input:
5 8 2
6 7 3
2 6 1
6 0 6
6 4 1
Output:
5 6 7
8 2 6
2 6 0
3 6 4
1 6 1

Input:
9 9 4 0 6 2
2 3 2 6 4 7
1 5 9 3 1 5
0 2 6 0 4 7
Output:
9 2 3 2 6 4
9 1 5 9 3 1
4 0 2 6 0 4
0 6 2 7 5 7 

Answers:


20

果酱20

{z_)\zLa+(@+\.+s\,/}

编写为功能块。在线尝试

说明:

输入可以这样查看:

输入图

也就是说,我们将矩阵的其余部分与第一行和右列分开,并按照箭头所示的顺序考虑这些元素。

然后输出是这样的:

输出图

其余的矩形块整体沿对角线移动,并且边缘元素按照新箭头所示的顺序/位置重新排列。

该代码几乎可以做到这一点,除了输出是首先使箭头笔直向下(因此矩阵具有尾部,如字母P)生成,然后进行校正。

z      zip (transpose) the matrix
_      make a copy
)      take out the last row (right column before transposing)
\      swap with the rest of the matrix
z      transpose back
La+    append an empty row (needed for the single-column case,
        which leaves an empty matrix here)
(      take out the first row (top row without the corner)
@+     bring the right column to the top of the stack and concatenate
        obtaining an array of the edge elements (marked with the blue arrow)
\      swap with the remaining part (big white block in the diagrams)
.+     concatenate element by element
        each edge element is concatenated with a row of the white block
        after the white block runs out, the remaining elements form new rows
s      convert the whole thing to a string (concatenating all rows)
\      swap with the copy of the transposed matrix
,      get its length (number of original columns)
/      split the string into rows of that length

Pyth的答案也是20字节,但是您的答案早一些,所以我接受。
Zgarb 2015年

9

CJam,44 43 42 40字节

qN/:ReeSf.*:sz1fm<{Rz,{(S-(o\}*~]{},No}h

在这里测试。

嗯,比我的第一次尝试要好得多,但是我有一种感觉,丹尼斯无论如何都会解决这个问题的……

输入和输出为ASCII网格:

0123
4567
8901

0456
1890
2371

7
划掉

3
@TimmyD我应该一直等到宽限期结束之前,将其从47修改为43。:P
Martin Ender 2015年

是! 它已成为模因
intrepidcoder

1
我终于去了,学习了一种高尔夫语言,所以我可以将4个字节
打成

6

J,24个字符

函数采用一个参数。

$$<@(1&|.)/./:&;</.@i.@$

J有一个/.叫做Oblique的运算符。它不能反转它,因此重建并非易事,但是您可以将“倾斜列表”视为数组元素的排列。因此,我们通过将(size )的“倾斜列表”排列放在右边,将我们的新倾斜值(正确旋转)放在左边,用/:(二进位排序)反转该排列</.@i.@$。然后,我们使用good old将列表重塑为旧的矩形数组$$

   3 4$i.10
0 1 2 3
4 5 6 7
8 9 0 1
   ($$<@(1&|.)/./:&;</.@i.@$) 3 4$i.10
0 4 5 6
1 8 9 0
2 3 7 1

在线尝试。


这是峰J。做得好。
约拿书

5

J,38 30字节

@algorithmshark节省了8个字节。

{./.((}.~#),~({.~#),.])}:"1@}.   

该功能将顶部和左侧边缘收集到一个列表中,将列表切成两段足够大的尺寸,并将它们缝在核心部件的右侧和底部。

用法:

   ]input=.0 1 2 3, 4 5 6 7,: 8 9 0 1
0 1 2 3
4 5 6 7
8 9 0 1
   ({./.((}.~#),~({.~#),.])}:"1@}.) input
0 4 5 6
1 8 9 0
2 3 7 1

在这里在线尝试。


1
减少到30个字符:{./.替换}:@{.,{:"1,您可以通过左右翻转火车来节省两个波浪号{./.((}.~#),~({.~#),.])}:"1@}.
algorithmhark

4

利亚,153个 149 139字节

A->(length(A)>1&&((m,n)=size(A);r(X)=for i=1:n X[:,i]=reverse(X[:,i])end;r(A);for i=-m:m A[diagind(A,i)]=circshift(diag(A,i),1)end;r(A));A)

这将创建一个未命名的函数,该函数接受一个数组并返回就地修改的输入数组。

取消高尔夫:

# Create a function to reverse the columns of a matrix
function revcols!(X)
    for = 1:size(X, 2)
        X[:,i] = reverse(X[:,i])
    end
    return X
end

# Our main function
function zgarb!(A)
    # Only perform operations if the array isn't one element
    if length(A) > 1
        # Record the number of rows
        m = size(A, 1)

        # Reverse the columns in place
        revcols!(A)

        # Shift each diagonal
        for i = -m:m
            A[diagind(A, i)] = circshift(diag(A, i), 1)
        end

        # Reverse the columns back
        revcols!(A)
    end
    return A
end

感谢MartinBüttner提供的算法建议并节省了4个字节!


3

ES6,75个字节

这接受一个数组数组作为参数并对其进行修改。

a=>{t=a.shift();a.map(r=>{t.push(r.pop());r.unshift(t.shift())});a.push(t)}

取消高尔夫:

function anti_diagonal(array) {
    var temp = array.shift(); // strip off the first row
    array.forEach(row => temp.push(row.pop())); // strip off the last elements of each row
    array.forEach(row => row.unshift(temp.shift())); // distribute the elements to the beginning of each row
    array.push(temp); // the remaining elements become the last row
}

请参阅@aditsu的图以进一步说明。


您可以通过更改{t.push(r.pop());r.unshift(t.shift())}t.push(r.pop())+r.unshift(t.shift())
user81655

3

Pyth,20个字节

J+PhQ.)MQ++L.(J0tQ]J

使用Adistu的方法,先删除顶部的行和右侧的列,然后将其粘贴在左侧和底部。但是具有可变的数据结构,而不是转置。


2

八度,85字节

@(a)[(b=[a(1,1:end),a(2:end,end)'])(1:(s=size(a)(1)))',[a(2:end,1:end-1);b(s+1:end)]]

我希望我可以摆脱ends。


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.