Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Notifications
Mark all as read
Challenges

Merge two strings

+11
−0

Challenge

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

(Inspired by https://chat.stackexchange.com/transcript/message/57816868#57816868 )

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?
You might want to add some details to your flag.
Why should this post be closed?

3 comment threads

What characters are allowed? (1 comment)
Can I take input in reverse order? (1 comment)
General comments (2 comments)

8 answers

+5
−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?
You might want to add some details to your flag.

0 comment threads

+4
−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?
You might want to add some details to your flag.

1 comment thread

You might want to verify that that input format is acceptable. (1 comment)
+4
−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?
You might want to add some details to your flag.

0 comment threads

+2
−0

Jelly, 13 bytes

W;;Ƥ@ḣL}¹⁼ʋƇḢ

Try it online!

Takes the two strings as arguments in reverse order.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Japt -h, 13 12 9 bytes

à m+V kbU

Try it

à m+V kbU     :Implicit input of strings U & V
à             :Combinations of U
  m           :Map
   +V         :  Append V
      k       :Filter elements that return falsey (i.e., 0)
       bU     :  First 0-based index of U
              :Implicit output of last element
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Python 3, 82 81 bytes

lambda a,b:[a[:i]+b for i in range(len(a)+2)if i>len(a)or b[:len(a)-i]==a[i:]][0]

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Vyxal, 1 byte

Try it Online!

Builtin be like

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 40 bytes

f=->a,b{b.index(a)==0?b:a[/./m]+f[$',b]}

Attempt This Online!

If we could assume that string contains only letters and numbers (or to be more specific, no characters like \t\n\v\f\r \#$()*+-.?[\\]^{|}) then it could be improved to 36 bytes:

f=->a,b{b=~/^#{a}/?b:a[/./]+f[$',b]}

Attempt This Online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate