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 7859619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:57:31+00:00 2026-06-02T21:57:31+00:00

def f2(L): sum = 0 i = 1 while i < len(L): sum =

  • 0
def f2(L):
    sum = 0
    i = 1
    while i < len(L):
        sum = sum + L[i]
        i = i * 2
    return sum

Let n be the size of the list L passed to this function. Which of the following most accurately describes how the runtime of this function grow as n grows?

(a) It grows linearly, like n does.
(b) It grows quadratically, like n^2 does.

(c) It grows less than linearly.
(d) It grows more than quadratically.

I don’t understand how you figure out the relationship between the runtime of the function and the growth of n. Can someone please explain this to me?

  • 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-02T21:57:33+00:00Added an answer on June 2, 2026 at 9:57 pm

    ok, since this is homework:

    this is the code:

    def f2(L):
        sum = 0
        i = 1
        while i < len(L):
            sum = sum + L[i]
            i = i * 2
        return sum
    

    it is obviously dependant on len(L).

    So lets see for each line, what it costs:

    sum = 0
    i = 1
    # [...]
    return sum
    

    those are obviously constant time, independant of L.
    In the loop we have:

        sum = sum + L[i] # time to lookup L[i] (`timelookup(L)`) plus time to add to the sum (obviously constant time)
        i = i * 2 # obviously constant time
    

    and how many times is the loop executed?
    it’s obvously dependant on the size of L.
    Lets call that loops(L)

    so we got an overall complexity of

    loops(L) * (timelookup(L) + const)

    Being the nice guy I am, I’ll tell you that list lookup is constant in python, so it boils down to

    O(loops(L)) (constant factors ignored, as big-O convention implies)

    And how often do you loop, based on the len() of L?

    (a) as often as there are items in the list (b) quadratically as often as there are items in the list?

    (c) less often as there are items in the list (d) more often than (b) ?

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

Sidebar

Related Questions

My code: import heapq def makeHuffTree(symbolTupleList): trees = list(symbolTupleList) heapq.heapify(trees) while len(trees) > 1:
Consider this way of solving the Subset sum problem: def subset_summing_to_zero (activities): subsets =
Why does this sum function not work? It's intent is to sum the variable
Assuming I have a decorator and a wrapped function like this: def squared(method): def
this is a simple question... Does this method can be refactored? def sum total
Would love to refactor this into just one line: def sum_something sum = 0
I try to override this method def sum[B >: A](implicit num: Numeric[B]): B =
#this works in python 3 def pi_sum(n): total, k = 0,1 while k <=
I want to write the function below in a more concise manner: def sum_list(l):
def partitions(n): # base case of recursion: zero is the sum of the empty

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.