def gt(nums, n):
for c in nums:
if max(nums) > n:
return True
elif max(nums) < n:
return False
elif not nums:
return False
for the last elif, it should validate the list of nums whether it is empty or not. But not working for me. Does anyone know what code i can use to check if the list is empty?
Thanks.
You need to check for
not numsfirst. And you don’t need aforloop.Note that this (like your code) doesn’t explicitly check for
max(nums) == n, returningFalsein this situation (which I think should be the correct behaviour for a function calledgt()):EDIT: Some timings (Python 2.7.3):
So Burhan’s and my solution are equivalent in terms of speed (not really surprising since they do exactly the same thing, mine is just a bit more verbose), and gnibbler’s is noticeably faster only if the list is long enough (I’ve removed previous timings where it way always slower when the list only contained ten items), the condition evaluates to
Trueand the search value is reached very early in the list. Otherwise, all the Python-level comparisons will slow it down a lot.