dailylog 04-05-21

less than 1 minute read

Hopping back into leetcode on EASY!!

This bad code (brute force) was the product of 30 minutes of hammering away at test cases. A more thoughtful approach (and a nice refactor) is needed :)

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        # print(x)
        negative = False

        if x == 0:
            return 0
        if x < 0:
            negative = True
            x = abs(x)

        str_num = [d for d in str(x)]
        new_x = []
        for i in str_num:
            new_x.insert(0, i)
            # print(new_x)

        result = ''.join(new_x)
        if int(result) > ((2**31) -1):
            return 0
        if negative:
            result = '-' + result

        return(int(result))
        # print(''.join(new_x))