-1

Beginner here. I did "Leetcode 9. Palindrone Number" and think I figured out the solution, but as a beginner I am not familiar with some basic Python formats.

Leetcode Problem =

  • Given an integer x, return true if x is a palindrome, and false otherwise.

My Code Solution =

`input_string_for_test = "1155215511"
    
bucket_1 = []
bucket_2 = []
boolean_bucket = 0

bucket_1.append(input_string_for_test)
bucket_2.append(input_string_for_test[::-1])

'''
print(bucket_1)
print(bucket_2)
'''

if bucket_1 == bucket_2:
    boolean_bucket =+ 1
    '''
    print("PALINDROME")
    '''
    print(bool(boolean_bucket))
    
else:
    '''
    print("NO")
    '''
    print(bool(boolean_bucket))`

**The Leetcode required format = **

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """

How can I convert my code into what Leetcode requires, so that I can submit my attempt?

Many thanks!

Im not familiar/fluent with Syntax because I am just a hobbyist who enjoys coding in my free time.

I tried to change "bucket_1.append(input_string_for_test" to "bucket_1.append(x)", but the error message was this:

TypeError: 'int' object has no attribute '__getitem__'
    bucket_2.append(x[::-1])
Line 12 in isPalindrome (Solution.py)
    ret = Solution().isPalindrome(param_1)
Line 43 in _driver (Solution.py)
    _driver()
Line 53 in <module> (Solution.py)

  • `x` is an int, but in the code you wrote you're assuming it's a string. You can turn it into a string with `x = str(x)`. – Loocid May 09 '23 at 05:15
  • "Im not familiar/fluent with Syntax because I am just a hobbyist who enjoys coding in my free time." - then you should *study these fundamentals* from a *Python language tutorial* first, *before* trying to use a competitive or self-test coding site such as Leetcode. – Karl Knechtel May 09 '23 at 06:47

4 Answers4

1

You just need to convert integer to string and to check if this string is a palindrome.

class Solution(object):
    def isPalindrome(self, x):
        s = str(x)
        return s == s[::-1]
Yuri Ginsburg
  • 2,302
  • 2
  • 13
  • 16
0
class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        bucket_1 = []
        bucket_2 = []
        boolean_bucket = 0

        bucket_1.append(x)
        bucket_2.append(x[::-1])
        if bucket_1 == bucket_2:
            boolean_bucket += 1
        return bool(boolean_bucket)

just change the format, not sure it is True or not and enjoy coding.

ChenChao
  • 39
  • 6
0

In your code solution, the type of 'input_string_for_test' is string, however Leetcode requires the type of 'x' is int. So you can write as bucket_1.append(str(x))

oldtank
  • 1
  • 1
0

You just need to do three things:

  1. Put your code inside the function
  2. Consider the parameters provided with the function as your input
  3. Return your output in the format they asked (there are some problems that require altering the input without any output)

You don't need to run any function, it's run on the test cases when you hit run