Участник:Gadaevtamaz/Super Palindromes

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/super-palindromes/

from math import sqrt
from itertools import product
class Solution(object):
    def superpalindromesInRange(self, L, R):
        a = ['012'] * (((len(R) + 1) // 2 + 1) // 2)
        l = int(sqrt(int(L)))
        r = int(sqrt(int(R)))
        s = 0
        for x in product(*a):
            x=''.join(x).lstrip('0') or '0'
            var = (int(x + x[::-1]), int(x[:-1] + x[-1] + x[:-1][::-1]))
            for y in var:
                if l <= y <= r and str(y * y) == str(y * y)[::-1]: 
                    s += 1
        return s+(int(L) <= 9 <=int(R))