37
2
Objective
Create a function to reverse string concatenation
Input
Two strings (alphanumeric + spaces), where one should be subtracted for the other.
- You can assume that the string to be subtracted will never be larger than the other one.
Output
The result from the subtraction
Subtraction
You should remove one string from the start or the end of another string.
If the string is present in the start and in the end, you can only remove one, which one will be removed is up to you.
If the string isn't in the start or in the end, or isn't an exact match, it is an invalid subtraction and you should output the original string.
Test Cases
Valid Subtraction
'abcde','ab' -> 'cde'
'abcde','cde' -> 'ab'
'abab','ab' -> 'ab'
'abcab','ab' -> 'abc' or 'cab'
'ababcde','ab' -> 'abcde'
'acdbcd','cd' -> 'acdb'
'abcde','abcde' -> ''
'abcde','' -> 'abcde'
'','' -> ''
Invalid Subtraction (returns original string)
'abcde','ae' -> 'abcde'
'abcde','aa' -> 'abcde'
'abcde','bcd' -> 'abcde'
'abcde','xab' -> 'abcde'
'abcde','yde' -> 'abcde'
Invalid Input (don't need to be handled)
'','a' -> ''
This is code-golf, so the shortest code in bytes wins!
4Why is the result of the first case not
cde
? What do you mean by valid? Do we need to judge the validity of the input, or do you mean that we will not receive invalid inputs? – Leaky Nun – 2017-05-16T16:01:40.770@LeakyNun was a typing error, fixed – Rod – 2017-05-16T16:03:27.347
I think the empty string edge-case should probably be in the invalid section - mind you I suppose it doesn't make any difference to the result :) – Jonathan Allan – 2017-05-16T16:14:35.637
I think you should drop the terms "valid" and "invalid", as they have a common meaning on PPCG which you are clearly not intending. – Ørjan Johansen – 2017-05-16T16:22:00.350
7Damn you,
'abcde','bcd' -> 'abcde'
, for breaking my solution – John Dvorak – 2017-05-16T16:26:07.9075Can we assume the strings will be regex-safe (alphanumeric + spaces)? – John Dvorak – 2017-05-16T16:27:07.603
2I'd suggest
'ababcde', 'ab'
→'abcde'
as a test case. Some naive algorithms fail on that one. – None – 2017-05-16T16:48:18.420@ais523 good point – Rod – 2017-05-16T16:53:22.280
Wait, why
'abcde','bcd' -> 'abcde'
?abcde
containsbcd
, and should therefore returnae
. – MD XF – 2017-05-16T19:42:37.287@MDXF the objective here is "reverse a concatenation", not just to remove a substring c: – Rod – 2017-05-16T19:45:19.690
@Rod Yeah, just noticed that. Whoops. – MD XF – 2017-05-16T19:45:35.323
2@Rod You might consider retitling the challenge "Reverse string concatenation"? – MD XF – 2017-05-16T19:52:15.740
Suggested Testcase
AbcdEbcd, bcd
=>AbcdE
and notAEbcd
– Wernisch – 2017-08-09T13:30:47.610@Wernisch I guess this is covered by
'acdbcd','cd' -> 'acdb'
, right? – Rod – 2017-08-09T13:33:20.743@Rob, You are right, apologies. Had incorrectly copied that test case. – Wernisch – 2017-08-09T13:44:33.667