Non-decimal radices/Convert: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: Split long line)
Line 848: Line 848:


=={{header|Python}}==
=={{header|Python}}==
<lang python>def baseN(num,b):
<lang python>digits = "0123456789abcdefghijklmnopqrstuvwxyz"
def baseN(num,b):
return (((num == 0) and "0" )
return (((num == 0) and "0" )
or ( baseN(num // b, b).lstrip("0")
or ( baseN(num // b, b).lstrip("0")
+ "0123456789abcdefghijklmnopqrstuvwxyz"[num % b]))
+ digits[num % b]))


# alternatively:
# alternatively:
Line 859: Line 860:
while num != 0:
while num != 0:
num, d = divmod(num, b)
num, d = divmod(num, b)
result += "0123456789abcdefghijklmnopqrstuvwxyz"[d]
result += digits[d]
return result[::-1] # reverse
return result[::-1] # reverse