- A recursive algorithm must have a base case.
- A recursive algorithm must change its state and move toward the base case.
- A recursive algorithm must call itself, recursively.
4.5. Converting an Integer to a String in Any Base
def toString(number,base):
convertstring = '0123456789ABCDEF'
if number < base:
return convertstring[number]
else :
return toString(number//base,base) + convertstring[number%base)
reverse a string
def reverse(s):
if len(s) == 1 or 0 :
return s
else:
return reverse(s[1:])+ s[0]
check ispalindrome
def ispalindrome(s):
if len(s) == 0 or 1 :
return True
else:
s = s.strip()
if s[0] == s[-1]:
return ispalindrome(s[1:-1])
else:
return False