Posted in GCJ

GCJ Post #7

Continuing with my said goal ( solving 5 problems per day) This is what I did for the 2 days…

((งง •̀•̀__•́•́))งง

I had planned to put 5 problems of the GCJ types and discuss post and lot picked one for the next 2 weeks until July 10th.But I suddenly felt that I was going too easy on myself , so I started doing 20 sums a day since yesterday until I am not able to do 20 sums per day…
.
.
.

So doing that I ended up overworking myself… So I ended up cutting down to 3 problems per day… I think I am gradually but definitely losing my motive to seriously participate in the upcoming contest … *sigh 

.
.
.
Maybe this is what they refer to as anxiety attack!!!


So the list of problems that were chosen the last 2 days were:

  1. Bytelandian Gold Coins
  2. Prime Generator
  3. Clossing the tweets
  4. Marbles
  5. The Next Palindrome
  6. Root of the problem
  7. Flipping coins
  8. Array Transforms
  9. Number of Factors
  10. Mixtures
  11. Sreeja nd Votes
  12. Pairwise AND sum
  13. One Dimensional Kingdoms
  14. AND Operations

So the lot picked problem for today is

The next Palindrome…

The problem statement :

A positive integer is called a palindrome if its representation in the decimal system is the same when reading from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input

The first line contains integer t, the number of test cases. Followed by t lines containing integers K.

Output

For each K, output the smallest palindrome larger than K.

Example

Input:

2
808
2133

Output:

818
2222

Explanation

Well, this problem is kinda straight forward so I’ll just get down with the code… *sigh

def near_palindrome(n):
   if len(n) % 2 == 0: return n[:len(n) // 2] + n[:len(n) // 2][::-1]
   return n[:len(n) // 2] + n[len(n) // 2] + n[:len(n) // 2][::-1]

for t in range(int(input())):
   n = input()
   temp = near_palindrome(n)
   if temp > n:
      print(temp)

   else:
      j = len(n) // 2 + len(n) % 2 – 1
      while n[j] == ‘9’ and j >= 0: j -= 1
      if j >= 0: 
         n = n[:j] + str(int(n[j]) + 1) + ‘0’ * (len(n) // 2 + len(n) % 2 – j – 1) + n[len(n) // 2 + len(n) % 2:]
      else: 
         n = ‘1’ + ‘0’ * (len(n) // 2 + 1) + n[len(n) // 2 + 1:]
      print(near_palindrome(n))

o(╥﹏╥)o
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s