Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
https://repl.it/C7Dk/1 created by charles2588
#Recursive way to convert integer to string
def int2string(num):
#Base case:
if num<10:##check if it is single digit number
return stringlookup(num)
else:
return int2string(int(num/(10**1)))+stringlookup(int(num%(10**1)))
def stringlookup(n):
d = {1:"1",2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9",0:"0"}
return d[n]
def int2stringbase(num,base):
stringlookup="0123456789ABCDEF"
#Base case:
if num<base:##check if it is single digit number
return stringlookup[num]
else:
return int2stringbase(int(num/base),base)+stringlookup[num%base] # int(num/base) can be written num//base will return decimals
print(int2string(1234560))
print(int2stringbase(1234560,16))
print(int2stringbase(16,2))
print(17//2)
Python 3.5.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
>>> 1234560
12D680
10000
8
=> None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.