Skip to main content
edited body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Python 2, 104 bytes

l=[] for x in input().split('\n'):n=len(x);l=[a[:n]+b[n:]for a,b in zip(l+[x],['']+l)] print'\n'.join(l) 

An iterative one-pass algorithm. We go through each line in order, updating the list l of lines to output. The new word effectively pushes from the bottom, shifting all letters above it one space. For example, in the test case

Programming Puzzles & Code Golf 

after we've done up to Code, we have

P Prog &uzzram Codelesming 

and then adding Golf results in

P Prog &uzz Coderam Golflesming 

which we can view as the combination of two pieces

P | Prog | &uzz | Code | ram Golf | lesming 

where the first piece got shifted up by golf. We perform this shifting with a zip of the output list with the element at the end (left side) and output list precedence by a blank line (right side), cutting off each part at the length of the new element.

It might seem more natural to instead iterate backwards, letting new letters fall from the top, but my attempt at that turned out longer.

For comparison, here's a zip/filter approach, with map(None,*x) used for iziplongest (109 bytes):

f=lambda xz:[''.join(filter(None,x))for x in map(None,*x*z)] lambda x:'\n'.join(f(f(x.split('\n')[::-1]))[::-1]) 

Python 2, 104 bytes

l=[] for x in input().split('\n'):n=len(x);l=[a[:n]+b[n:]for a,b in zip(l+[x],['']+l)] print'\n'.join(l) 

An iterative one-pass algorithm. We go through each line in order, updating the list l of lines to output. The new word effectively pushes from the bottom, shifting all letters above it one space. For example, in the test case

Programming Puzzles & Code Golf 

after we've done up to Code, we have

P Prog &uzzram Codelesming 

and then adding Golf results in

P Prog &uzz Coderam Golflesming 

which we can view as the combination of two pieces

P | Prog | &uzz | Code | ram Golf | lesming 

where the first piece got shifted up by golf. We perform this shifting with a zip of the output list with the element at the end (left side) and output list precedence by a blank line (right side), cutting off each part at the length of the new element.

It might seem more natural to instead iterate backwards, letting new letters fall from the top, but my attempt at that turned out longer.

For comparison, here's a zip/filter approach, with map(None,*x) used for iziplongest (109 bytes):

f=lambda x:[''.join(filter(None,x))for x in map(None,*x)] lambda x:'\n'.join(f(f(x.split('\n')[::-1]))[::-1]) 

Python 2, 104 bytes

l=[] for x in input().split('\n'):n=len(x);l=[a[:n]+b[n:]for a,b in zip(l+[x],['']+l)] print'\n'.join(l) 

An iterative one-pass algorithm. We go through each line in order, updating the list l of lines to output. The new word effectively pushes from the bottom, shifting all letters above it one space. For example, in the test case

Programming Puzzles & Code Golf 

after we've done up to Code, we have

P Prog &uzzram Codelesming 

and then adding Golf results in

P Prog &uzz Coderam Golflesming 

which we can view as the combination of two pieces

P | Prog | &uzz | Code | ram Golf | lesming 

where the first piece got shifted up by golf. We perform this shifting with a zip of the output list with the element at the end (left side) and output list precedence by a blank line (right side), cutting off each part at the length of the new element.

It might seem more natural to instead iterate backwards, letting new letters fall from the top, but my attempt at that turned out longer.

For comparison, here's a zip/filter approach, with map(None,*x) used for iziplongest (109 bytes):

f=lambda z:[''.join(filter(None,x))for x in map(None,*z)] lambda x:'\n'.join(f(f(x.split('\n')[::-1]))[::-1]) 
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Python 2, 104 bytes

l=[] for x in input().split('\n'):n=len(x);l=[a[:n]+b[n:]for a,b in zip(l+[x],['']+l)] print'\n'.join(l) 

An iterative one-pass algorithm. We go through each line in order, updating the list l of lines to output. The new word effectively pushes from the bottom, shifting all letters above it one space. For example, in the test case

Programming Puzzles & Code Golf 

after we've done up to Code, we have

P Prog &uzzram Codelesming 

and then adding Golf results in

P Prog &uzz Coderam Golflesming 

which we can view as the combination of two pieces

P | Prog | &uzz | Code | ram Golf | lesming 

where the first piece got shifted up by golf. We perform this shifting with a zip of the output list with the element at the end (left side) and output list precedence by a blank line (right side), cutting off each part at the length of the new element.

It might seem more natural to instead iterate backwards, letting new letters fall from the top, but my attempt at that turned out longer.

For comparison, here's a zip/filter approach, with map(None,*x) used for iziplongest (109 bytes):

f=lambda x:[''.join(filter(None,x))for x in map(None,*x)] lambda x:'\n'.join(f(f(x.split('\n')[::-1]))[::-1])