Make a number palindrome

12

2

Write a function that takes a number as an argument and makes it a palindrome by appending minimum number of digits. The number will be at max of 100 digits.

Sample Inputs
12
122
232
2323
1012121
Sample Outputs
121
1221
232
23232
101212101

fR0DDY

Posted 2011-02-23T09:15:49.023

Reputation: 4 337

http://golf.shinh.org/p.rb?palindromize for reference lengths – Nabb – 2011-02-26T00:28:36.573

Can the sample inputs and outputs include an example where the correct answer is an even number of digits, just to make sure that submitters cover that case? It would seem to me that some algorithms might fail if the halfway point is between digits instead of on a digit. – Computronium – 2017-05-24T15:56:06.513

1@Computronium Done. – fR0DDY – 2017-05-25T03:01:16.897

Answers

4

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

Eelvex

Posted 2011-02-23T09:15:49.023

Reputation: 5 204

10

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.

J B

Posted 2011-02-23T09:15:49.023

Reputation: 9 638

5

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

user62131

Posted 2011-02-23T09:15:49.023

Reputation:

3

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

YOU

Posted 2011-02-23T09:15:49.023

Reputation: 4 321

nice with the pop. pity you can't pop from str – gnibbler – 2011-02-23T20:09:36.743

2

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

Hoa Long Tam

Posted 2011-02-23T09:15:49.023

Reputation: 1 902

You can replace s=str(n) with s=n. – fR0DDY – 2011-02-23T10:50:39.737

@fR0DDY, that wont work if n is large enough to need a long – gnibbler – 2011-02-23T11:32:56.113

@fR0DDY, Python doesn't care so much about ints vs longs anymore. int(2346765434567875432456) returns 2346765434567875432456 on v2.6.5. I don't see how s=n helps; I need s to be a string so I can subscript to get digit ranges. What's the reasoning there? – Hoa Long Tam – 2011-02-23T11:55:57.067

@Hoa, I think fR0DDY had backticks there but they don't show up in the comments – gnibbler – 2011-02-23T12:18:39.693

@Hoa It was s=[tick]n[tick]. – fR0DDY – 2011-02-23T13:53:26.440

Ah, that makes more sense. – Hoa Long Tam – 2011-02-23T15:42:55.930

1

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

gnibbler

Posted 2011-02-23T09:15:49.023

Reputation: 14 170

Not sure I'm using this right, but: "NameError: global name 'next' is not defined" – J B – 2011-02-23T12:21:47.397

@J B, Ah, you need python2.6 for that :) otherwise it can be written return(...).next() usually that would cost an extra char, but I get to drop the space after return. Hoa has improved on it again anyhow by using a LC instead of a GE – gnibbler – 2011-02-23T20:07:33.033

1

Golfscript - 32 chars

{`:s-1%:r,,{s<r+..-1%=*}%{}?~}:f

gnibbler

Posted 2011-02-23T09:15:49.023

Reputation: 14 170

1

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

J B

Posted 2011-02-23T09:15:49.023

Reputation: 9 638

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

YOU

Posted 2011-02-23T09:15:49.023

Reputation: 4 321

x*'' instead of x.join saves 2 chars. – steenslag – 2011-02-23T18:10:51.457

1nice one @steenslag, thanks for teaching, I am ruby newbie :-) – YOU – 2011-02-23T18:12:16.023

1

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;
  }
}

Axarydax

Posted 2011-02-23T09:15:49.023

Reputation: 166

1

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

Jenkar

Posted 2011-02-23T09:15:49.023

Reputation: 211

0

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.

Computronium

Posted 2011-02-23T09:15:49.023

Reputation: 151

0

PHP, 64 Bytes

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

Try it online!

Jörg Hülsermann

Posted 2011-02-23T09:15:49.023

Reputation: 13 026