Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8409925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:02:51+00:00 2026-06-10T00:02:51+00:00

def gt(nums, n): for c in nums: if max(nums) > n: return True elif

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:02:52+00:00Added an answer on June 10, 2026 at 12:02 am

    You need to check for not nums first. And you don’t need a for loop.

    Note that this (like your code) doesn’t explicitly check for max(nums) == n, returning False in this situation (which I think should be the correct behaviour for a function called gt()):

    def gt(nums, n):
        if not nums:
            return False
        return max(nums) > n
    

    EDIT: Some timings (Python 2.7.3):

    >>> import timeit
    >>> all = """l = list(range(100))
    ... rl = list(reversed(range(100)))
    ... """
    >>> tim = all + """def gt(nums, n):
    ...     if not nums:
    ...         return False
    ...     return max(nums) > n"""
    >>> gnibbler = all + """def gt(nums, n):
    ...     return any(x>n for x in nums)"""
    >>> burhan = all + """def gt(nums, n):
    ...   return max(nums) > n if nums else False"""
    >>> # Test with the condition being False:
    ... timeit.timeit(setup=tim, stmt="gt(l, 100)")
    3.011574096311698
    >>> timeit.timeit(setup=gnibbler, stmt="gt(l, 100)")
    8.00847921677337
    >>> timeit.timeit(setup=burhan, stmt="gt(l, 100)")
    2.9805757305956178
    >>> timeit.timeit(setup=tim, stmt="gt(rl, 100)")
    2.8600606448831307
    >>> timeit.timeit(setup=gnibbler, stmt="gt(l, 100)")
    7.997938412127745
    >>> timeit.timeit(setup=burhan, stmt="gt(l, 100)")
    3.032805185133668
    >>> # Now what if the condition is True?
    ... timeit.timeit(setup=tim, stmt="gt(l, 98)")
    2.98623750798793
    >>> timeit.timeit(setup=gnibbler, stmt="gt(l, 98)")
    8.265056412191534
    >>> timeit.timeit(setup=burhan, stmt="gt(l, 98)")
    2.9731271156252888
    >>> timeit.timeit(setup=tim, stmt="gt(rl, 98)")
    2.8777295865334764
    >>> timeit.timeit(setup=gnibbler, stmt="gt(rl, 98)")
    1.0481696827076092
    >>> timeit.timeit(setup=burhan, stmt="gt(rl, 98)")
    2.8776150752220246
    

    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 True and the search value is reached very early in the list. Otherwise, all the Python-level comparisons will slow it down a lot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def partial(template, *args) options = args.extract_options! options.merge!(:layout => false) if collection = options.delete(:collection) then
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
def any? if block_given? method_missing(:any?) { |*block_args| yield(*block_args) } else !empty? end end In
Google Python Class | List Exercise - Given a list of numbers, return a
def register(request): flag = True possible = '0123456789abcdefghijklmnopqrstuvwxyz' token = '' current_datetime = datetime.datetime.now()
When I run the following code, I get an error: def genSet(nums): for i
def test4(request): logging.debug(request) logging.debug(request.META['HTTP_REFERER']) return render_to_response('test2/test4.html', context_instance=RequestContext(request)) In the above code can request be
def my_method(parameter) if <what should be here?> puts parameter is a string elsif <and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.