| Bytes | Lang | Time | Link |
|---|---|---|---|
| 072 | Raku Perl 6 rakudo | 251002T162722Z | xrs |
| 049 | APLNARS | 251001T113132Z | Rosario |
| 048 | Python | 250930T040000Z | l4m2 |
| 061 | Python | 250929T172955Z | 97.100.9 |
| 109 | AWK | 250929T162137Z | xrs |
| 002 | Vyxal | 250916T164428Z | pacman25 |
| 011 | Jelly | 210122T010358Z | caird co |
| 007 | 05AB1E | 201211T204148Z | Makonede |
| 126 | JavaScript ES6 | 170519T104855Z | Axarydax |
| 064 | PHP | 170520T224007Z | Jör |
| 174 | Java | 170520T221649Z | Computro |
| 070 | Ruby | 170519T144616Z | Jenkar |
| 008 | Brachylog 2 | 170519T055306Z | user6213 |
| 026 | J | 110223T163013Z | Eelvex |
| 088 | Python | 110223T183753Z | YOU |
| 072 | Ruby 1.9 | 110223T180533Z | YOU |
| 085 | Haskell | 110223T144155Z | J B |
| 032 | Perl | 110223T123433Z | J B |
| 032 | Golfscript | 110223T121624Z | gnibbler |
| 096 | Python | 110223T103230Z | Hoa Long |
| 098 | Python | 110223T114101Z | gnibbler |
Raku (Perl 6) (rakudo), 72 bytes
->$n {for ^$n.chars {$!=$n~substr($n,0,$_).flip;last if $!==$!.flip};$!}
Doesn't work in ATO.
APL(NARS), 49 chars
{⍎'x',⍨↑k/⍨{⍵≡⌽⍵}¨k←(⊂w),¨⌽¨{w[⍵]}¨(⊂⍬),⍳¨⍳≢w←⍕⍵}
Input one not negative integer (or a big not negative integer), output one big not negative integer (type 'x').
I copy the function in what I understood from post by 'Eelvex' the J solution.
It seems the clue is write the input with a range of index 0,1..1,1..2,1..3,1..4 ecc and reverse each.
For example for input "12" one has to consider
"" the 0 index of "12" and reverse (string null)
"1" the 1 index of "12" and reverse
"21" the 1..2 indeces of "12" (with 2 = max index it means end) and reverse
and concatenate with input for find the first it is a palindrome.
test:
f←{⍎'x',⍨↑k/⍨{⍵≡⌽⍵}¨k←(⊂w),¨⌽¨{w[⍵]}¨(⊂⍬),⍳¨⍳≢w←⍕⍵}
f 12343
1234321
~~~~~~~
f 12
121
~~~
f 122
1221
~~~~
f 232
232
~~~
f 2323
23232
~~~~~
f 289179827910280918298390x
28917982791028091829839093892819082019728971982
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
f 3
3
~
f 33
33
~~
f 111
111
~~~
Python, 48 bytes
f=lambda i:i==i[::-1]and i or i[0]+f(i[1:])+i[0]
Not many points, so this pointed changed better treated not a variant of ord(adam)'s answer
Python, 61 bytes
f=lambda i,s=0:((p:=i+i[:s][::-1])==p[::-1])and p or f(i,s+1)
This response is unfair for two reasons:
- It's using more modern input/output rules which allow input and output of a string instead of an integer
- It's using a version of Python which wasn't available when the question was posed.
I also definitely feel like you can do something cleverer with recursion to avoid having to do the clunky walrus operator, but my attempts didn't go anywhere.
AWK, 109 bytes
func r(t){x=X;for(i=0;i++<l=split(t,a,X);)x=a[i]x;return x}v=r(y=$0){for(m=l;y!=r(y);)y=$0 substr(v,m--)}$0=y
Feels heavy, but AWK doesn't have a reverse function.
Jelly, 11 bytes
D;ⱮUƤ$ŒḂƇḢḌ
How it works
D;ⱮUƤ$ŒḂƇḢḌ - Main link. Takes n on the left
D - Digits of n
$ - To the digits of n:
Ƥ - Yield the prefixes
U - Reverse
;Ɱ - Concatenate with the digits
ŒḂƇ - Keep the palindromes
Ḣ - Take the first (i.e. the shortest)
Ḍ - Convert from digits
05AB1E, 27 7 bytes
-18 bytes thanks to @Razetime.
ηí«.ΔÂQ
ηí«.ΔÂQ # full program
.Δ # find first element in...
« # joined...
í # reversed...
η # prefixes of...
# implicit input...
.Δ # where...
Q # it's equal to...
 # itself reversed
# implicit output
JavaScript (ES6), 145 126 chars
p=a=>{S=x=>x.split``.reverse();for(s=String(a),i=0;i<s.length;i++)if(x=s+S(s.substring(0,i)).join``,x==S(x).join``)return x}
Commented:
function palindrome(n){
s = String(n);
for(i=0;i<s.length;i++)
{
x=s+s.substring(0,i).split("").reverse().join("") //take first n characters, reverse and append to the end
if(x==x.split("").reverse().join("")) //is the number a palindrome?
return x;
}
}
Java, 174 bytes
x->{Function<String,String>r=t->new StringBuilder(t).reverse().toString();String y=r.apply(x),z=x;int m=x.length();while(!z.equals(r.apply(z)))z=x+y.substring(--m);return z;}
Ungolfed:
x -> {
Function<String, String> r = t -> new StringBuilder(t).reverse().toString();
String y = r.apply(x), z=x;
int m = x.length();
while (!z.equals(r.apply(z))) z = x+y.substring(--m);
return z;
}
I have a feeling it could be a lot tighter but it's not immediately obvious to me how. The Function eats up a lot of space but I needed it in two places.
This works for any string, not just numbers, and it can be any length.
Ruby, 70 bytes
f=->x{x=x.to_s.chars;99.times{|i|x.insert~i,x[i]if x!=x.reverse};x*''}
Based on YOU'S answer, with chars instead of .split'' to gain 2 chars. And i'm sure there's way to squeeze just a bit more ><
Brachylog 2, 8 bytes, language postdates challenge
ẹ;AcB↔Bc
Try it online! The question asks for a function, so I provided one; the TIO link takes an argument that runs a function like a full program.
Explanation
ẹ;AcB↔Bc
ẹ Split {the input} into digits
;Ac Append {the shortest possible} list
B↔B to produce a palindrome
c then concatenate the resulting list of digits back into a number
J, 50, 32 26 characters!
f=:{.@(,"1(-:|.)\.#|.@}:\)
eg
f '12'
121
f '232'
232
f '2323'
23232
f '1012121'
101212101
How it works (by example)
y =: '1012121'
[\.y NB. Sub lists of y
1012121
012121
12121
2121
121
21
1
|.\. y NB> Reverses of sub lists of y
1212101
121210
12121
1212
121
12
1
([\. y) -:"1 (|. \. y) NB. Which of them are equal? (those are palindromes)
NB. ( -:"1 ) checks equality item by item
0 0 1 0 1 0 1
(-: |.)\. y NB. Shortcut of the above
0 0 1 0 1 0 1
(0 0 1 0 1 0 1) # }:\y NB. Choose (#) the palindrome prefixes (\)
10
1012
101212
y, |.'10' NB. Reverse and append the first prefix.
101212101
Python, 88 chars
def f(x):
x,y=list(str(x)),[]
while x!=x[::-1]:y+=x.pop(0)
return''.join(y+x+y[::-1])
Ruby 1.9, 72 chars
f=->x{x=x.to_s.split'';99.times{|i|x.insert~i,x[i]if x!=x.reverse};x*''}
Haskell, 85
Using the same algorithm as most everyone else:
import List
r=reverse
f s=s++(r.snd.head.filter g.zip(tails s)$inits s)
g(s,_)=s==r s
Examples from problem description:
*Main> map (f.show) [12,232,2323,1012121]
["121","232","23232","101212101"]
Perl, 32 chars
s/((.)(?1)\2|.?)$/$&.reverse$`/e
Needs Perl 5.10 or later for regex features, but no special command-line switch.
Sample use:
$ perl -pe 's/((.)(?1)\2|.?)$/$&.reverse$`/e' << EOT
> 12
> 232
> 2323
> 1012121
> EOT
121
232
23232
101212101
Uses Perl 5.10's recursive regex extensions to match the longest trailing palindrome as such:
m/
( # paren 1 - a palindrome is either:
(.) # paren 2 - a character
(?1) # a palindrome as defined in paren 1
\2 # the same character as in paren 2
| # or:
.? # a 0- or 1-character string
)
$ # at end of string
/x
It then replaces it with itself ($&) and appends whatever the string started with ($`), reversed.
Golfscript - 32 chars
{`:s-1%:r,,{s<r+..-1%=*}%{}?~}:f
Python (101 96)
edit: Shortened based on @gnibbler's solution
def p(n):s=str(n);r=s[::-1];l=len(s);return[int(s+r[l-i:])for i in range(l)if s[i:]==r[:l-i]][0]
Original:
def p(n):
s=str(n);r=s[::-1];l=len(s)
for i in range(l):
if s[i:]==r[:l-i]:return int(s+r[l-i:])
Python - 98 chars
Based on Hoa's answer :)
def p(n):s=str(n);r=s[::-1];l=len(s);return next(int(s+r[l-i:])for i in range(l)if s[i:]==r[:l-i])