Challenges

# Merge two strings

+8
−0

## Challenge

Given two strings a and b, return the shortest string s so that s starts with a and ends with b.

## Examples

'ABCDEF', 'EFGHI' -> 'ABCDEFGHI'
'AAAAAA', 'AAAAAAAA' -> 'AAAAAAAA'
'ABC', '123' -> 'ABC123'
'', 'ABCDE' -> 'ABCDE'
'ABCD', 'ABCD' -> 'ABCD'
'', '' -> ''


Brownie points for beating my 26 in APL.

Why does this post require moderator attention?
Why should this post be closed?

You need to specify if these strings are taken as program input or if they can be constants. For example in C, you can merge two string literals by just typing "ABC" "DEF". But to merge strings taken as input in run-time, it turns much more intricate. Lundin‭ 15 days ago

The strings should either be function parameters or taken from input. rak1507‭ 15 days ago

+4
−0

# APL(Dyalog Unicode), 26 bytes SBCS

{⊃x/⍨⊃¨⍺∘⍷¨x←,∘⍵¨(⊂⍬),,\⍺}


Try it on APLgolf!

A dfn submission which takes the inputs as left and right argument.

I took way too long to come up with this. Fun challenge.

Why does this post require moderator attention?

+3
−0

# Sed -E, 25 bytes

Takes second input, , comma, first input (inputs cannot contain , commas).

s/^(.*)(.*),(.*)\1\$/\3\2/

Why does this post require moderator attention?

+3
−0

# BQN, 18 bytesSBCS

{⊑(⊑𝕨⊸⍷)¨⊸/∾⟜𝕩¨↑𝕨}


Run online!

A direct translation of Razetime's APL solution (my attempted improvement ⊣∾{⊢´/𝕨⊸«⊸≡¨↑𝕩}↓⊢ turns out to be not at all correct).

The BQN solution is much shorter mainly because it has Prefixes (↑) built in. Being able to filter with ⊸/ (see Before) also helps a lot.

{⊑(⊑𝕨⊸⍷)¨⊸/∾⟜𝕩¨↑𝕨}  # Function with left argument 𝕨 and right argument 𝕩
↑𝕨   # All prefixes of 𝕨
∾⟜𝕩¨     # Append 𝕩 after each one
⊸/         # Filter by...
(    )¨           #   On each string,
𝕨⊸⍷             #     Where does 𝕨 appear as a substring?
⊑                #     But I only care if it's the first one
⊑                  # Then take the first


Put together, the pattern ⊑𝕨⊸⍷ tests if 𝕨 is a prefix of the argument.

Why does this post require moderator attention?