在保留形状的同时反转列


20

介绍

假设您有一个整数列表的列表(或者实际上是任何对象,但是为了简单起见,我们坚持使用整数)。列表的长度可能不同,其中一些可能为空。让我们以表格格式编写列表:

[[ 1,   2,   3,   4,   5],
 [ 6,   7],
 [ 8,   9,  10,  11],
 [],
 [12,  13,  14],
 [15,  16,  17,  18]]

这个表有5个垂直的列中,包含数字1, 6, 8, 12, 152, 7, 9, 13, 163, 10, 14, 174, 11, 18,和5。如果我们反向每一列中,我们得到的名单15, 12, 8, 6, 116, 13, 9, 7, 217, 14, 10, 318, 11, 4,和5。让我们将这些数字重新插入表的列中,同时使行的长度与以前相同:

[[15,  16,  17,  18,   5],
 [12,  13],
 [ 8,   9,  14,  11],
 [],
 [ 6,   7,  10],
 [ 1,   2,   3,   4]]

您的任务是实施此操作。

输入输出

您的输入是一个非负整数列表的列表,代表行。这些行的长度可能不同,其中一些可能为空。总是至少会有一行。输出是反转每一列的结果,如上所述。输入和输出可以采用任何合理的格式。

每种语言的最低字节数为准。适用标准规则。

测试用例

[[]] -> [[]]
[[],[]] -> [[],[]]
[[8,5,1]] -> [[8,5,1]]
[[1,200],[0,3]] -> [[0,3],[1,200]]
[[],[3,9],[1],[]] -> [[],[1,9],[3],[]]
[[],[5,8,7],[0,6,5,7,1]] -> [[],[0,6,5],[5,8,7,7,1]]
[[1,8,5],[7,5,4],[],[1]] -> [[1,5,4],[7,8,5],[],[1]]
[[],[],[2],[],[31],[],[5],[],[],[],[7]] -> [[],[],[7],[],[5],[],[31],[],[],[],[2]]
[[1,10,100,1000],[2,20,200],[3,30],[4],[5,50,500],[6,60],[7]] -> [[7,60,500,1000],[6,50,200],[5,30],[4],[3,20,100],[2,10],[1]]
[[8,4],[3,0,4,8,1],[8],[0,8],[9,7,1,6],[3,8,1,9,5]] -> [[3,8],[9,7,1,9,5],[0],[8,8],[3,0,1,6],[8,4,4,8,1]]
[[3,9,3],[5],[1],[3,5],[9,0,6,2],[1,3],[4,9,2],[6,6,7,8,7]] -> [[6,6,7],[4],[1],[9,9],[3,3,2,8],[1,0],[5,5,6],[3,9,3,2,7]]
[[8,5,6],[3,5,2,4,9],[4,3,8,3,7],[6,1,1],[1,8,9,9],[9,1,2],[8,7]] -> [[8,7,2],[9,1,9,9,7],[1,8,1,3,9],[6,1,8],[4,3,2,4],[3,5,6],[8,5]]
[[2,4],[1,4],[0,8,7,3],[4,9,2,5],[2,8,0],[0,8,3],[7,3,1],[],[3,3,7,8]] -> [[3,3],[7,3],[0,8,7,8],[2,8,1,5],[4,9,3],[0,8,0],[1,4,2],[],[2,4,7,3]]

1
我们可以用空值填充输出的行吗?(例如[[1,9],[3],[2,4,5]] -> [[2,4],[3,null],[1,9,5]]
ETHproductions '18

@ETHproductions否,输出应仅包含数字。
Zgarb

-1,因为它不通用(不允许负数,字母,字符串和所有可能的类型作为行元素)+我不喜欢它(似乎没有必要的困难)
RosLuP

Answers:


5

果冻,16字节

ḟṚṁṣj
z-ç€-ZFḟ-ṁ

在线尝试!验证所有测试用例

怎么运行的

z-ç€-ZFḟ-ṁ  Main link. Argument: M (matrix / 2D array)

z-          Zip the rows of M, using -1 as filler.
  ç€-       Map the helper link over the result, with right argument -1.
     Z      Zip the rows of the result.
      F     Flatten the resulting matrix.
       ḟ-   Filterfalse -1; remove all occurrences of -1.
         ṁ  Mold; shape the result like M.


ḟṚṁṣj       Helper link.
            Left argument: A (row / 1D array). Right argument: -1

ḟ           Filterfalse; remove all occurrences of -1.
 Ṛ          Reverse the resulting vector.
   ṣ        Split A at occurrences of -1.
  ṁ         Mold; shape the vector to the left like the 2D array to the right.
    j       Join the resulting 2D array, separating by -1.

很好,最上面的一行非常聪明!(ḟṚṁṣj不会⁸ḟ⁹Ṛṁ⁸ṣ⁹¤j⁹吧?),否则我有多一个字节的
埃里克Outgolfer

是的,这就是它的作用。
丹尼斯

4

Japt15 13字节

@Shaggy节省了2个字节

y@=XfÊX£o
®fÄ

在线测试!

如果允许我们使用空值填充行,那么可以节省第二个行,从而节省了4个字节。

说明

 y@  =XfÊ X£  o      Implicit: U = input array
UyX{U=Xfl Xm{Uo}}    (Ungolfed)
UyX{            }    Map each column X in the input by this function:
    U=Xfl              Set U to X filtered to only items whose factorial is truthy;
                       this just gets rid of the empty slots in the column.
          Xm{  }       Map each item in X to
             Uo          the last item in U, popping this item from the list.
                       Due to the way .map works in JS, this is only called on real items
                       and not empty slots, so this preserves empty slots.
                     Newline: set U to the resulting column-reversed array
 ®   fÄ              Due to the way y works, there will now be `undefined` in some rows.
UmZ{Zf+1}            (Ungolfed)
 mZ{    }            Map each row Z in U to
    Zf+1               Z filtered to only items where the item + 1 is truthy.
                     undefined + 1 is NaN, which is falsy, and thus eliminated.
                     Implicit: output result of last expression

好东西!你可以把它降低到13个字节替换l;Êmf_Ä®fÄ
粗野的

实际上,mf似乎只适用于第二行。
粗野的

@Shaggy谢谢,没想到这些!mf不幸的是,它将消除结果中的任何零...
ETHproductions

啊,是的,我没有想到这一点。
毛茸茸的

4

APL(Dyalog Unicode)20 19 16 字节SBCS

-4感谢ngn。

完整程序。提示从STDIN输入。

0~¨⍨↓⍉⌽@×⍤1⍉↑*⎕

在线尝试!

实例演练说明

 提示输入评估值
[[1,8,5],[7,5,4],[],[1]]

* 提高e的幂(e n,确保没有零)
[[2.7,2981,148.4],[1096.6,148.4,54.6],[],[2.7]]

 将列表混合到一个矩阵中,并用零填充:
┌ ┐
│2.7E0 3.0E3 1.5E2│
│1.1E3 1.5E2 5.5E1│
│0.0E0 0.0E0 0.0E0│
│2.7E0 0.0E0 0.0E0│
└ ┘

 转置
┌ ┐
│2.7E0 1.1E3 0.0E0 2.7E0│
│3.0E3 1.5E2 0.0E0 0.0E0│
│1.5E2 5.5E1 0.0E0 0.0E0│
└ ┘

⌽@×⍤1 反转每一行的正元素
┌ ┐
│2.7E0 1.1E3 0.0E0 2.7E0│
│1.5E2 3.0E3 0.0E0 0.0E0│
│5.5E1 1.5E2 0.0E0 0.0E0│
└ ┘

 转置
┌ ┐
│2.7E0 1.5E2 5.5E1│
│1.1E3 3.0E3 1.5E2│
│0.0E0 0.0E0 0.0E0│
│2.7E0 0.0E0 0.0E0│
└ ┘

 将矩阵拆分为列表列表
[[2.7,148.4,54.6],[1096.6,2981,148.4],[0,0,0],[2.7,0,0]]

0~¨⍨ 从每个列表中删除零
[[2.7,148.4,54.6],[1096.6,2981,148.4],[],[2.7]]

 自然对数
[[1,5,4],[7,8,5],[],[1]]


如果输入包含-1怎么办?
ngn

@ngn输入永远不会包含负数;请参见“输入和输出”部分。
Zgarb

@Zgarb太好了,谢谢。
ngn

@Adám我编辑为使用等级1而不是mix-each-split。
ngn

@亚当:EXP /日志,而不是+ 1 / -1覆盖与⎕fr测试←1287
NGN

3

K4,36个字节

解:

+{x[w]:|x w:&~^x;x}'x'[::;]@!|/#:'x:

例子:

q)k)+{x[w]:|x w:&~^x;x}'x'[::;]@!|/#:'x:(1 2 3 4 5;6 7;8 9 10 11;0#0N;12 13 14;15 16 17 18)
15 16 17 18 5
12 13        
8  9  14 11  

6  7  10     
1  2  3  4

q)k)+{x[w]:|x w:&~^x;x}'x'[::;]@!|/#:'x:(0#0N;5 8 7; 0 6 5 7 1)

0 6 5    
5 8 7 7 1

说明:

这一直很痛苦,我仍在努力简化省略的索引。

而非索引在,例如,x[0]它会返回前,我们要采取的第一个,这是可以做到用x[;0]

但是,将变量传递yx[;]它时,将其视为x[y]没有x[;y]::in 推入其中x[::;]

这等效于翻转列表列表,但是翻转要求所有列表的长度相等!

+{x[w]:|x w:&~^x;x}'x'[::;]@!|/#:'x: / the solution
                                  x: / save input as variable x
                               #:'   / count (#:) each (') 
                             |/      / take the max of these lengths
                            !        / til, range 0..max-1
                           @         / apply (index into)
                      [::;]          / :: is a kind of null, 
                    x'               / index into x at each of these    
 {              ; }'                 / two statement lambda on each (')
              ^x                     / null x (returns true if entry is null)
             ~                       / not, so flip true/false
            &                        / where, indexes where true
          w:                         / save as variable w  
        x                            / index into w at these indexes
       |                             / reverse
  x[w]:                              / store this back in variable x at indexes w
                 x                   / return x from function
+                                    / flip the result

3

Haskell,174个字节

f x=map g.h.map(g.reverse>>=(!)).h$take(maximum$length<$>x).(++z).map pure<$>x
g=concat
h x|g x==[]=x|4>2=foldr(zipWith(:))z x
x!(c:d)|c==[]=c:x!d|a:b<-x=[a]:b!d
_!y=y
z=[]:z

在线尝试!

取消高尔夫/解释

这个想法是包装所有元素[]并用行[]填充(结果比用负整数填充要短,这也允许负输入,这很好),然后转置,反转所有行并再次转置并展平每一行:

map concat                                   -- flatten each row
  . transpose'                               -- transpose (*)
  . map (\row-> reverse (concat row) ! row)  -- reverse each row (see below)
  . transpose'                               -- tranpose (*)
  $ take (maximum $ length <$> x)            -- only keep up as many as longest row
      . (++ z)                               -- pad row with [],[],..
      . map (\e-> [e])                       -- wrap elements in []
 <$> x

* h如果根本没有元素,此转置函数()仅返回列表。

反向函数必须忽略[]元素(例如[[],[1],[],[3],[4]]-> [[],[4],[],[3],[1]]),它通过接收两个参数来做到这一点:第一个是反向顺序的元素(例如[4,3,1]),第二个是原始行。

x@(a:b) ! (c:d)
 | c == []   = c:x ! d    -- if current element is []: skip it
 | otherwise = [a]:b ! d  -- else: replace with new one (a) and continue
_ ! y = y                 -- base case (if no new elements are left): done


2

JavaScript(ES6),79 76字节

(a,d=[],g=s=>a.map(b=>b.map((c,i)=>(d[i]=d[i]||[])[s](c))))=>g`push`&&g`pop`

编辑:由于@ETHproductions,节省了3个字节。


@ETHproductions正确;我不知道为什么我认为不会,否则我已经做到了。
尼尔


0

Clojure,123个字节

#(map(fn[i R](map(fn[j _](let[V(for[Q %](get Q j))F filter](nth(reverse(F + V))(count(F +(take i V))))))(range)R))(range)%)

我本来希望(+ nil)引发异常,但是它的结果是nil:o

此操作无需填充,而是计算有多少前几行至少与当前行一样长R

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.