Suppose we have a number n, we have to find the base 3 equivalent of this number as string.
So, if the input is like 17, then the output will be 122.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, n): sign = '-' if n<0 else '' n = abs(n) if n < 3: return str(n) s = '' while n != 0: s = str(n%3) + s n = n//3 return sign+s ob = Solution() print(ob.solve(17))