Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if number is palindrome or not in Octal in Python
A palindrome is a number that reads the same forwards and backwards. In this problem, we need to check if a given number is a palindrome in its octal representation. If the input number is already in octal format (all digits < 8), we check it directly. If it's in decimal format, we first convert it to octal, then check if the result is a palindrome.
For example, if the input is 178, it contains digit 8 which is not valid in octal, so it's treated as decimal. Converting 178 to octal gives 262, which is a palindrome.
Algorithm Steps
- Determine if the number is in octal (all digits < 8) or decimal format
- Set base = 8 if number contains digits ? 8, otherwise base = 10
- Convert the number to its representation in the determined base
- Check if the resulting digits form a palindrome
Implementation
def is_all_under_8(num):
"""Check if all digits in the number are less than 8"""
while num:
if (num % 10) >= 8:
return False
else:
num = int(num / 10)
return True
def solve(num):
"""Check if number is palindrome in octal representation"""
# If number has digits >= 8, it's decimal, so use base 8 for conversion
# If all digits < 8, it's already octal, so use base 10 for checking
base = 8 if (is_all_under_8(num) == False) else 10
# Convert number to list of digits in the determined base
digits_list = []
while num != 0:
digits_list.append(num % base)
num = int(num / base)
# Check if the digits form a palindrome
j = len(digits_list) - 1
k = 0
while k <= j:
if digits_list[j] != digits_list[k]:
return False
j -= 1
k += 1
return True
# Test the function
num = 178
result = solve(num)
print(f"Is {num} a palindrome in octal? {result}")
# Let's also show the octal conversion
print(f"178 in decimal = {oct(178)} in octal = 262")
print("262 reversed is also 262, so it's a palindrome")
Is 178 a palindrome in octal? True 178 in decimal = 0o262 in octal = 262 262 reversed is also 262, so it's a palindrome
How It Works
The algorithm first determines the input format by checking if any digit is 8 or 9. If found, the number is treated as decimal and converted to octal (base 8). If all digits are below 8, it's already in octal format.
The conversion process stores digits in reverse order in a list. Finally, we compare digits from both ends moving inward to check for palindrome property.
Additional Example
# Test with a number that's already in octal format
num1 = 121 # All digits < 8, so treated as octal
result1 = solve(num1)
print(f"Is {num1} a palindrome in octal format? {result1}")
# Test with another decimal number
num2 = 85 # Contains 8, so treated as decimal
result2 = solve(num2)
print(f"Is {num2} a palindrome in octal? {result2}")
print(f"85 in decimal = {oct(85)} in octal = 125 (not a palindrome)")
Is 121 a palindrome in octal format? True Is 85 a palindrome in octal? False 85 in decimal = 0o125 in octal = 125 (not a palindrome)
Conclusion
This solution efficiently determines whether a number is a palindrome in its octal representation by first identifying the input format, then performing base conversion if needed, and finally checking the palindrome property using a two-pointer approach.
