In [1]:
#As we have seen before, 
#one function can call another function. 
#What does this function do?
#what is printed?
#Having more than one function
def passing_grade(h):
    if h>50:
        #print 'good'#True  <--Some people are confused by print Vs. return
        return True
    else:
        #print 'bad'#False
        return False

def candy_for_grade(g):
    if passing_grade(g):
        return 'candy'
    else:
        return 'no_candy'
y=candy_for_grade(51)
#print y

#what is the value of y?
In [2]:
#As we have seen before, one function can call another function. 
#What does this function do?
#what is printed?
#Having more than one function
def passing_grade(h):
    if h>50:
        print 'good'#True  <--Some people are confused by print Vs. return
        return True
    else:
        print 'bad'#False
        return False

def candy_for_grade(g):
    if passing_grade(g):
        return 'candy'
    else:
        return 'no_candy'
y=candy_for_grade(51)
print y

#what is the value of y? Now? Rembmer 
#whether or not we print something, this 
#does not change
#the value of y.
good
candy
In [6]:
#In lab, you have looked at the function 
#isPalindrome which 
#returns True if a given string is a palindrome. 
#I.e., isPalindrome('abcde') returns False and isPalindrome('abcba') 
#returns True. 

#I give you a function called reverse. It takes in
#a string, and returns the reversed version of
#the string. So reverse('abcba') returns abcba.
#reverse('12345') returns '54321'. 
#reverse(12345) gives an error. 
#can you think of a way to create the function 
#isPalindrom by using the function reverse?
#First try to write down in English how the function would work.
In [4]:
def Reverse(x):
    y=''
    for n in range(len(x)):
        y += x[len(x)-n-1] 
    print y
    return y
    

#def isPalindrome(x):
#    if Reverse(x)==x: #I am saying if None==x:
#        return True
#    else:
#        return False
#isPalindrome('abcba')

def isPalindrome(x):
    return Reverse(x)==x
isPalindrome('abcba')
abcba
Out[4]:
True
In [ ]:
def isPalindrome(x):
    if x==reverse(x):
        return True
    else:
        return False

isPalindrome('123210')  
In [ ]:
#Another way
def isPalindrome(x):
    x=str(x)
    return x==reverse(x)
#this does the same thing as the function 
#isPalindrome above. 
#why?
#if I give isPalindrome('12345') I want False
#if I give isPalindrome(12345) I want False
#if I give isPalindrome('abcba') I want True
#if I give isPalindrome(12321) I want True
#isPalindrome('12345')
In [ ]:
#Ok so now we want to have a function isPalindrome. 
#However, we want it
#to be able to have a string or an int as an input. 
#the function we wrote above can only take a string.
#what is the output of the code below?

def reverse(x):
    y=''
    for n in range(len(x)): 
      y += x[len(x)-n-1] 
    return y

def isPalindrome(x):
    #x=str(x) #add this to use ints. 
    if x==reverse(x):
        return True
    else:
        return False
    
isPalindrome(123210)
In [ ]:
#One way to do this is to have 2 functions. 
#One checks if a string is 
#a palindrome. Another one checks if an int is 
#a palindrome.
def reverse(x):
    '''
    This function takes a string. And returns
    the reversed version of the string.
    '''
    y=''
    for n in range(len(x)):
      y += x[len(x)-n-1] 
    return y

def isStrPalindrome(x):
    '''
    This function takes a string and returns 
    True if the string is a palindrome. It returns
    False otherwise. 
    '''
    #return x==reverse(x)
    #same as
    if x==reverse(x):
        return True
    else:
        return False

def isIntPalindrome(x):
    '''
    This function takes an int. And returns
    True if the int is a plandrome. It returns
    False otherwise. 
    '''
    #what is  x==reverse(x)
    #return str(x)==reverse(str(x))
    if str(x)==reverse(str(x)):
        return True
    else:
        return False

def isPalindrome(x):
    if type(x)==str:
      #isStrPalindrome takes a string
      #and returns True if the string is 
      #a palindrome. False if not. 
      return isStrPalindrome(x)
      
    elif type(x)==int:
      #isStrPalindrome takes a int
      #and returns True if the int is 
      #a palindrome. False if not.
      return isIntPalindrome(x)
    else:
      print 'Wrong data type'
      return

y=isPalindrome(12.56)
print y