Reconstruct a Text Rectangle from Diagonal Strips

10

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

Cristian Lupascu

Posted 2015-09-16T06:05:35.063

Reputation: 8 369

Will the input strings contain any spaces? – kirbyfan64sos – 2015-09-16T19:40:53.720

Also, is trailing whitespace allowed? – kirbyfan64sos – 2015-09-16T19:46:24.853

@kirbyfan64sos Yes, input may contain spaces. Trailing whitespace is allowed. – Cristian Lupascu – 2015-09-17T06:38:58.053

Answers

5

CJam, 23 20 bytes

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

Try it here.

Lynn

Posted 2015-09-16T06:05:35.063

Reputation: 55 648

Taking advantage of the input format (and not breaking on spaces): {_z,,Nf*W%\.+zW%sN%} – Dennis – 2015-09-16T13:57:15.343

@Dennis Do I count the curly braces, because it's a "function"? I'm new to CJam golf. – Lynn – 2015-09-16T19:13:38.157

Yes. The block (with curly brackets) is CJam's closest alternative to an anonymous function/lambda, so this counts as 20 bytes. – Dennis – 2015-09-16T19:15:54.317

3

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.

xnor

Posted 2015-09-16T06:05:35.063

Reputation: 115 687

1

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)

Celeo

Posted 2015-09-16T06:05:35.063

Reputation: 520

Is the last s='' needed? – Cristian Lupascu – 2015-09-17T06:42:16.953

Ah, it sneaked in there; thanks! – Celeo – 2015-09-17T15:43:54.343

0

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

RK.

Posted 2015-09-16T06:05:35.063

Reputation: 497

1

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.

– DankMemes – 2015-09-17T03:03:24.057

I'll check it out, thanks! – RK. – 2015-09-17T14:30:56.703