形状を維持しながら列を反転


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]]

あなたの仕事は、この操作を実装することです。

入出力

入力は、行を表す非負整数のリストのリストです。行の長さは異なる場合があり、空の行もあります。常に少なくとも1つの行があります。上記のように、出力は各列を反転した結果です。入力および出力は、合理的な形式である可能性があります。

各言語の最低バイト数が優先されます。標準の規則が適用されます。

テストケース

[[]] -> [[]]
[[],[]] -> [[],[]]
[[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

@ETHproductionsいいえ、出力には数字のみを含める必要があります。
ズガルブ

-1一般的ではないため(負の数字、文字、文字列、およびすべての可能なタイプを行要素として許可しない)+気に入らない(不必要に思える)
-RosLuP

回答:


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⁹?右)それ以外の場合は私が持っていたこれを 1以上のバイトのために
Outgolferエリック

はい、それがまさにそれです。
デニス

4

Japt15 13バイト

@Shaggyのおかげで2バイト節約

y@=XfÊX£o
®fÄ

オンラインでテストしてください!

行にヌル値を埋め込み、4バイトを節約できる場合、2行目を削除できます。

説明

 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Ä
シャギー

実際、mf2行目で機能しているようです。
シャギー

@シャギーありがとう、それらを考えていませんでした!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それは完璧です、ありがとう。
ngn

@Adámmix-each-splitではなく、ランク1を使用するように編集しました。
ngn

@Adám:+ 1 / -1の代わりにexp / logが⎕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]最初のを返すatでインデックスを作成する代わりに、を使用して実行できる最初のを取得しx[;0]ます。

しかし、変数を渡すyx[;]やっとして扱い、それをx[y]ないx[;y]ので、無理に勧め::であり: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)は、要素がまったくない場合に単純にリストを返します。

reverse関数は[]要素を無視する必要があります(例[[],[1],[],[3],[4]]-> [[],[4],[],[3],[1]])、2つの引数を受け取ることでそうします:最初の引数は逆順の要素(例[4,3,1])で、2番目は元の行です。

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 Right; なぜそうしないと思ったのか、私にはわかりません。
ニール


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

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.