#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Raising and Catching Exceptions


    Heeeeellllloooo everyone. I'm new to both these forums, AND Python, so please, bear with me.
    I have a question that I've become stuck on recently, and no matter how much research I've done, I just can't get it through my head on how to solve it. Mental Blank / Lack of knowledge. Any help would be greatly appreciated for this. I'm also not entirely sure If I've managed to set it out right, so sorry in advance.

    ## # REQUIREMENTS SPECIFICATION
    ## #
    ## # Step 1:
    ## # Define a function which takes a list of strings, # representing
    ## # the results of flipping a coin, 'H' for heads and 'T' for
    # tails,
    ## # and returns whichever of the two strings occurs most # frequently
    ## # in the list, or 'X' if they occur equally often.
    ## #
    ## # Step 2:
    ## # To allow for corrupted data, modify your function so that
    ## # it raises an appropriate exception if a value is # encountered
    ## # which is not a string or does not have the value 'H' or 'T'.
    ## #
    ## # NB: Your function must raise an exception with a # message
    ## # that tells the caller what the FIRST invalid value was. # See
    ## # the tests below for details of the required exception
    ## # messages.
    ## #
    ## # Hint: Python's "repr" function returns a printable string
    ## # representation of a value. It is useful when displaying
    ## # an unexpected value of an unknown type.
    ##
    #---------------------------------------------------------

    # TESTCASES-----------------------------------------------
    """
    ---------- "Normal" cases, with expected inputs ----------

    Normal case - equal number of heads and tails
    >>> commonest_raise(['H', 'T']) # Test 1
    'X'

    Normal case - more heads than tails
    >>> commonest_raise(['H', 'H', 'T', 'H', 'H', 'H']) # Test 2
    'H'

    Normal case - more tails than heads
    >>> commonest_raise(['T', 'H', 'T', 'H', 'T', 'H', 'T']) # Test 3
    'T'

    Normal case - no tails
    >>> commonest_raise(['H', 'H', 'H']) # Test 4
    'H'

    Normal case - no heads
    >>> commonest_raise(['T']) # Test 5
    'T'

    Boundary case - no coin tosses:
    >>> commonest_raise([]) # Test 6
    'X'

    -------- "Invalid" cases, with unexpected inputs ---------

    Invalid case - invalid string at end of list
    >>> commonest_raise(['H', 'H', 'Y']) # Test 7
    Traceback (most recent call last):
    ValueError: Invalid value found in list: 'Y'

    Invalid case - multiple invalid strings in list
    >>> commonest_raise(['H', 'H', 'I', 'T', 'J']) # Test 8
    Traceback (most recent call last):
    ValueError: Invalid value found in list: 'I'

    Invalid case - non-string at start of list
    >>> commonest_raise([7, 'H']) # Test 9
    Traceback (most recent call last):
    ValueError: Invalid value found in list: 7

    Invalid case - multiple non-strings in list
    >>> commonest_raise(['T', 'H', ['H'], 6, 4]) # Test 10
    Traceback (most recent call last):
    ValueError: Invalid value found in list: ['H']

    """
    #---------------------------------------------------------



    ## DEFINE YOUR (ROBUST) commonest_raise FUNCTION HERE
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    Please post you code and describe which part(s) you have trouble with.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    "Define a function which takes a list of strings,"

    def function(want_a_list_of_strings):



    "returns whichever of the two strings"

    return the_value_you_computed



    "occurs most frequently"

    There are two competing issues. One is to get you to read the python documentation. Investigate the count method of lists. In the interpreter find out what what methods are available for lists.

    Experiment:
    >>> dir(list)

    Experiment:
    >>> help(list.count)

    The other objective is to get you to learn to write code. Please write two versions of your program. One version uses an appropriate simplifying or accumulating builtin function or method, and another version of the program that uses a more explicit method in which you spell out the details of an algorithm.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo