repl.it
@charles2588/

recursiveint2stringconvert

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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)