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

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

2 comments

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

3 answers

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

0 comments

+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 comments

+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 comments

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!