← RU Pythoncursus

Uitwerking: Fair and square

from math import sqrt

def is_square(x):
    isqrt = int(sqrt(x))
    return x == isqrt*isqrt

def is_palindrome(x):
    x = str(x)
    for i in range(0, len(x)):
        if x[i] != x[len(x) - i - 1]:
            return False
    return True

# bonus: versie is_palindrome met reverse
def is_palindrome_rev(x):
    x = str(x)
    return x is str(reversed(x))

def find_fair_and_square(mini, maxi):
    count = 0
    for i in range(mini, maxi):
        if is_square(i) and is_palindrome(i):
            count += 1
            print("(", mini, ",", maxi, "): ", i)
    return count

print(find_fair_and_square(0, 100))
print(find_fair_and_square(100, 1000))
print(find_fair_and_square(1000, 100000))

print(is_palindrome_rev(1001))