Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
For changed mission - Explained solution in Clear category for Integer Palindrome by Selindian
def int_palindrome(number: int, B: int) -> bool:
# The changed mission requires to convert the number to another base and check if that's a palindrom
# For example 445 Base 2 ==> 111000111 ==> True as it doesn't matter if read from left or right
# 2nd example 961 Base 5 ==> 12321 ==> True as it doesn't matter if read from left or right
# 3rd example 34 Base 2 ==> 100010 ==> False as reading direction matters
num = [] # Initialyze and empty list for the converted number
while number: # Loop: To convert the number to base B, it must be divided by the base until it is 0.
number, rem = divmod(number, B) # ... Divide number by base ...
num.insert(0, rem) # ... the remaining will be the next digit of our converted number
return num == num[::-1] # Now just check (return) list and inverted list
print("Example:")
print(int_palindrome(455, 2))
assert int_palindrome(6, 2) == False
assert int_palindrome(34, 2) == False
assert int_palindrome(455, 2) == True
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 21, 2022
Comments: