Created
June 29, 2016 05:45
-
-
Save charles2588/82d007057003ca893c2681a9df22c82e to your computer and use it in GitHub Desktop.
https://repl.it/C7Dk/1 created by charles2588
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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