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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:35:25+00:00 2026-05-22T12:35:25+00:00

I solved the problem using this (horribly inefficient method): def createList(word, wordList): #Made a

  • 0

I solved the problem using this (horribly inefficient method):

def createList(word, wordList):
    #Made a set, because for some reason permutations was returning duplicates.  
    #Returns all permutations if they're in the wordList

    return set([''.join(item) for item in itertools.permutations(word) if ''.join(item) in wordList])

def main():

    a = open('C:\\Python32\\megalist.txt', 'r+')
    wordList = [line.strip() for line in a]
    maximum = 0
    length = 0
    maxwords = ""

    for words in wordList:
        permList = createList(words, wordList)
        length = len(permList)
        if length > maximum:
            maximum = length
            maxwords = permList
    print (maximum, maxwords)

It took something like 10 minutes to find the five-letter word that has the most dictionary-valid anagrams. I want to run this on words without a letter constraint, but it would take a ludicrous amount of time. Is there anyway to optimize this?

  • 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-22T12:35:26+00:00Added an answer on May 22, 2026 at 12:35 pm

    This following seems to work OK on a smallish dictionary. By sorting the letters in the word, it becomes easy to test if two words are an anagram. From that starting point, it’s just a matter of accumulating the words in some way. It wouldn’t be hard to modify this to report all matches, rather than just the first one

    If you do need to add constraints on the number of letters, the use of iterators is a convenient way to filter out some words.

    def wordIterator(dictionaryFilename):
        with open(dictionaryFilename,'r') as f:
            for line in f:
                word = line.strip()
                yield word
    
    def largestAnagram(words):
        import collections
        d = collections.defaultdict(list)
        for word in words:
            sortedWord = str(sorted(word))
            d[ hash(sortedWord) ].append(word)
        maxKey = max( d.keys(), key = lambda k : len(d[k]) )
        return d[maxKey]
    
    iter = wordIterator( 'C:\\Python32\\megalist.txt' )
    #iter = ( word for word in iter if len(word) == 5 )
    print largestAnagram(iter)
    

    Edit:

    In response to the comment, the hash(sortedWord), is a space saving optimization, possibly premature in this case, to reduce sortedWord back to an integer, because we don’t really care about the key, so long as we can always uniquely recover all the relevant anagrams. It would have been equally valid to just use sortedWord as the key.

    The key keyword argument to max lets you find the maximum element in a collection based on a predicate. So the statement maxKey = max( d.keys(), key = lambda k : len(d[k]) ) is a succinct python expression for answering the query, given the keys in the dictionary, which key has the associated value with maximum length?. That call to max in that context could have been written (much more verbosely) as valueWithMaximumLength(d) where that function was defined as:

    def valueWithMaximumLength( dictionary ):
        maxKey = None
        for k, v in dictionary.items():
            if not maxKey or len(dictionary[maxKey]) < len(v):
                maxKey = k
        return maxKey
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

as I asked time ago in this question , I solved my problem using
The responses I've got to this question have solved the problem I had in
This problem has been solved thanks to your suggestions. See the bottom for details.
I'm creating some basic work assistance utilities using Ruby. I've hit a problem that
I though I had solved this problem, but it is back: Code generation for
I 've already solved the problem of getting the object id being edited using
I have a simple, real life problem I want to solve using an OO
Problem solved: Thanks guys, see my answer below. I have a website running in
After having solved the problem of getting the editor to launch when there's a
it seems that I just solved my problem why I couldn't use the ASP.NET

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.