10
\$\begingroup\$

This challenge is inspired by a SO question about traversing a matrix by enumerating all its diagonal strips.

Instead of a matrix, consider a block of text:

ABCD
EFGH
IJKL

Traversing this block's SW-NE diagonals from left to right, starting from the top left corner and ending in the bottom right, results in the following output:

A
EB
IFC
JGD
KH
L

Challenge

Write a program or a function that execute the reverse of the process described above. That is, given a set SW-NE diagonal strips, output the block of text that produced it.

Input and Output

Both input and output can be represented as strings with newlines, or arrays/lists of strings.

Trailing newlines are optional.

The input will consist of at least one printable character and can be assumed to be correct (there will not be inconsistent row lengths).

The output block will always have a number of columns greater than or equal to the number of rows.

Test Cases

Input:

A

Output:

A

Input:

.
LI
PO.
PV.
CE
G

Output:

.I..
LOVE
PPCG

Input:

M
DA
AIT
LAR
SGI
/OX
/N
/

Output:

MATRIX
DIAGON
ALS///
\$\endgroup\$
  • \$\begingroup\$ Will the input strings contain any spaces? \$\endgroup\$ – kirbyfan64sos Sep 16 '15 at 19:40
  • \$\begingroup\$ Also, is trailing whitespace allowed? \$\endgroup\$ – kirbyfan64sos Sep 16 '15 at 19:46
  • \$\begingroup\$ @kirbyfan64sos Yes, input may contain spaces. Trailing whitespace is allowed. \$\endgroup\$ – Cristian Lupascu Sep 17 '15 at 6:38
5
\$\begingroup\$

CJam, 23 20 bytes

{_z,,Nf*W%\.+zW%sN%}

Try it here.

\$\endgroup\$
  • \$\begingroup\$ Taking advantage of the input format (and not breaking on spaces): {_z,,Nf*W%\.+zW%sN%} \$\endgroup\$ – Dennis Sep 16 '15 at 13:57
  • \$\begingroup\$ @Dennis Do I count the curly braces, because it's a "function"? I'm new to CJam golf. \$\endgroup\$ – Lynn Sep 16 '15 at 19:13
  • \$\begingroup\$ Yes. The block (with curly brackets) is CJam's closest alternative to an anonymous function/lambda, so this counts as 20 bytes. \$\endgroup\$ – Dennis Sep 16 '15 at 19:15
3
\$\begingroup\$

Python 2, 84

L=[]
for w in input():
 i=0;L+=[''][:len(w)-len(L)]
 for c in w:i-=1;L[i]+=c
print L

Input and output are lists of strings.

input: ['A','EB','IFC','JGD','KH','L']
output: ['ABCD', 'EFGH', 'IJKL']

The list of lines L to output is built up as we read the input. Each new character is appended to a line, starting from the last line i=-1 and progressing towards the front.

Whenever the new line to add is too long for the list, a new empty line is appended: L+=[''][:len(w)-len(L)]. I'm hoping for a way to shorten this part.

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

Python 2, 165 162 169 163 bytes

import sys
j=map(list,[e.strip() for e in sys.stdin.readlines()])
r=max(1,len(j)-2)
while j:
 s=''
 for b in range(r):s+=j[b].pop()
 j=[e for e in j if e]
 print s

Reads all lines from input, then turns them into a list of lists. Loops while that list has elements. In each iteration, it pops the last element from the number of inner lists equal to the number of columns in the output. The list is then cleaned and the line printed.

Examples:

$ python rectangle.py << EOF
> A
> EB
> IFC
> JGD
> KH
> L
> EOF
ABCD
EFGH
IJKL
$ python rectangle.py << EOF
> .
> LI
> PO.
> PV.
> CE
> G
> EOF
.I..
LOVE
PPCG
$ python rectangle.py << EOF
> M
> DA
> AIT
> LAR
> SGI
> /OX
> /N
> /
> EOF
MATRIX
DIAGON
ALS///

Thanks to w0lf for saving 6 bytes.

PYG, 139 bytes

j=M(L,[e.strip() for e in sys.stdin.readlines()])
r=Mx(1,len(j)-2)
while j:
 s=''
 for b in R(r):s+=j[b].pop()
 j=[e for e in j if e]
 P(s)
\$\endgroup\$
  • \$\begingroup\$ Is the last s='' needed? \$\endgroup\$ – Cristian Lupascu Sep 17 '15 at 6:42
  • \$\begingroup\$ Ah, it sneaked in there; thanks! \$\endgroup\$ – Celeo Sep 17 '15 at 15:43
0
\$\begingroup\$

Python, 332 325 bytes

Because Python.

n=[]
v=[]
x=y=-1
l=0
s=""
while 1:
 k=raw_input()
 if len(k)<1:break
 n.append(k)
while 1:
 if l>len(n):break
 y+=1
 try:
    x+=1;k=n[y][x]
    if[x,y]not in v:s+=k;v.append([x,y])
    else:raise
 except:
    try:
     x-=1;k=n[y][x]
     if[x,y]not in v:s+=k;v.append([x,y])
    except:s+="\n";x=-1;y=l;l+=1
print s[:s.rstrip("\n").rfind("\n")]
\$\endgroup\$
  • 1
    \$\begingroup\$ This is a [code-golf] question, which means answers must try to be a short as possible. Try removing some whitespace and simplifying your algorithm to save more space. Check out this great resource on golfing in python if you need ideas. \$\endgroup\$ – DankMemes Sep 17 '15 at 3:03
  • \$\begingroup\$ I'll check it out, thanks! \$\endgroup\$ – RK. Sep 17 '15 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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