DEV Community

Abhishek Chaudhary
Abhishek Chaudhary

Posted on

Base 7

Given an integer num, return a string of its base 7 representation.

Example 1:

Input: num = 100
Output: "202"

Example 2:

Input: num = -7
Output: "-10"

Constraints:

  • -107 <= num <= 107

SOLUTION:

class Solution:
    def convertToBase7(self, num: int) -> str:
        if num == 0:
            return "0"
        sign = ""
        if num < 0:
            sign = "-"
            num *= -1
        op = ""
        while num > 0:
            d = num % 7
            num = num // 7
            op = str(d) + op
        return sign + op
Enter fullscreen mode Exit fullscreen mode

Top comments (0)