title: SPOJ The Next Palindrome
body: <p>I am trying to solve SPOJ problem 5: find the next largest integer "palindrome" for a given input; that is, an integer that in decimal notation reads the same from left-to-right and right-to-left.</p>
  
  <p>Please have a look of the question <a href="http://www.spoj.pl/problems/PALIN/" rel="nofollow">here</a></p>
  
  <p>Instead of using brute force search, I try to calculate the next palindrome. But my code still returns <strong>TLE</strong> (that is, Time Limit Exceeded) and I am frustrated... Would you mind giving me a hint?</p>
  
  <p>Here is my code in python 3.x</p>
  
  <pre><code>if __name__ == '__main__':
      n = int(input())
      for i in range(n):
          string = input()
          length = len(string)
          ans = ""
          if length %2 == 0 :
              half = length // 2
              str_half = string[0:half]
              ans =  str_half + str_half[::-1]         
              if(ans &lt;= string):
                  str_half = str(int(str_half) + 1)
                  ans = str_half + (str_half[0:half])[::-1]
              print(ans)
          else:
              half = length // 2
              str_half = string[0:half]
              ans = str_half + string[half] + str_half[::-1]
              if(ans&lt;= string):            
                  str_half = str(int(str_half+string[half]) + 1)
                  ans = str_half + (str_half[0:half])[::-1]
              print(ans)
  </code></pre>