I defined a function by:
input: two numbers l,k
output: the number k in base l
Code:-
def base(k,l):
result=''
while k!=0:
remainder = k%l
k=k//l
result= result + str(remainder)
return result
The Problem occurs when the numbers are divisible by any power of the base.
For ex:- If base = 2 and k = 2, the output is 01 instead of 10. The same happens in some other numbers as well
You need to prepend the remainder:
result = str(remainder) + result
You are not asking about efficiency, just how to get it to work.
A quick test using
def base(k,l):
result=''
while k!=0:
remainder = k%l
k=k//l
result= str(remainder) + result
return result
works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.