CIT 590 Little functions practice
Spring 2010, David Matuszek

Below is a list of 20 "little functions" -- also available as the text file lf.py.

To study for the upcoming quiz, here's what I recommend that you do:

for i in range(0, 20):
    Figure out what you think the function does
    Run it and see if it does what you think
For each of the functions in turn (work on one at a time), try to figure out what it does when given the correct kind of input. Try the function with several values to see if you are correct.

The quiz will consist of five functions similiar to this. Some of them may even be from this list.

def f1(ints):   # ints is a list of integers
    n = 0
    for i in range(0, len(ints)):
        n += ints[i]
    return n

def f2(ints):   # ints is a list of integers
    n = 0
    for i in range(1, len(ints)):
        if ints[i] > ints[n]:
            n = i
    return n

def f3(ints):   # ints is a list of integers
    n = 0
    for i in range(1, len(ints)):
        if ints[i] > ints[n]:
            n = i
    return ints[n]

def f4(ints):   # ints is a list of integers
    n = ints[0]
    for i in range(1, len(ints)):
        if ints[i] > n:
            return ints[i]
    return n

def f5(ints):   # ints is a list of integers
    for i in range(1, len(ints)):
        if ints[i] < ints[i - 1]:
            return False
    return True

def f6(ints):   # ints is a list of integers
    for i in range(1, len(ints)):
        if ints[i] < ints[i - 1]:
            return False
        else:
            return True

def f7(ints):   # ints is a list of integers
    for i in range(1, len(ints)):
        if ints[i] < ints[i - 1]:
            flag = False
        else:
            flag = True
    return flag

def f8(s):  # s is a string
    count = 0
    for i in range(1, len(s)):
        if s[i] == s[i - 1]:
            count += 1
    return count

def f9(ints):   # ints is a list of integers
    flag = True
    for i in range(0, len(ints)):
        flag = flag and (ints[i] < 100)
    return flag

def f10(ints):   # ints is a list of integers
    flag = True
    for i in range(0, len(ints)):
        flag = flag or (ints[i] < 100)
    return flag

def f11(ints):   # ints is a list of integers
    flag = False
    for i in range(0, len(ints)):
        flag = flag or (ints[i] < 100)
    return flag

def f12(ints):   # ints is a list of integers
    flag = False
    for i in range(1, len(ints)):
        flag = flag or (ints[i] < 100)
    return flag

def f13(s):  # s is a string containing words and spaces
    if s[0].isalpha() and s[0].islower():
        return False
    for i in range(1, len(s)):
        if s[i - 1] == " " and s[i].islower():
            return False
    return True

def f14(s):  # s is a string containing words and spaces
    count = 0
    if s[0].isalpha():
        count = 1
    for i in range(1, len(s)):
        if s[i - 1] == " " and s[i].isalpha():
            count += 1
    return count
    
def f15(s):  # s is a string
    j = 0
    k = len(s) - 1
    while (j < k):
        if (s[j] != s[k]):
            return False;
        j += 1
        k -= 1
    return True

def f16(n):  # n is a positive integer
    k = n
    while k < 1000000:
        k = k * n
    return k

def f17(n):  # n is a positive integer
    k = 1
    while n * k <= 100:
        k += 1
    return n * k

def f18(ints):   # ints is a list of integers
    for i in range(1, len(ints)):
        ints[i] += ints[i - 1]
    return ints

def f19(ints):   # ints is a list of integers
    last = len(ints) - 1
    for i in range(0, len(ints) / 2):
        temp = ints[i]
        ints[i] = ints[last - i]
        ints[last - i] = temp
    return ints

def f20(s):   # s is a string
    global index
    for index in range(0, len(s)):
        return s[index]