RESOURCES

COMP150: Practice Exercises

COMP150: Practice Exercises

Progression 4: Practice exercises

Take a good look at the doctests to further understand the question requirements.

Question 1

Write a function that takes a string, s, and a single character, ch, and returns a slice of s up to and including the first occurence of ch. It should pass the following doctests:

def find_slice(s, ch):
    '''
    >>> find_slice("abcdefghijk", "f")
    'abcdef'
    >>> find_slice("aaabbbccc", "b")
    'aaab'
    >>> find_slice("aaabbbccc", "d")
    ''
    '''

def find_slice(s, ch):
    '''
    >>> find_slice("abcdefghijk", "f")
    'abcdef'
    >>> find_slice("aaabbbccc", "b")
    'aaab'
    >>> find_slice("aaabbbccc", "d")
    ''
    '''
    return s[0: s.find(ch)+1]

Question 2

Write a function that takes a string and returns the first half of it. Exclude the middle character if the string is uneven in length. It should pass the following doctests:

           
def first_half(s):
    '''
    >>> first_half("abcdef")
    'abc'
    >>> first_half("abcdefg")
    'abc'
    >>> first_half("a")
    ''
    >>> first_half("good dogs")
    'good'
    ''' 

def first_half(s):
    '''
    >>> first_half("abcdef")
    'abc'
    >>> first_half("abcdefg")
    'abc'
    >>> first_half("a")
    ''
    >>> first_half("good dogs")
    'good'
    '''
    return s[:len(s)/2]

Question 3

Write a function that takes a string and returns the second half of it. Exclude the middle character if the string is uneven in length. It should pass the following doctests:

def second_half(s):
    '''
    >>> second_half("abcdef")
    'def'
    >>> second_half("abcdefg")
    'efg'
    >>> second_half("a")
    ''
    >>> second_half("good dogs")
    'dogs'
    '''

def second_half(s):
    '''
    >>> second_half("abcdef")
    'def'
    >>> second_half("abcdefg")
    'efg'
    >>> second_half("a")
    ''
    >>> second_half("good dogs")
    'dogs'
    '''
    return s[(len(s)+1)/2:]

Question 4

Write a function that takes a string as an argument and returns the number of vowels in the sentence. Remember the reserved word in

The function should pass the following doctests.

def count_vowels(sentence):
    """
    Return the number of vowels (a,e,i,o,u) in a sentence.
    >>> count_vowels("Hello World!")
    3
    >>> count_vowels("baa baa do baa baa")
    9
    >>> count_vowels("HELLO")
    2
    >>> count_vowels("zzz")
    0
    """
            

def count_vowels(sentence):
    """
    Return the number of vowels (a,e,i,o,u) in a sentence.
    >>> count_vowels("Hello World!")
    3
    >>> count_vowels("baa baa do baa baa")
    9
    >>> count_vowels("HELLO")
    2
    >>> count_vowels("zzz")
    0
    """
    count = 0
    for ch in sentence:

        if ch.lower() in "aeiou":
            count += 1

    return count

Question 5

Write a function that takes a string as an argument and returns the number of upper case letters in the sentence. Remember the str method isupper()

The function should pass the following doctests.


def count_upper(sentence):
    """
    >>> count_upper("Hello World!")
    2
    >>> count_upper("27th of April 1968")
    1
    >>> count_upper("HELLO")
    5
    >>> count_upper("zzz")
    0
    """

def count_upper(sentence):
    """
    >>> count_upper("Hello World!")
    2
    >>> count_upper("27th of April 1968")
    1
    >>> count_upper("HELLO")
    5
    >>> count_upper("zzz")
    0
    """
    count = 0
    for c in sentence:

        if c.isupper():
            count +=1

    return count

Question 6

Each Unicode character has a corresponding integer. This int can be found using the ord function e.g.

ord('a') returns 97
ord('b') returns 98
ord('z') returns 122
ord('A') returns 65
ord('Z') returns 90

You might like to google the ASCII subset of Unicode to get a better picture.

Write a function that takes a string and loops through it. It should return a string containing the ord numbers of each character in the original, separated by spaces.

The function should pass the following doctests.

def get_ords(s):
    '''
    >>> get_ords('abc')
    '97 98 99 '
    >>> get_ords('a b c')
    '97 32 98 32 99 '
    >>> get_ords('a1 b2 c3')
    '97 49 32 98 50 32 99 51 '
    >>> get_ords('[(!)]')
    '91 40 33 41 93 '

def get_ords(s):
    '''
    >>> get_ords('abc')
    '97 98 99 '
    >>> get_ords('a b c')
    '97 32 98 32 99 '
    >>> get_ords('a1 b2 c3')
    '97 49 32 98 50 32 99 51 '
    >>> get_ords('[(!)]')
    '91 40 33 41 93 '
    '''
    result = ''

    for c in s:

        result += str(ord(c)) + ' '

    return result