""" 0009.1 - Palindrome Number - Solution 1 - String Conversion """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isPalindrome(self, x: int) -> bool:
"""Palindrome Number Function"""
return str(x) == str(x)[::-1]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isPalindrome(121)) # True
print(Solution().isPalindrome(-121)) # False
print(Solution().isPalindrome(10)) # False
print(Solution().isPalindrome(0)) # True
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
/** 0009.1 - Palindrome Number - Solution 1 - String Conversion */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
/** Palindrome Number Function */
isPalindrome(x) {
if (x < 0) return false;
const s = String(x);
return s === s.split("").reverse().join("");
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
function testcase() {
/** Test Function */
const solution = new Solution();
console.log(solution.isPalindrome(121)); // true
console.log(solution.isPalindrome(-121)); // false
console.log(solution.isPalindrome(10)); // false
console.log(solution.isPalindrome(0)); // true
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
testcase();
/** 0009.1 - Palindrome Number - Solution 1 - String Conversion */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
/** Palindrome Number Function */
isPalindrome(x: number): boolean {
if (x < 0) return false;
const s: string = String(x);
return s === s.split("").reverse().join("");
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
function testcase(): void {
/** Test Function */
const solution = new Solution();
console.log(solution.isPalindrome(121)); // true
console.log(solution.isPalindrome(-121)); // false
console.log(solution.isPalindrome(10)); // false
console.log(solution.isPalindrome(0)); // true
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
testcase();
// 0009.1 - Palindrome Number - Solution 1 - String Conversion
package main
/////////////////////////////////////////////////////////////////////////////////////
// Imports
/////////////////////////////////////////////////////////////////////////////////////
import (
"fmt"
"strconv"
)
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
func isPalindrome(x int) bool {
if x < 0 {
return false
}
s := strconv.Itoa(x)
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
if s[i] != s[j] {
return false
}
}
return true
}
func testcase() {
// Test Function
fmt.Println(isPalindrome(121)) // true
fmt.Println(isPalindrome(-121)) // false
fmt.Println(isPalindrome(10)) // false
fmt.Println(isPalindrome(0)) // true
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
func main() {
testcase()
}
// 0009.1 - Palindrome Number - Solution 1 - String Conversion
/////////////////////////////////////////////////////////////////////////////////////
// Structs
/////////////////////////////////////////////////////////////////////////////////////
struct Solution;
/////////////////////////////////////////////////////////////////////////////////////
// Implementations
/////////////////////////////////////////////////////////////////////////////////////
impl Solution {
/// Palindrome Number Function
pub fn is_palindrome(x: i32) -> bool {
if x < 0 {
return false;
}
let s = x.to_string();
s.chars().eq(s.chars().rev())
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
fn testcase() {
// Test Function
println!("{}", Solution::is_palindrome(121)); // true
println!("{}", Solution::is_palindrome(-121)); // false
println!("{}", Solution::is_palindrome(10)); // false
println!("{}", Solution::is_palindrome(0)); // true
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
fn main() {
testcase();
}
""" 0009.2 - Palindrome Number - Solution 2 - Reverse Half """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isPalindrome(self, x: int) -> bool:
"""Palindrome Number Function"""
# Negative numbers and numbers ending in 0 (except 0 itself) are not palindromes
if x < 0 or (x % 10 == 0 and x != 0):
return False
reversed_half = 0
while x > reversed_half:
reversed_half = reversed_half * 10 + x % 10
x //= 10
# For even-length numbers: x == reversed_half
# For odd-length numbers: x == reversed_half // 10 (drop the middle digit)
return x == reversed_half or x == reversed_half // 10
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isPalindrome(121)) # True
print(Solution().isPalindrome(-121)) # False
print(Solution().isPalindrome(10)) # False
print(Solution().isPalindrome(0)) # True
print(Solution().isPalindrome(12321)) # True
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
/** 0009.2 - Palindrome Number - Solution 2 - Reverse Half */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
/** Palindrome Number Function */
isPalindrome(x) {
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let reversedHalf = 0;
while (x > reversedHalf) {
reversedHalf = reversedHalf * 10 + (x % 10);
x = Math.trunc(x / 10);
}
return x === reversedHalf || x === Math.trunc(reversedHalf / 10);
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
function testcase() {
/** Test Function */
const solution = new Solution();
console.log(solution.isPalindrome(121)); // true
console.log(solution.isPalindrome(-121)); // false
console.log(solution.isPalindrome(10)); // false
console.log(solution.isPalindrome(0)); // true
console.log(solution.isPalindrome(12321)); // true
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
testcase();
/** 0009.2 - Palindrome Number - Solution 2 - Reverse Half */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
/** Palindrome Number Function */
isPalindrome(x: number): boolean {
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let reversedHalf: number = 0;
while (x > reversedHalf) {
reversedHalf = reversedHalf * 10 + (x % 10);
x = Math.trunc(x / 10);
}
return x === reversedHalf || x === Math.trunc(reversedHalf / 10);
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
function testcase(): void {
/** Test Function */
const solution = new Solution();
console.log(solution.isPalindrome(121)); // true
console.log(solution.isPalindrome(-121)); // false
console.log(solution.isPalindrome(10)); // false
console.log(solution.isPalindrome(0)); // true
console.log(solution.isPalindrome(12321)); // true
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
testcase();
// 0009.2 - Palindrome Number - Solution 2 - Reverse Half
package main
/////////////////////////////////////////////////////////////////////////////////////
// Imports
/////////////////////////////////////////////////////////////////////////////////////
import "fmt"
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
func isPalindrome(x int) bool {
if x < 0 || (x%10 == 0 && x != 0) {
return false
}
reversedHalf := 0
for x > reversedHalf {
reversedHalf = reversedHalf*10 + x%10
x /= 10
}
return x == reversedHalf || x == reversedHalf/10
}
func testcase() {
// Test Function
fmt.Println(isPalindrome(121)) // true
fmt.Println(isPalindrome(-121)) // false
fmt.Println(isPalindrome(10)) // false
fmt.Println(isPalindrome(0)) // true
fmt.Println(isPalindrome(12321)) // true
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
func main() {
testcase()
}
// 0009.2 - Palindrome Number - Solution 2 - Reverse Half
/////////////////////////////////////////////////////////////////////////////////////
// Structs
/////////////////////////////////////////////////////////////////////////////////////
struct Solution;
/////////////////////////////////////////////////////////////////////////////////////
// Implementations
/////////////////////////////////////////////////////////////////////////////////////
impl Solution {
/// Palindrome Number Function
pub fn is_palindrome(mut x: i32) -> bool {
if x < 0 || (x % 10 == 0 && x != 0) {
return false;
}
let mut reversed_half: i32 = 0;
while x > reversed_half {
reversed_half = reversed_half * 10 + (x % 10);
x /= 10;
}
x == reversed_half || x == reversed_half / 10
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
fn testcase() {
// Test Function
println!("{}", Solution::is_palindrome(121)); // true
println!("{}", Solution::is_palindrome(-121)); // false
println!("{}", Solution::is_palindrome(10)); // false
println!("{}", Solution::is_palindrome(0)); // true
println!("{}", Solution::is_palindrome(12321)); // true
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
fn main() {
testcase();
}