21
\$\begingroup\$

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!

asked Mar 4, 2016 at 22:14
\$\endgroup\$
7
  • \$\begingroup\$ Is input a.....b ..c....... possible? What's the output then? \$\endgroup\$ Commented Mar 4, 2016 at 22:22
  • \$\begingroup\$ @DonMuesli That would become a.c...b.... \$\endgroup\$ Commented Mar 4, 2016 at 22:22
  • \$\begingroup\$ Can we output a list of characters instead of a string? \$\endgroup\$ Commented Mar 5, 2016 at 0:49
  • \$\begingroup\$ @DenkerAffe No, sorry \$\endgroup\$ Commented Mar 5, 2016 at 0:51
  • \$\begingroup\$ Can the strings be taken in the opposite order? \$\endgroup\$ Commented Mar 5, 2016 at 4:05

19 Answers 19

16
\$\begingroup\$

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.
answered Mar 4, 2016 at 22:19
\$\endgroup\$
1
  • 5
    \$\begingroup\$ Nice eu / el trick! \$\endgroup\$ Commented Mar 4, 2016 at 22:26
11
\$\begingroup\$

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.
answered Mar 4, 2016 at 22:36
\$\endgroup\$
3
  • 6
    \$\begingroup\$ NOOO! You can't adopt Jelly too! We will be left with all the mods as golfing masters. \$\endgroup\$ Commented Mar 5, 2016 at 4:47
  • \$\begingroup\$ @rikerw haha, why d'you think they're mods? cause they're good at golf :P \$\endgroup\$ Commented Mar 5, 2016 at 5:07
  • 4
    \$\begingroup\$ @RikerW I just had to preempt someone else beating me by porting my own solution to Jelly. ¯\_(ツ)_/¯ \$\endgroup\$ Commented Mar 5, 2016 at 10:11
6
\$\begingroup\$

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])
answered Mar 4, 2016 at 22:35
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This will fail for input such as: f('c', 'a....b') \$\endgroup\$ Commented Mar 5, 2016 at 9:00
  • \$\begingroup\$ @dev-null, fixed \$\endgroup\$ Commented Mar 5, 2016 at 21:14
5
\$\begingroup\$

Pyth, 11

smenD\.d.TQ

Try it online or run the Test Suite

answered Mar 4, 2016 at 22:23
\$\endgroup\$
0
4
\$\begingroup\$

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
answered Mar 4, 2016 at 22:22
\$\endgroup\$
4
\$\begingroup\$

Octave, 50 bytes

function c=m(a,b)c=b;c(a>0)=a;i=b>46;c(i)=b(i);end
answered Mar 4, 2016 at 23:02
\$\endgroup\$
1
  • \$\begingroup\$ You can replace != by > \$\endgroup\$ Commented Mar 5, 2016 at 2:24
3
\$\begingroup\$

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!

answered Mar 4, 2016 at 23:16
\$\endgroup\$
1
  • \$\begingroup\$ "you can assume that the input only contains periods and lowercase letters", so you could check c<'a' to save a byte. \$\endgroup\$ Commented Mar 5, 2016 at 1:28
3
\$\begingroup\$

Python 2, 47 bytes

lambda s,t:`map(max,s.upper(),t)`[2::5].lower()
answered Mar 5, 2016 at 6:01
\$\endgroup\$
2
  • \$\begingroup\$ very golfy! I'm looking for a way to get rid of upper() and lower() but no luck so far... \$\endgroup\$ Commented Mar 5, 2016 at 9:35
  • \$\begingroup\$ @Max you sadly can't... this program uses the fact that '.'<uppercase<lowercase. \$\endgroup\$ Commented Jan 8, 2023 at 12:24
2
\$\begingroup\$

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.

answered Mar 4, 2016 at 22:33
\$\endgroup\$
2
\$\begingroup\$

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.

answered Mar 4, 2016 at 22:52
\$\endgroup\$
1
\$\begingroup\$

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/ *//
answered Mar 5, 2016 at 0:15
\$\endgroup\$
1
  • 1
    \$\begingroup\$ The Retina version fails with a..k.f....b c...f.g...g. => .c..kffg...g \$\endgroup\$ Commented Mar 5, 2016 at 11:34
1
\$\begingroup\$

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.

answered Mar 5, 2016 at 0:49
\$\endgroup\$
1
\$\begingroup\$

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
answered Mar 5, 2016 at 8:56
\$\endgroup\$
1
  • \$\begingroup\$ Pre-extend the target string (which looks very pretty too): $_^=$^I^$^I;s/\.|0円/substr$^I,pos,1or$&/ge \$\endgroup\$ Commented Mar 6, 2016 at 11:27
1
\$\begingroup\$

05AB1E (legacy), 6 bytes

u‚ζ€àl

Port of @MartinEnder's CJam answer

Input as two loose strings; output as a list of characters.

Try it online or verify all test cases.

Explanation:

u # Uppercase the first (implicit) input-string
 ‚ # Pair it together with the second (implicit) input-string
 ζ # Zip/transpose; swapping rows/columns,
 # implicitly using " " as filler for unequal length strings
 € # Map over each pair:
 à # Leave its maximum, based on the codepoint of the character
 l # Convert each character back to lowercase
 # (after which the resulting character-list is output implicitly)

Uses the legacy version of 05AB1E, because the min/max builtins work on characters based on codepoints.
In the new version of 05AB1E, (削除) this would have been 7 bytes (削除ここまで) it's 6 bytes as well with I/O as character lists:

uζε{θl

Two loose inputs as a list of characters; output as a list of characters as well.

Try it online or verify all test cases.

Explanation:

u # Uppercase the characters of the first (implicit) input-list
 ζ # Zip/transpose it with the second (implicit) input-list; swapping rows/columns,
 # implicitly using " " as filler for unequal length strings
 ε # Map over each pair:
 { # Sort its characters from lowest to highest based on codepoints
 θ # Pop and leave the last/highest character
 l # Convert this character to lowercase
 # (after which the resulting character-list is output implicitly)

†: With inputs as strings, it would use the uppercased first input as filler explicit filler instead of the implicit space, and will zip the second input with itself.

answered Apr 11, 2024 at 13:23
\$\endgroup\$
1
\$\begingroup\$

R, (削除) 137 (削除ここまで) 126 bytes

\(a,b,`!`=nchar,y=`if`((!b)>!a,c(a,b),c(b,a)),x=strsplit(y,""))Reduce(paste0,Map(max,names(Map(c,el(x),u<-x[[2]])),u,na.rm=T))

Attempt This Online!

My answer to this challenge is the largest one because R has default vector recycling. The code's key piece is the call Map(max,a,b), where a and b are characters vectors. This outputs the list of characters with a larger value - similar to the other solutions here.

But that works only on the strings with the same size. If the strings are of different length, than the shortest one is "recycled", meaning that the missing elements are added from the beginning of the same vector. For example, strings "a.....b" and "..c......." will be merged to "a.c...ba.." instead of "a.c...b...".

As far as I know, it is impossible to deactivate vector recycling in vector-operating functions like Map, rbind, paste etc. Luckily, I have noticed that names adds NAs to the end if the names vector is shorter than the host. Now that the shortest vector has the proper length, it could be passed to max function with na.rm=TRUE filter, which removes NAs.

Finally, there is a statement y=`if`((!b)>!a,c(a,b),c(b,a)) which is choosing the shortest string and puts it to the first place, so that it will be properly converted with the names as shown above.

Other notable tricks used here:

  • Reduce(paste0,x), 16 bytes instead of paste0(x,collapse=""), 21 bytes
  • el(x), 5 bytes instead of x[[1]], 6 bytes.

Vanilla version - R, 122 bytes

Employs a for cycle.

\(a,b,`!`=nchar,x=strsplit(c(a,b),""),d={}){for(j in 1:max(!a,!b))d=Reduce(paste0,c(d,max(el(x)[j],x[[2]][j],na.rm=T)));d}

Attempt This Online!

answered Apr 11, 2024 at 12:00
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 49 bytes

f=([a,...b],[c,...d])=>a+c?(c>{}?c:a||c)+f(b,d):d

Try it online!

answered Jan 8, 2023 at 10:39
\$\endgroup\$
0
\$\begingroup\$

PHP>=7.1, 58 Bytes

for([,$x,$y]=$argv;~$c=$y[$i++];)$c<_?:$x[$i-1]=$c;echo$x;

Online Version

answered May 28, 2017 at 13:26
\$\endgroup\$
1
  • \$\begingroup\$ Undefined constant _ \$\endgroup\$ Commented Jan 9, 2023 at 12:04
0
\$\begingroup\$

q/kdb+, (削除) 43 (削除ここまで) 40 bytes

Solution:

lower{l:max(#:)each(x;y);upper[l$x]|l$y}

Example:

q)lower{l:max(#:)each(x;y);upper[l$x]|l$y}["a..b..";"...c"]
"a..c.."

Explanation:

(#:) // k equivalent of count
max (#:) each(x;y) // takes each string, count the length, return maximum
l$x // whites-space pad string x to length l
| // take the maximum (per Martin's strategy)
upper[...] // convert padded string 1 to uppercase
lower{...} // convert result of function to lowercase

Notes: I'm taking taking advantage of "Given two non-empty strings" and assuming that inputs are strings. In kdb "c" is an atom, (),"c" is a string, otherwise need to add 6 bytes to the score, as we cannot use $ to pad an atom...

answered May 28, 2017 at 10:59
\$\endgroup\$
0
\$\begingroup\$

C (GCC), (削除) 86 (削除ここまで) 76 bytes

-10 bytes thanks to @ceilingcat

f(x,y)char*x,*y;{for(;*x|*y;putchar(0[y=*y?y:x]-46?*y:*x),x++,y++)x=*x?x:y;}

Attempt This Online!

answered Jan 8, 2023 at 19:31
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.