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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:20:06+00:00 2026-06-09T13:20:06+00:00

So this is one of the first programs I’ve written in Python. I’m trying

  • 0

So this is one of the first programs I’ve written in Python. I’m trying to take a string, and output all strings that are real words. I have it completed (I need to find a reference file that contains more words) however it is not scalable as I cannot input more than 8 characters without Python taking a real long time to return something.

def lower_and_remove_spaces(fill_string):
    '''
    function takes a string of 2 or more characters and prints out all the permutations
    of words that the characters can make. 
    '''
    lower_string = ''

    for i in fill_string:
        if i.isalpha():
            lower_string += i.lower()

    return lower_string    

def fill_list(input_string):
   iter_list = []
   string_list = []
   this_string = lower_and_remove_spaces(input_string)
   for num in range(2,len(this_string)+1):
      iter_list.append(itertools.permutations(this_string,num))

   for iters in iter_list:
      for lists in iters:
         string_list.append(list(lists))

    return string_list

def word_list(string):
   string_list = fill_list(string)
   a_word_list = []
   a_string = ''
   for i in string_list:
      if not a_string == '':
         a_word_list.append(a_string)
      a_string = ''
      for y in i:
         a_string += y
    return a_word_list

I understand this jumps around a lot but I’m wondering what’s a better way to do this so that it’s scalable?

  • 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-09T13:20:07+00:00Added an answer on June 9, 2026 at 1:20 pm

    Some quick ideas: making all permutations is going to O(n!), there’s no way around this. Even if you optimize your code, you’ll still run into a wall when n approaches larger numbers. If you have a dictionary of valid words, this problem is a bit different. Under a pathological input set ( your dictionary contains all permutations ) you can’t do any better than this.

    However, you can do the following

    1. Keep the dictionary of valid words in a prefix-tree
    2. Manually generate permutations recursively instead of via itertools.i.e., Choose a letter, start a word, recurse
    3. At each step, check if the prefix is valid, prune the search tree otherwise.

    The performance of this will be much better in practice than O(n!)

    If you’re unfamiliar with prefix trees, here’s a way to simulate the same thing with a Python hash

       def prefix_hash(list_o_words):
           ret = {}
           for word in list_o_words:
               for i in range(2,len(word)-1):
                   ret[word[:i]] = 'prefix'  # this should check if it's a word first..
           ret[word] = 'word'
    

    Ask questions if you need more help.

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

Sidebar

Related Questions

All, I've just started using Python (v 2.7.1) and one of my first programs
I'm trying to create a several programs, where the first one initiates all the
I can't figure this one out. At first let me say that my cache
First of all I did gave a look at this one. But I didn't
I have two programs- First one being a C program that writes in to
I have 2 programs (written by me). The first one called MAN will invoke
I'm using Python 3. I've written two programs. One loops through a csv file
I'm writing one of my first C# programs. Here's what I'm trying to do:
This one is a huge issue: first off, while I know a little bit
I have two arrays the first one has this structure Parameter Keys array array

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.