forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-7.py
More file actions
26 lines (25 loc) · 634 Bytes
/
base-7.py
File metadata and controls
26 lines (25 loc) · 634 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
"""
@param num: the given number
@return: The base 7 string representation
"""
def convertToBase7(self, num):
# Write your code here
minus = False
if num < 0:
num = -num;
minus = True
digits = []
while num >= 7:
digits.append(num % 7)
num = int(num / 7)
digits.append(num)
k = 1
ret = 0
for d in digits:
ret += d * k
k *= 10
if minus:
return str(-ret)
return str(ret)
# easy: https://www.lintcode.com/problem/base-7/