1 / 4
Jan 2020

I am getting NZEC for my python code and I don’t know why.
Do you have any idea what I can do to overcome that?

def is_palindrome(my_str):
    for k in range(len(my_str)):
        if my_str[k] != my_str[len(my_str)-1-k]:
            return False
    return True


def char_plus_one(string):
    map_dict = {'0': '1', '1': '2', '2': '3', '3': '4', '4': '5',
            '5': '6', '6': '7', '7': '8', '8': '9',
            '9': '10', '10': '11'}
    return map_dict[string]


def plus_one_on_index(my_string, indx):
    if indx == -1:
        return '1' + my_string
    increased = char_plus_one(my_string[indx])
    if len(increased) == 1:
        return my_string[0:indx] + increased + my_string[indx + 1:]
    else:
        return plus_one_on_index(my_string[0:indx] + '0' + my_string[indx + 1:], indx - 1)


def get_next_palindrome(num_str):
    even_odd_delta = len(num_str) % 2  # 1 if odd
    pivot = len(num_str)/2 + even_odd_delta
    mirror_tail = num_str[pivot - 1 - even_odd_delta::-1]

    if mirror_tail < num_str[-pivot+even_odd_delta:]:
        next_guess = plus_one_on_index(num_str, pivot-1)
        return next_guess[0:pivot] + next_guess[::-1][pivot:]
    return num_str[0:pivot] + mirror_tail


runs = int(raw_input().split()[0])

input_numbers = []
for run in range(runs):
    input_numbers.append(raw_input().split()[0])

for number in input_numbers:
    while number[0] == '0' and number != '0':
        number = number[1:]
    if len(number) == 1:
        if number == '9':
            print('11')
        else:
            print(plus_one_on_index(number, len(number)-1))
    elif is_palindrome(number):
        number = plus_one_on_index(number, len(number)-1)
        print(get_next_palindrome(number))
    else:
        print(get_next_palindrome(number))
  • created

    Jan '20
  • last reply

    Jan '20
  • 3

    replies

  • 845

    views

  • 2

    users

  • 1

    like

  • 1

    link

You know how important the correct indentation is to Python right? So are you seriously expecting people to read the above?

Please re-post the code, but before doing so, add four spaces to the front of each line.

Thanks for pointing that out. I reformatted the code and you can see it above.

Th indentation is better but still not fully correct, so I made some guesses.

Are you submitting it as Python 3? Is yes, see here16.

So I replaced raw_input with input, and it at least runs then, but some numbers still result in runtime error.