30
4
Introduction
For the ones who don't know, a palindrome is when a string is equal to the string backwards (with exception to interpunction, spaces, etc.). An example of a palindrome is:
abcdcba
If you reverse this, you will end up with:
abcdcba
Which is the same. Therefore, we call this a palindrome. To palindromize things, let's take a look at an example of a string:
adbcb
This is not a palindrome. To palindromize this, we need to merge the reversed string into the initial string at the right of the initial string, leaving both versions intact. The shorter, the better.
The first thing we can try is the following:
adbcb
bcbda
^^ ^^
Not all characters match, so this is not the right position for the reversed string. We go one step to the right:
adbcb
bcbda
^^^^
This also doesn't match all characters. We go another step to the right:
adbcb
bcbda
This time, all characters match. We can merge both string leaving the intact. The final result is:
adbcbda
This is the palindromized string.
The Task
Given a string (with at least one character) containing only lowercase letters (or uppercase, if that fits better), output the palindromized string.
Test cases
Input Output
abcb abcba
hello hellolleh
bonobo bonobonob
radar radar
hex hexeh
This is code-golf, so the submission with the least amount of bytes wins!
3Related. – Zgarb – 2016-04-04T18:24:11.017
6You should specify that the reversed string has to be merged into the original string with the reversed one at the right. If it can go at left,
obonobo
would be a better solution to the test case. – Level River St – 2016-04-04T21:04:42.4672Related 2 – Sp3000 – 2016-04-05T01:16:53.973
2@LevelRiverSt +1 just because "obonobo" is such an amazing word – Nathaniel – 2016-04-05T12:57:17.283
1@Nathaniel Thanks but
bono b o nob
is an entire sentence. What's the difference between God and Bono? God doesn't wander round Dublin pretending to be Bono ;-) – Level River St – 2016-04-05T14:07:29.490Related 3: revenge of the relations – Mego – 2016-04-27T07:04:49.627
Why is this question so ridiculously long-winded? The whole task can literally be summarised in a sentence, there's no need for such a thorough (and weird) explanation. – theonlygusti – 2017-02-06T17:56:43.740