Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Computer Lab 04, Iterables

more complex data types: Strings, tuples, lists. Traversing. Basic methods.

puzzle

  • a weekly puzzle to train the brain

practical work

implement the following functions and test them appropriately.

strings

def count_chars(string, char):
    """Return the number of occurances of char in string.
 
    :param string: a string. Example "hello world"
    :param char: character. Example "l"
    :return: int number of char occurences in string
    >>> count_chars("hello","l")
    2
    """

def replace_chars(string, char_original, char_replacement):
    """Return a new string with all occurences of char_original replaced by char_replacement.
 
    :param string: string in which some chars shall be replaced
    :param char_original: character to be replaced
    :param char_replacement: character to be inserted instead of char_original
    :return: a new string with replaced characters
    >>> replace_chars("hello","h","H")
    "Hello"
    """

def reverse_string(string):
    """Return a reversed version of string.
 
    :param string: string to be reversed
    :return: reversed string
    >>> reverse_string("hello")
    "olleh"
    """

def is_palindrom(string):
    """Return True if and only if the string is equal to its reversed version.
 
    :param string: string to check for palindrom
    :return: boolean
    >>> is_palindrom("aka")
    True
    >>> is_palindrom("hello")
    False
    """

lists

def remove_duplicates(items):
    """Remove duplicate items.
 
    :param items: input list, can contain anything
    :return: (unique_list, duplicates)
        unique_list - a list with unique items only
        duplicates - a list with items that were in items more than once
    >>> remove_duplicates(['h','e','l','l','o'])
    (['h','e','l','o'],['l'])
    """

homework

  • Solve homework quadratic_equation and submit it via Upload system in time! Check the deadline in Upload system.
  • The homework from last week was to read chapters 6,7,8,9 from [Wentworth2012]. Go through them once more and concentrate on things you do not understand.
  • Read chapter 11 (Lists) from [Wentworth2012].
courses/be5b33prg/labs/week_04.txt · Last modified: 2016/10/27 13:00 by xposik