DEV Community

Abhishek Chaudhary
Abhishek Chaudhary

Posted on

1 1

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)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay