# cs111  exam review questions
#

## TASK 1
# 1A.  Write a function called dropEnds(s) that takes a string parameter
# and returns a string without the first and last characters. Below are
# some sample invocations:
#
#  dropEnds('paper') returns 'ape'
#  dropEnds('good') returns 'oo'
#  dropEnds('at') returns ''
#  dropEnds('X') returns ''
#  dropEnds('') returns ''

#  1B.  Write a function called switchEnds(s) that takes a string parameter
#  and returns a string with the last character and the first character switched.
#  Below are some sample invocations:
#
      #switchEnds('apple') returns 'eppla'
      #switchEnds('ox') returns 'xo'
      #switchEnds('fox') returns 'xof'
      #switchEnds('banana') returns 'aananb'
      #switchEnds('X') returns 'X'
      #switchEnds('') returns ''
      
#  1C.  Write the result of the following three python lines of code:
#     switchEnds(dropEnds("otter"))
#     dropEnds(switchEnds("lemon"))
#     switchEnds(switchEnds(dropEnds("cheese")))


#----------------------------------------------------------------------          
## TASK 2
# 2A.  Write a function called bigger that takes two numbers as parameters and
# returns the larger number.  Below are some sample invocations:
#
#  bigger(2,4) returns 4
#  bigger(4,2) returns 4
#  bigger(5,5) returns 5
#
# 2B.  Write a function called biggest(n1,n2,n3) that takes three
# numbers are parameters and returns the largest of the three numbers.
# Below are some sample invocations:

#  biggest(1,3,5) returns 5
#  biggest(20,15,10) returns 20
#  biggest(5,5,3) returns 5
#  biggest(9,11,10) returns 11

# -----------------------------code below here-----------------------------


def dropEnds(s):
    """return the string s with the first and last characters 
    dropped e.g. 'hello' --> 'ell' """
    if len(s) <= 2:
        return '' # empty string
    else:    
        penultimate = len(s)-1
        return s[1:penultimate]

# alternate version of dropEnds()
def dropEnds(s):
    """return the string s with the first and last characters 
    dropped e.g. 'hello' --> 'ell' """
    return s[1:-1]  # returns the substring without the first and last characters
                    # and works for empty strings as well
    
def switchEnds(s):
    """return the string s with the first and last characters swapped
    e.g. 'hello' -> 'oellh'   """
    if len(s) <= 1:
        return s
    else:    
        first = s[0]
        last = s[-1]
        middle = dropEnds(s)
        return last + middle + first
    

def bigger(n1,n2):
    """Returns larger of the two numbers.  If the two numbers have same
    value, return the number"""
    if n1> n2:
        return n1
    else:
        return n2
                
def biggest(n1,n2,n3):
    """Returns largest of the three numbers"""
    if bigger(n1,n2)==n1 and bigger(n1,n3)==n1:
        return n1
    elif bigger(n2,n3) == n2 and bigger(n2,n1) == n2:
        return n2
    else: 
        return n3
        
# Testing
# print bigger(5,10) ==> 10
# print bigger(10,5) ==> 10
# print bigger(5,5) ==> 5

print 'testing biggest'
print biggest(1,2,3)
print biggest(1,2,2)
print biggest(9,10,9)
print '--------------'
print 'testing dropEnds'
print dropEnds('hello')
print dropEnds('superduper')
print dropEnds('OMG')
print dropEnds('XY')
print dropEnds('X')
print '--------------'
print 'testing switchEnds'
print switchEnds('hello')
print switchEnds('OMG')
print switchEnds('absolutely')
print switchEnds('python')
print switchEnds('XY')
print switchEnds('X')
print '--------------'

print dropSwitch('butterfly')
print dropSwitch('sushi')
print dropSwitch('banana')
print dropSwitch('lemon')
print '------'
print switchEnds(dropEnds("otter"))
print dropEnds(switchEnds("lemon"))
print switchEnds(switchEnds(dropEnds("cheese")))


        
        
