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

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Processing Lists Using a For Loop


    Hey guys,

    I'm having some trouble with the following question:

    Write a function definition of has_gt that takes a list of numbers and a number and returns True if the first argument contains a number greater than the second argument. False is returned otherwise.

    I understand that I need to have a list that python cycles through, checking each number to see whether it is greater than the second argument using a for loop. However, I am having some trouble with the actual code.

    Help would be greatly appreciated.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481

    Emphasize test first rather than for loop.


    Code:
    '''
        Use command line
        $ python -m doctest thisfile.py
        to run these doctests.
    
        >>> has_gt([1,5], -1)
        True
        >>> has_gt([1,5], 1)
        True
        >>> has_gt([1,5], 5)
        False
        >>> has_gt([1,5], 99999)
        False
        >>> has_gt([], -9e20)
        False
    
        
        >>> has_gt('has_gt works with any orderable types:', 'm')
        True
        >>> has_gt('THE FIRST ARGUMENT MUST BE ITERABLE', 'm')
        False
    '''
    
    def has_gt(L, a):
        return any(a < b for b in L)
    
        # quiz, does
        return a < max(L)
        # work?  Is it faster?  Why?  Under what condition?
    Last edited by b49P23TIvg; August 15th, 2013 at 09:56 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo