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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:25:00+00:00 2026-06-12T15:25:00+00:00

def filter(f, lst): if lst == []: return [] if f(lst[0]): return [lst[0]] +

  • 0
def filter(f, lst):     
    if lst == []: return []
    if f(lst[0]): return [lst[0]] + filter(f, lst[1:])
    return filter(f, lst[1:]) 

def my_reverse(lst):        # Reverse the list
    def reverse_helper(x,y):
        if x == []: return y
        return reverse_helper(x[1:], [x[0]] + y)
    return reverse_helper(lst, []) 

def revfilter_alpha(f, lst):    # Reverse and filter ...
    return my_reverse(filter(f, lst))

def revfilter_beta(f, lst): # Reverse and filter ...
    if lst == []: return []
    return revfilter_beta(f, lst[1:]) + ([lst[0]]  if f(lst[0])  else [])   

Could someone explain to me how to determine the running time in Big Θ notation for these? I’ve read quite a few things but still have no idea where to begin.

In filter, I think it is Θ(n^2) because it checks each element in list of size n with the predicate function f with n recursive calls so n*n.

revfilter_beta looks pretty similar just reversing while filtering so wouldn’t this also be Θ(n^2)?

revfilter_alpha does filter then a reverse, so wouldn’t this be n^2*n^2 = Θ(n^4)?

Does anyone have any thoughts?

  • 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-12T15:25:02+00:00Added an answer on June 12, 2026 at 3:25 pm

    filter has n recursive calls, but you also do a copy operation on each iteration which takes n, so you end up having Θ(n^2). If you implemented it ‘properly’ it should be Θ(n) though.

    Same for my_reverse.

    Same for revfilter_beta.

    revfilter_alpha just does a filter and then a reverse, so thats Θ(n^2 + n^2) = Θ(n^2).


    EDIT: Let’s look into filter a bit more.

    What you want to figure out is how many operations are performed relative to the size of the input. O(n) means that at the very worst case, you will do on the order of n operations. I say “on the order of” because you could, for example, do O(n/2) operations, or O(4n), but the most important factor is n. That is, as n grows, the constant factor becomes less and less important, so we only look at the non-constant factor (n in this case).

    So, how many operations does filter perform on a list of size n?

    Let’s take it from the bottom up. What if n is 0 – an empty list? Then it will just return an empty list. So let’s say that’s 1 operation.

    What if n is 1? It will check whether lst[0] should be included – that check takes however long it takes to call f – and then it will copy the rest of the list, and do a recursive call on that copy, which in this case is an empty list. so filter(1) takes f + copy(0) + filter(0) operations, where copy(n) is how long it takes to copy a list, and f is how long it takes to check whether an element should be included, assuming it takes the same amount of time for each element.

    What about filter(2)? It will do 1 check, then copy the rest of list and call filter on the remainder: f + copy(1) + filter(1).

    You can see the pattern already. filter(n) takes 1 + copy(n-1) + filter(n-1).

    Now, copy(n) is just n – it takes n operations to slice the list in that way. So we can simplify further: filter(n) = f + n-1 + filter(n-1).

    Now you can try just expanding out filter(n-1) a few times to see what happens:

    filter(n) = f + n-1 + filter(n-1)
              = 1 + n-1 + (f + n-2 + filter(n-2))
              = f + n-1 + f + n-2 + filter(n-2)
              = 2f + 2n-3 + filter(n-2)
              = 2f + 2n-3 + (f + n-3 + filter(n-3))
              = 3f + 3n-6 + filter(n-3)
              = 3f + 3n-6 + (f + n-4 + filter(n-4))
              = 4f + 4n-10 + filter(n-4)
              = 5f + 5n-15 + filter(n-5)
              ...
    

    Can we generalize for x repetitions? That 1, 3, 6, 10, 15… sequence is the triangle numbers – that is, 1, 1+2, 1+2+3, 1+2+3+4, etc. The sum of all numbers from 1 to x is x*(x-1)/2.

              = x*f + x*n - x*(x-1)/2 + filter(n-x)
    

    Now, what is x? How many repetitions will we have? Well, you can see that when x = n, you have no more recursion – filter(n-n)=filter(0)=1. So our formula is now:

    filter(n) = n*f + n*n - n*(n-1)/2 + 1
    

    Which we can simplify further:

    filter(n) = n*f + n^2 - (n^2 - n)/2 + 1
              = n*f + n^2 - n^2/2 + n/2 + 1
              = n^2 - n^2/2 + f*n + n/2 + 1
              = (1/2)n^2 + (f + 1/2)n + 1
    

    So there ya have it – a rather detailed analysis. That would be Θ((1/2)n^2 + (f + 1/2)n + 1)… assuming f is insignificant (say f=1) that gets to Θ((1/2)n^2 + (3/2)n + 1).

    Now you’ll notice, if copy(n) took a constant amount of time instead of a linear amount of time (if copy(n) was 1 instead of n), then you wouldn’t get that n^2 term in there.

    I’ll admit, when I said Θ(n^2) initially, I didn’t do this all in my head. Rather, I figured: ok, you have n recursive steps, and each step will take n amount of time because of the copy. n*n = n^2, thus Θ(n^2). To do that a bit more exactly, n shrinks at each step, so you really have n + (n-1) + (n-2) + (n-3) + ... + 1, which ends up being that same figure as above: n*n - (1 + 2 + 3 + ... + n) = n*n - n*(n-1)/2 = (1/2)n^2 + (1/2)n, which is the same if I had used 0 instead of f, above. Likewise, if you had n steps but each step took 1 instead of n (if you didn’t have to copy the list), then you’d have 1 + 1 + 1 + ... + 1, n times, or simply n.

    But, that requires a bit more intuition so I figured I’d also show you the brute force method that you can apply to anything.

    • 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
I'm trying to create a function: filter(delete,lst) When someone inputs: filter(1,[1,2,1]) returns [2] What
I have a filter: class MyFilters { def filters = { before = {
Given the following view : def comments(request): comments_list = Thing.objects.filter(thing_type=2) #Thing model extends MPTTModel
def filters = { forUser(controller:'user', action:'*') { before = { user=springSecurityService.getCurrentUser() log.info(came to filter
In my django views i have the following def create(request): query=header.objects.filter(id=a)[0] a=query.criteria_set.all() logging.debug(a.details) I
I have a piece of code like this def filter(t: String) : Boolean =
I have a list of functions... e.g. def filter_bunnies(pets): ... def filter_turtles(pets): ... def
I'm defining a Hibernate filter which specifies a default condition as follows: <filter-def name=IsDeletedFilter

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.