Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Introduction

Let's say that S1 = a...b and S2 = ..c... If we place them on top of each other, we get:

a...b
..c..

We merge both strings, with the . as the liquid character (which can overlapped). We get this:

a.c.b

If one of the string is longer than the other, we just apply the same algorithm:

a.....b
..c..  

becomes:

a.c...b

and

a.....b
..c.......

becomes:

a.c...b...

If two characters collide, we just use the bottom character, e.g.

a..b
...c

becomes:

a..c

The Task

Given two non-empty strings, output the merged string. Note, you can assume that the input only contains periods and lowercase letters (or uppercase letters if that is more convenient).

Test cases

Input              Output
a....b ..c...      a.c..b
aaaaaa bbbbbb      bbbbbb
ab.ab. b.b.b.      bbbab.
a.......b c        c.......b

This is , so the submission with the least amount of bytes wins!

share|improve this question
    
Is input a.....b ..c....... possible? What's the output then? – Luis Mendo Mar 4 at 22:22
    
@DonMuesli That would become a.c...b.... – Adnan Mar 4 at 22:22
    
Can we output a list of characters instead of a string? – DenkerAffe Mar 5 at 0:49
    
@DenkerAffe No, sorry – Adnan Mar 5 at 0:51
    
Can the strings be taken in the opposite order? – Mego Mar 5 at 4:05

13 Answers 13

up vote 9 down vote accepted

Jelly, 5 bytes

Œu»Œl

Input via command-line arguments.

Try it online!

Explanation

This is a direct port of my CJam answer (see that for an explanation why this works):

Œu     # Convert first argument to upper case.
  »    # Element-wise maximum between both strings.
   Œl  # Convert result back to lower case.
share|improve this answer
3  
NOOO! You can't adopt Jelly too! We will be left with all the mods as golfing masters. – Eᴀsᴛᴇʀʟʏ Iʀᴋ Mar 5 at 4:47
    
@rikerw haha, why d'you think they're mods? cause they're good at golf :P – cat Mar 5 at 5:07
2  
@RikerW I just had to preempt someone else beating me by porting my own solution to Jelly. ¯\_(ツ)_/¯ – Martin Ender Mar 5 at 10:11

CJam, 9 bytes

leul.e>el

Test it here.

Explanation

Makes use of the fact that '.' < upper case letters < lower case letters. This way, when taking the element-wise maximum between two strings, any letter overrides a ., but we can make a letter from the second input override a letter from the first if we upper case the first. Confusing? Here's one of the test cases as an example:

ab.ab.
b.b.b.

Convert first to upper case:

AB.AB.
b.b.b.

Take the element-wise maximum:

bBbAb.

Convert back to lower case:

bbbab.

And here is how the code does that:

l    e# Read first line.
eu   e# Convert to upper case.
l    e# Read second line.
.e>  e# Take element-wise maximum. If the lengths are different, the additional elements
     e# from the longer list are just appended.
el   e# Convert back to lower case.
share|improve this answer
4  
Nice eu / el trick! – Luis Mendo Mar 4 at 22:26

Javascript ES6, 52 55 chars

(a,b)=>b.replace(/\./g,(m,i)=>a[i]||m)+a.slice(b.length)

Test

f=(a,b)=>b.replace(/\./g,(m,i)=>a[i]||m)+a.slice(b.length)
;`
a....b ..c...      a.c..b
aaaaaa bbbbbb      bbbbbb
ab.ab. b.b.b.      bbbab.
a.......b c        c.......b
c a....b           a....b
`.split('\n').filter(Boolean).map(s=>s.split(/\s+/)).every(a=>f(a[0],a[1])==a[2])
share|improve this answer
1  
This will fail for input such as: f('c', 'a....b') – andlrc Mar 5 at 9:00
    
@dev-null, fixed – Qwertiy Mar 5 at 21:14

Pyth, 11

smenD\.d.TQ

Try it online or run the Test Suite

share|improve this answer

Seriously, 10 bytes

,û,Z`M`MΣù

Try it online!

Uses the same strategy as Martin's CJam answer

Explanation:

,û,Z`M`MΣù
,û          get first string, uppercase
  ,Z        get second string, zip with first string
    `M`M    map maximum
        Σù  join and uppercase
share|improve this answer

Octave, 50 bytes

function c=m(a,b)c=b;c(a>0)=a;i=b>46;c(i)=b(i);end
share|improve this answer
    
You can replace != by > – Luis Mendo Mar 5 at 2:24

Haskell, 43 42 bytes

(a:b)#(c:d)|c<'a'=a:b#d|1<2=c:b#d
a#b=a++b

Usage example: "ab.ab." # "b.b.b." -> "bbbab.".

How it works:

  • if both list are non-empty, pick the the head of the 1st list if the head of the 2nd list is ".", else pick the head of the second list. Append a recursive call with the tails of the lists.

  • if at least one list is empty, append both lists.

Edit: @Lynn saved a byte. Thanks!

share|improve this answer
    
“you can assume that the input only contains periods and lowercase letters”, so you could check c<'a' to save a byte. – Lynn Mar 5 at 1:28
    
@Lynn: Thank you! – nimi Mar 5 at 1:36

Python 2, 47 bytes

lambda s,t:`map(max,s.upper(),t)`[2::5].lower()
share|improve this answer
    
very golfy! I'm looking for a way to get rid of upper() and lower() but no luck so far... – Max Mar 5 at 9:35

Julia, 101 bytes

f(s,t,r=i->rpad(i,max((n=endof)(s),n(t)),"."))=join([min(a,b)<90?max(a,b):b for(a,b)=zip(r(s),r(t))])

This is a function that accepts two strings and returns a string.

We compute m as the maximum length of the two inputs, then define a function r that right pads its input with .s to length m and store that as a function argument. We then zip the right padded inputs and check the minimum (as defined by the ASCII code) of each pair. If it's a ., we use whichever character has the bigger code, otherwise we use whichever came from the second input. The resulting array is joined into a string and returned.

share|improve this answer

C, 106 89 bytes

i,j,k;f(s,z)char*s,*z;{for(i=0,j=k=1;j|k;i++)putchar((k=k?z[i]:0)>46|!(j=j?s[i]:0)?k:j);}

Test live on ideone.

share|improve this answer

Retina, 55

$

+`(.?(\S* )(\w)|(\S)(\S* ).?)(\S* .*)
$2$5$6$3$4


Line 5 is a single space. Line 6 is an empty line (with no trailing newline).

Try it online.

I started this one in GNU sed, (with the -r option). Straightforward port to Retina once I got the regexes figured out. The sed version is:

s/$/ /
:
s/(.?(\S* )(\w)|(\S)(\S* ).?)(\S* .*)/\2\5\6\3\4/
t
s/ *//
share|improve this answer
1  
The Retina version fails with a..k.f....b c...f.g...g. => .c..kffg...g – randomra Mar 5 at 11:34

Python 2, 70 bytes

lambda x,y:"".join([k if k!="."and k else j for j,k in map(None,x,y)])

Try it here!

First we create zip both strings into one list. If the second string is longer than the first, it is padded with None (map(None,x,y) does that).
Then we iterate over this list with j being the character from the first string and k the one from the second string. We choose k if it's not a dot and otherwise j.

This could be 61 bytes if I could output the result as list of characters instead of a string.

share|improve this answer

Perl, 48 + 3 = 51 bytes

s/\./substr($^I,$.=pos,1)||$&/ge;$_.=substr$^I,$.

Bah cannot find a shorter solution. (Same approach as @Qwertiy's JavaScript's answer).
Requires -pl and takes input from stdin and -i

$ perl -i'a...ce' -ple's/\./substr($^I,$.=pos,1)||$&/ge;$_.=substr$^I,$.' <<< '..b.d..f'
a.b.de..f
share|improve this answer
    
Pre-extend the target string (which looks very pretty too): $_^=$^I^$^I;s/\.|\0/substr$^I,pos,1or$&/ge – Ton Hospel Mar 6 at 11:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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