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

  • Home
  • SEARCH
  • 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 974297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:24:51+00:00 2026-05-16T03:24:51+00:00

I have a problem, and I’m not too sure how to solve it without

  • 0

I have a problem, and I’m not too sure how to solve it without going down the route of inefficiency. Say I have a list of words:

  • Apple
  • Ape
  • Arc
  • Abraid
  • Bridge
  • Braide
  • Bray
  • Boolean

What I want to do is process this list and get what each word starts with up to a certain depth, e.g.

  • a – Apple, Ape, Arc, Abraid
  • ab – Abraid
  • ar -Arc
  • ap – Apple, Ape
  • b – Bridge, Braide, Bray, Boolean
  • br – Bridge, Braide, Bray
  • bo – Boolean

Any ideas?

  • 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-05-16T03:24:52+00:00Added an answer on May 16, 2026 at 3:24 am

    Perhaps you’re looking for something like:

        #!/usr/bin/env python
        def match_prefix(pfx,seq):
            '''return subset of seq that starts with pfx'''
            results = list()
            for i in seq:
                if i.startswith(pfx):
                    results.append(i)
            return results
    
        def extract_prefixes(lngth,seq):
            '''return all prefixes in seq of the length specified'''
            results = dict()
            lngth += 1
            for i in seq:
                if i[0:lngth] not in results:
                    results[i[0:lngth]] = True
            return sorted(results.keys())
    
        def gen_prefix_indexed_list(depth,seq):
            '''return a dictionary of all words matching each prefix
               up to depth keyed on these prefixes'''
            results = dict()
            for each in range(depth):
                for prefix in extract_prefixes(each, seq):
                    results[prefix] = match_prefix(prefix, seq)
            return results
    
    
        if __name__ == '__main__':
            words='''Apple Ape Arc Abraid Bridge Braide Bray Boolean'''.split()
            test = gen_prefix_indexed_list(2, words)
            for each in sorted(test.keys()):
                print "%s:\t\t" % each,
                print ' '.join(test[each])
    

    That is you want to generate all the prefixes that are present in a list of words between one and some number you’ll specify (2 in this example). Then you want to produce an index of all words matching each of these prefixes.

    I’m sure there are more elegant ways to do this. For for a quick and easily explained approach I’ve just built this from a simple bottom-up functional decomposition of the apparent spec. Of the end result values are lists each matching a given prefix, then we start with the function to filter out such matches from our inputs. If the end result keys are all prefixes between 1 and some N that appear in our input then we have a function to extract those. Then our spec. is an extremely straightforward nested loop around that.

    Of course this nest loop might be a problem. Such things usually equate to an O(n^2) efficiency. As shown this will iterate over the original list C * N * N times (C is the constant number representing the prefixes of length 1, 2, etc; while N is the length of the list).

    If this decomposition provides the desired semantics then we can look at improving the efficiency. The obvious approach would be to lazily generate the dictionary keys as we iterate once over the list … for each word, for each prefix length, generate key … append this word to the the list/value stored at that key … and continue to the next word.

    There’s still a nested loop … but it’s the short loop for each key/prefix length. That alternative design has the advantage of allowing us to iterate over lists of words from any iterable, not just an in memory list. So we could iterate over lines of a file, results generated from a database query, etc — without incurring the memory overhead of keeping the entire original word list in memory.

    Of course we’re still storing the dictionary in memory. However we can also change that, decouple the logic from the input and storage. When we append each input to the various prefix/key values we don’t care if they’re lists in a dictionary, or lines in a set of files, or values being pulled out of (and pushed back into) a DBM or other key/value store (for example some sort of CouchDB or other “noSQL clustered/database.”

    The implementation of that is left as an exercise to the reader.

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

Sidebar

Related Questions

I do not have problem as such but I am quite new to Ruby.
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I have problem when I try insert some data to Informix TEXT column via
I have a problem using the Java search function in Eclipse on a particular
I have a problem with a little .Net web application which uses the Amazon
I have a problem in my project with the .designer which as everyone know

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.