29
\$\begingroup\$

Objective

Given a grid of numbers, fill in the inequalities.

Assumptions

The number of columns and rows in the grid are equal.
The maximum size of the grid is 12x12.
The grid only consists of integers 0-9.
The output may contain a trailing newline.
The input is exactly as written below, including spaces and newlines.

Example Input

4 2 3 1

6 2 3 1

6 9 2 1

0 2 1 6

Example Output

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

Example Input (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

Example Output (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

Example Input (3)

8

Example Output (3)

8

Example Input (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

Example Output (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
\$\endgroup\$
8
  • \$\begingroup\$ In Ex. 2 rows and columns aren't equal. \$\endgroup\$
    – geokavel
    Oct 24, 2015 at 0:49
  • 1
    \$\begingroup\$ Will there be 2-digit numbers? \$\endgroup\$
    – Downgoat
    Oct 24, 2015 at 0:50
  • \$\begingroup\$ @Vɪʜᴀɴ The grid only consists of integers 0-9. \$\endgroup\$
    – Dennis
    Oct 24, 2015 at 0:52
  • 1
    \$\begingroup\$ I assume there are no trailing spaces on the numbered lines of the input. Are there any spaces on the blank lines? If I press the edit button I find that there are 23 spaces on each blank line for example 4, zero for all other examples. But the rendered page removes them all, at least when I copy it into a text editor to check.. \$\endgroup\$ Oct 24, 2015 at 10:57
  • 1
    \$\begingroup\$ @steveverrill There are no spaces on the blank lines. \$\endgroup\$
    – absinthe
    Oct 24, 2015 at 13:41

6 Answers 6

7
\$\begingroup\$

CJam, 52 bytes

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

Try it online in the CJam interpreter.

Thanks to @CroCo for pointing out a bug in revision 3.

Thanks to @Pyrrha for pointing out a bug in revision 6.

How it works

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#
\$\endgroup\$
0
4
\$\begingroup\$

Python 2, 207 197 bytes

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

This one creates a function f which takes the grid of numbers as a string and returns the corresponding string with filled inequalities.

The function iterates over each character in the string. If the character is a space, it is replaced with the inequality for the numbers on either side. If the character and the next character are newlines, the whole line is replaced with the inequalities for all the numbers above and below.

Here is the output of the function for each of the examples in the question, except the really long one:

>>> 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
\$\endgroup\$
1
  • \$\begingroup\$ That's clever, putting the characters in a string then subscripting it. I'll have to use that sometime. \$\endgroup\$
    – jqkul
    Oct 24, 2015 at 13:39
3
\$\begingroup\$

Pyth, 46 bytes

juCms.iJ-d\ m.x@H._-FsMk\ .:J2Gc2"=><\"v^"%2.z

Try it online: Demonstration

\$\endgroup\$
3
\$\begingroup\$

C, 552 408 bytes

This is a mess, but it does work with the test cases (for the solo 8, the input has to be followed by a newline to work properly)

#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);}

Here's the expanded version; I'd love to here ways on how I could golf this more effectively. I know that there is much to be improved on here.

#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);
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Needs more ternary operator. E.g.: in your inner-most loop: s=t>b?'v':t<b?'^':'"'; Note that checking if two values are equal after you already checked if one or the other is greater is unnecessary. There's only 3 possibilities. \$\endgroup\$ Oct 24, 2015 at 15:22
  • \$\begingroup\$ Thanks for the advice, doing that in addition with a few other things took over 100 bytes off. \$\endgroup\$ Oct 24, 2015 at 16:15
2
\$\begingroup\$

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>

\$\endgroup\$
1
\$\begingroup\$

Haskell, 201 bytes

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 expects a string.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.