2D不等式


29

目的

数字のグリッドが与えられたら、不等式を埋めます。

仮定

グリッドの列と行の数は同じです。
グリッドの最大サイズは12x12です。
グリッドは、0〜9の整数のみで構成されます。
出力には末尾の改行が含まれる場合があります。
入力は、スペースと改行を含め、以下に正確に記述されています。

入力例

4 2 3 1

6 2 3 1

6 9 2 1

0 2 1 6

出力例

4>2<3>1
^ " " "
6>2<3>1
" ^ v "
6<9>2>1
v v v ^
0<2>1<6

入力例(2)

1 2 3 4 5

5 4 3 2 1

0 0 0 3 2

3 2 0 0 0

2 1 3 1 5

出力例(2)

1<2<3<4<5
^ ^ " v v
5>4>3>2>1
v v v ^ ^
0=0=0<3>2
^ ^ " v v
3>2>0=0=0
v v ^ ^ ^
2>1<3>1<5

入力例(3)

8

出力例(3)

8

入力例(4)

0 0 0 0 0 0 0 0 0 0 0 0

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

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

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

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

0 1 5 7 8 9 9 7 6 6 1 0

0 1 6 6 7 9 9 8 7 5 1 0

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

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

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

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

0 0 0 0 0 0 0 0 0 0 0 0

出力例(4)

0=0=0=0=0=0=0=0=0=0=0=0
" ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ "
0<1=1=1=1=1=1=1=1=1=1>0
" " ^ ^ ^ ^ ^ ^ ^ ^ " "
0<1<2<3<4<5<6<7<8<9>1>0
" " ^ ^ ^ ^ " v v v " "
0<1<3<9>8>7>6>5>4<8>1>0
" " ^ v ^ ^ ^ ^ ^ v " "
0<1<4<8<9>8>7>6>5<7>1>0
" " ^ v v ^ ^ ^ ^ v " "
0<1<5<7<8<9=9>7>6=6>1>0
" " ^ v v " " ^ ^ v " "
0<1<6=6<7<9=9>8>7>5>1>0
" " ^ v v v v ^ ^ v " "
0<1<7>5<6<7<8<9>8>4>1>0
" " ^ v v v v v ^ v " "
0<1<8>4<5<6<7<8<9>3>1>0
" " ^ ^ ^ " v v v v " "
0<1<9>8>7>6>5>4>3>2>1>0
" " v v v v v v v v " "
0<1=1=1=1=1=1=1=1=1=1>0
" v v v v v v v v v v "
0=0=0=0=0=0=0=0=0=0=0=0

例では 2行と2列は等しくありません。
geokavel

1
2桁の数字はありますか?
ダウンゴート

@Vɪʜᴀɴ グリッドは整数0-9のみで構成されます。
デニス

1
入力の番号付き行に末尾のスペースがないと仮定します。空白行にスペースはありますか?編集ボタンを押すと、各空白行に23個のスペースがあり、たとえば4、他のすべての例ではゼロであることがわかります。私がチェックするために、テキストエディタにコピーする場合でも、レンダリングされたページは...少なくとも、それらをすべて削除
レベル川セント

1
@steveverrill空白行にスペースはありません。
アブサン

回答:



7

CJam、52バイト

qN%::~_z_2{{_1>.-W<:g}%\z}*@@..{'=+}.{N@"\"v^"f=S*N}

CJamインタープリターでオンラインで試してください。

リビジョン3のバグを指摘してくれた@CroCoに感謝します。

リビジョン6のバグを指摘してくれた@Pyrrhaに感謝します。

使い方

qN%        e# Read all input and split it at runs of linefeeds.
::~        e# Evaluate each character separately.
           e# This turns non-empty lines into arrays of integers.
_z_        e# Copy, transpose rows and columns, and copy again.
2{         e# Do the following twice:
  {        e#   For each row:
    _1>    e#     Copy the row and remove the copy's first element.
    .-     e#     Perform vectorized subtraction.
    W<     e#     Remove the last element.
           e#     This pushes the array of increments of the row.
    :g     e#     Replace each difference with its sign (-1, 0 or 1).
  }%       e#
  \        e#   Swap the two topmost arrays on the stack.
  z        e#   Transpose rows and columns of the topmost array.
}*         e#
           e# The topmost result has been transposed before and after computing
           e# the increments of its rows. It holds the increments of it columns.
           e# The result below it has been transposed twice (therefore not at
           e# all) before computing the increments of its rows.
@@         e# Rotate the number array and the row increment array on top.
..{        e# For each number and the corresponding increment, push both; then:
  '=+      e#   Add the increment to the character '='.
}          e#
.{         e# For each row of the column increment array and corresponding row
           e# of the last result, push both rows; then:
  N@       e#   Push a linefeed and rotate the column increments on top.
  "\"v^"f= e#   For each, select the appropriate comparison character.
  S*       e#   Join those characters, separated by spaces.
  N        e#   Push another linefeed.
 }         e#

5

Python 2、207 197バイト

f=lambda a:''.join(['=><'[cmp(a[i-1],a[i+1])]if c==' 'else'\n'+' '.join('"v^'[cmp(a[j-a.index('\n')],a[j+2])]for j in range(i,i+a.index('\n'),2))if a[i:i+2]=='\n\n'else c for i,c in enumerate(a)])

これは関数fを作成します。この関数は、数値のグリッドを文字列として受け取り、不等式が埋められた対応する文字列を返します。

この関数は、文字列内の各文字を反復処理します。文字がスペースの場合、両側の数字の不等式に置き換えられます。文字と次の文字が改行の場合、行全体が上下のすべての数字の不等式に置き換えられます。

以下は、問題の各例に対する関数の出力です。ただし、実際に長いものは除きます。

>>> print f("""\
... 4 2 3 1
...
... 6 2 3 1
...
... 6 9 2 1""")
4>2<3>1
^ " " "
6>2<3>1
" ^ v "
6<9>2>1
>>> print f("""\
... 1 2 3 4 5
...
... 5 4 3 2 1
...
... 0 0 0 3 2
...
... 3 2 0 0 0
...
... 2 1 3 1 5""")
1<2<3<4<5
^ ^ " v v
5>4>3>2>1
v v v ^ ^
0=0=0<3>2
^ ^ " v v
3>2>0=0=0
v v ^ ^ ^
2>1<3>1<5
>>> print f("8")
8

文字列に文字を入れてから添え字を付けるのは賢明です。私はいつかそれを使わなければなりません。
bkul

3

C、 552 408バイト

これは混乱ですが、テストケースでは機能します(ソロの8場合、入力が適切に機能するためには改行を入力する必要があります)

#define P putchar
main(n,z)char**z;{char*t=*++z;n=0;while(*(*z)++!=10)if(**z!=32)n++;char a[n][n];int r=-1,c=0;n--;do*t>32?c?:r++,a[c][r]=*t:*t==10?c=0:c++;while(*++t);r=c=0;do{int j=a[c][r],s=61,k=a[c+1][r];P(j);if (c==n){if(r==n)break;c=0;r++;P(10);for(int t=a[c][r-1],b=a[c][r];c<n+1;t=a[c][r-1],b=a[c][r])s=t>b?118:t<b?94:34,printf("%c ",s),c++;c=0;P(10);continue;}s=j>k?62:j<k?60:s;P(s);c++;}while(1);}

これが拡張バージョンです。ここでもっと効果的にゴルフをする方法をここで教えたい。ここには改善すべき点がたくさんあることを知っています。

#define P putchar
main(n,z)char**z; {
    char *t = *++z;
    n = 0;
    while (*(*z)++!=10)
        if (**z!=32)
            n++;
    char a[n][n];
    int c,r=c=0;
    r = -1,n--;
    do
        *t>32?c?:r++, a[c][r] = *t:*t==10?c=0:c++; //32 is ASCII for space
    while (*++t);
    r=c=0;
    do {
        int j = a[c][r],s=61,k = a[c+1][r];P(j);
        if (c==n)
        {
            if (r==n)break;
            c=0;r++;P(10);
            for (int t=a[c][r-1],b=a[c][r];c<n+1; t = a[c][r-1],b = a[c][r])
                s=t>b?118:t<b?94:34,printf("%c ",s),c++;
            c = 0;
            P(10);
            continue;
        }
        s=j>k?62:j<k?60:s;

        P(s);
        c++;
    } while (1);
}

1
さらに三項演算子が必要です。例:最も内側のループ:s=t>b?'v':t<b?'^':'"';どちらかが大きいかどうかを確認した後、2つの値が等しいかどうかを確認する必要がないことに注意してください。可能性は3つしかありません。
ダレルホフマン

アドバイスをありがとう、それに加えていくつかの他のことを行うと、100バイト以上もかかりました。
クリスルーナム

2

JavaScript(ES6)162

f=s=>(s=s.split`
`).map((r,i)=>r?(w=r).replace(/ /g,(c,j)=>x('<=>',r[j-1]-r[j+1])):w.replace(/\d/g,(c,j)=>x('^"v', c-s[i+1][j]))  ,x=(y,v)=>y[-~(v>0)-(v<0)]).join`
`

// more readeable 
u=s=>(
  x=(y,v)=>y[-~(v>0)-(v<0)],
  s=s.split`\n`,
  s.map((r,i)=>r
    ?(w=r).replace(/ /g,(c,j)=>x('<=>',r[j-1]-r[j+1]))
    :w.replace(/\d/g,(c,j)=>x('^"v', c-s[i+1][j]))
  ).join`\n`
)

//TEST
console.log=x=>O.innerHTML+=x+'\n'

;[
 '4 2 3 1\n\n6 2 3 1\n\n6 9 2 1\n\n0 2 1 6'
,'1 2 3 4 5\n\n5 4 3 2 1\n\n0 0 0 3 2\n\n3 2 0 0 0\n\n2 1 3 1 5'
,'8',  
,'0 0 0 0 0 0 0 0 0 0 0 0\n\n0 1 1 1 1 1 1 1 1 1 1 0\n\n0 1 2 3 4 5 6 7 8 9 1 0\n\n0 1 3 9 8 7 6 5 4 8 1 0\n\n0 1 4 8 9 8 7 6 5 7 1 0\n\n0 1 5 7 8 9 9 7 6 6 1 0\n\n0 1 6 6 7 9 9 8 7 5 1 0\n\n0 1 7 5 6 7 8 9 8 4 1 0\n\n0 1 8 4 5 6 7 8 9 3 1 0\n\n0 1 9 8 7 6 5 4 3 2 1 0\n\n0 1 1 1 1 1 1 1 1 1 1 0\n\n0 0 0 0 0 0 0 0 0 0 0 0'  
].forEach(t=>console.log(t+'\n\n'+f(t)+'\n\n'))
<pre id=O></pre>


1

Haskell、201バイト

import Data.List
t=transpose
g=mapM_ putStrLn.t.map(h 1).t.map(h 0).lines
h n s@(a:_:b:r)|'/'<a&&a<':'=a:(o n a b):h n(b:r)
 |0<1=s
h n r=r
f=fromEnum
o n a b=l!!n!!(1+signum(f a-f b))
l=["<=>","^\"v"]

g 文字列が必要です。

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