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

Dashboard
Notifications
Mark all as read
Challenges

Merge two strings

+9
−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?

1 comment thread

General comments (2 comments)

5 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

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

0 comment threads

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

0 comment threads

+1
−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

+1
−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

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!