August 15th, 2013, 01:21 AM
Processing Lists Using a For Loop
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.
August 15th, 2013, 09:50 AM
Emphasize test first rather than for loop.
Use command line
$ python -m doctest thisfile.py
to run these doctests.
>>> has_gt([1,5], -1)
>>> has_gt([1,5], 1)
>>> has_gt([1,5], 5)
>>> has_gt([1,5], 99999)
>>> has_gt(, -9e20)
>>> has_gt('has_gt works with any orderable types:', 'm')
>>> has_gt('THE FIRST ARGUMENT MUST BE ITERABLE', 'm')
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] are essential for python code and Makefiles!