g | x | w | all
Bytes Lang Time Link
072Raku Perl 6 rakudo251002T162722Zxrs
049APLNARS251001T113132ZRosario
048Python250930T040000Zl4m2
061Python250929T172955Z97.100.9
109AWK250929T162137Zxrs
002Vyxal250916T164428Zpacman25
011Jelly210122T010358Zcaird co
00705AB1E201211T204148ZMakonede
126JavaScript ES6170519T104855ZAxarydax
064PHP170520T224007ZJör
174Java170520T221649ZComputro
070Ruby170519T144616ZJenkar
008Brachylog 2170519T055306Zuser6213
026J110223T163013ZEelvex
088Python110223T183753ZYOU
072Ruby 1.9110223T180533ZYOU
085Haskell110223T144155ZJ B
032Perl110223T123433ZJ B
032Golfscript110223T121624Zgnibbler
096Python110223T103230ZHoa Long
098Python110223T114101Zgnibbler

Raku (Perl 6) (rakudo), 72 bytes

->$n {for ^$n.chars {$!=$n~substr($n,0,$_).flip;last if $!==$!.flip};$!}

Attempt This Online!

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]

Attempt This Online!

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)

Attempt This Online!

This response is unfair for two reasons:

  1. It's using more modern input/output rules which allow input and output of a string instead of an integer
  2. 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

Attempt This Online!

Feels heavy, but AWK doesn't have a reverse function.

Vyxal, 2 bytes

Ḃ⋎

Try it Online!

bifurcate and merge join, add a byte if you want output to be a number

Jelly, 11 bytes

D;ⱮUƤ$ŒḂƇḢḌ

Try it online!

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

Try it online!

ηí«.ΔÂ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;
  }
}

PHP, 64 Bytes

for(;strrev($p=$argn.strrev(substr($argn,0,$i++)))!=$p;);echo$p;

Try it online!

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*''}

Try it online!

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])