These exercises are taken from

1) Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Python. (It is true that Python has the max() function built in, but writing it yourself is nevertheless a good exercise.)

In [1]:
def max(num1, num2):
    if num1 > num2:
        return num1
        return num2

In [4]:
# usage
print max(7,5) # this prints 7
assert max(10,1) == 10
assert max(5,7) == 7


2) Define a function max_of_three() that takes three numbers as arguments and returns the largest of them.

In [7]:
def max_of_three(num1, num2, num3):
    max1 = max(num1, num2)
    result = max(max1, num3)
    return result

In [8]:
def max_of_three(num1, num2, num3):
    return max(num1, max(num2, num3))

In [11]:
# test of max_of_three
print max_of_three(1,2,3) # this will print 3
print max_of_three(3,2,1) # this will print 3
print max_of_three(2,3,1) # this will print 3
assert max_of_three(5,6,7) == 7
assert max_of_three(6,7,8) == 8
assert max_of_three(9,10,11) == 11


3) Define a function that computes the length of a given list or string. (It is true that Python has the len() function built in, but writing it yourself is nevertheless a good exercise.)

In [12]:
def length(sequence):
    count = 0
    for element in sequence:
        count += 1
    return count

In [13]:
# test of length
print length("word") # prints 4
print length([7,7,7,9,10,11]) # prints 6
assert length("abc") == 3
assert length([]) == 0


4) Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.

In [15]:
def is_vowel(letter):
    if letter == 'a':
        return True
    elif letter == 'e':
        return True
    elif letter == 'i':
        return True
    elif letter == 'o':
        return True
    elif letter == 'u':
        return True
        return False

In [16]:
def is_vowel(letter):
    return letter in 'aeiou'

In [18]:
# test is_vowel
print is_vowel('a') # this prints True
print is_vowel('z') # this prints False
assert is_vowel('e') == True
assert is_vowel('p') == False


5) Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".

In [19]:
def translate(text):
    translation = ''
    for letter in text:
        if letter.isalpha() and not is_vowel(letter):
            translation += letter + 'o' + letter
            translation += letter
    return translation

In [21]:
# test translate
print translate('frog') # prints 'fofrorogog'
assert translate("this is fun") == "tothohisos isos fofunon"


6) Define a function sumlist() and a function multiplylist() that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sumlist([1, 2, 3, 4]) should return 10, and multiplylist([1, 2, 3, 4]) should return 24.

In [22]:
def sumlist(thelist):
    total = 0
    for number in thelist:
        total += number
    return total

In [23]:
def multiplylist(thelist):
    total = 1
    for number in thelist:
        total *= number
    return total

In [26]:
# test sumlist and multiplylist
print sumlist([1,2,3,4]) # prints 10
print multiplylist([1,2,3,4]) # prints 24
assert sumlist([1,1,2,3]) == 7
assert multiplylist([10,9,8,7,6,5,4,3,2,1,0]) == 0


7) Define a function reverse() that computes the reversal of a string. For example, reverse("I am testing") should return the string "gnitset ma I".

In [27]:
def reverse(thestring):
    newstring = ''
    for letter in thestring[::-1]:
        newstring += letter
    return newstring

In [30]:
# test reverse
print reverse("hello") # prints elloh
assert reverse("I am testing") == "gnitset ma I"


8) Define a function is_palindrome() that recognizes palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar") should return True.

In [31]:
def is_palindrome(astring):
    if astring == reverse(astring):
        return True
        return False

In [32]:
def is_palindrome(astring):
    return astring == reverse(astring)

In [35]:
# test is_palindrome
print is_palindrome("kayak") # prints True
print is_palindrome("octopus") # prints False
assert is_palindrome("radar") == True
assert is_palindrome("eggplant") == False


9) Write a function is_member() that takes a value (i.e. a number, string, etc) x and a list of values a, and returns True if x is a member of a, False otherwise. (Note that this is exactly what the in operator does, but for the sake of the exercise you should pretend Python did not have this operator.)

In [50]:
def is_member(element, alist):
    for other_element in alist:
        if other_element == element:
            return True
    return False

10) Define a function overlapping() that takes two lists and returns True if they have at least one member in common, False otherwise. You may use your is_member() function, or the in operator, but for the sake of the exercise, you should (also) write it using two nested for-loops.

In [51]:
def overlapping(list1, list2):
    for element in list1:
        if is_member(element, list2):
            return True
    return False

In [55]:
def overlapping(list1, list2):
    for element in list1:
        for other_element in list2:
            if element == other_element:
                return True
    return False

In [56]:
print overlapping([1,2,3],[3,4,5])
print overlapping([1,2,3],[4,5,6])


11) Write a function is_subseq(alist, anotherlist) that returns True if alist is a subset of anotherlist, False if it isn't.

In [57]:
def is_subseq(alist, anotherlist):
    al_length = len(alist)
    for pos in range(0, len(anotherlist) - al_length + 1):
        sub_list = anotherlist[pos:pos+al_length]
        if alist == sub_list:
            return True
    return False

In [58]:
# test is_subseq
print is_subseq([1,2], [5,4,1,2]) # prints True
print is_subseq([1,1], [5,4,1,2]) # prints False
assert is_subseq([1,2], [1,2,3,4]) == True
assert is_subseq([0,1], [1,2,3,4]) == False


In [ ]: