Python - Short and Simple with Explanation and Resources
Explanation below code snippet - hope this helps!\n\nIf you are confused about the conversion process from base 10 (numbers we are used to) to base 7 read this:
Explanation below code snippet - hope this helps!\n\nIf you are confused about the conversion process from base 10 (numbers we are used to) to base 7 read this:\n\nhttps://runestone.academy/runestone/books/published/pythonds/BasicDS/ConvertingDecimalNumberstoBinaryNumbers.html\n\n```\nclass Solution:\n    def convertToBase7(self, num: int) -> str:\n        \n        if not num:\n            return \'0\'\n        \n        sign = num < 0\n        num = abs(num)\n        \n        stack = []\n        while num:\n            stack.append(str(num % 7))\n            num = num // 7\n        \n        return \'-\'*sign + \'\'.join(stack[::-1])\n```\n\nThe first two lines simply handle the edge case that num is zero.  \n\nNext, we check to see if num is negative, if it is, lets make it positive using abs, but remember that it was negative with the variable sign.  (We will take the original sign of the number into account in the return statement.)\n\nNext, iteratively divide num by 7 and at each iteration, keep track of the remainder in a stack.  These remainders are the digits that will go into our result.  However, they are in the reverse order of what we need.  \n\nOnce num equals zero, the while loop will break.  Join all of the elements in the stack together in reverse order, and add the sign back in.  If the number was positive, then ```sign = False``` and  ```"-" * False = ""``` so the negative sign will not be included in the result.  \n\nSide note: The list we use to store the digits was named stack because using:\n\n```\nwhile stack:\n\tres += stack.pop()\n```\n\nwould achieve the same outcome as ```\'\'.join(stack[::-1])```
[]