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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:56:14+00:00 2026-05-16T02:56:14+00:00

I have 10 arbitrary letters and need to check the max length match from

  • 0

I have 10 arbitrary letters and need to check the max length match from words file

  1. I started to learn RE just some time ago, and can’t seem to find suitable pattern

    • first idea that came was using set: [10 chars] but it also repeats included chars and I don’t know how to avoid that
  2. I stared to learn Python recently but before RE and maybe RE is not needed and this can be solved without it

    • using “for this in that:” iterator seems inappropriate, but maybe itertools can do it easily (with which I’m not familiar)

I guess solution is known even to novice programmers/scripters, but not to me
Thanks

  • 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-16T02:56:15+00:00Added an answer on May 16, 2026 at 2:56 am

    I’m guessing this is something like finding possible words given a set of Scrabble tiles, so that a character can be repeated only as many times as it is repeated in the original list.

    The trick is to efficiently test each character of each word in your word file against a set containing your source letters. For each character, if found in the test set, remove it from the test set and proceed; otherwise, the word is not a match, and go on to the next word.

    Python has a nice function all for testing a set of conditions based on elements in a sequence. all has the added feature that it will “short-circuit”, that is, as soon as one item fails the condition, then no more tests are done. So if your first letter of your candidate word is ‘z’, and there is no ‘z’ in your source letters, then there is no point in testing any more letters in the candidate word.

    My first shot at writing this was simply:

    matches = []
    for word in wordlist:
        testset = set(letters)
        if all(c in testset for c in word):
            matches.append(word)
    

    Unfortunately, the bug here is that if the source letters contained a single ‘m’, a word with several ‘m’s would erroneously match, since each ‘m’ would separately match the given ‘m’ in the source testset. So I needed to remove each letter as it was matched.

    I took advantage of the fact that set.remove(item) returns None, which Python treats as a Boolean False, and expanded my generator expression used in calling all. For each c in word, if it is found in testset, I want to additionally remove it from testset, something like (pseudo-code, not valid Python):

    all(c in testset and "remove c from testset" for c in word)
    

    Since set.remove returns a None, I can replace the quoted bit above with “not testset.remove(c)”, and now I have a valid Python expression:

    all(c in testset and not testset.remove(c) for c in word)
    

    Now we just need to wrap that in a loop that checks each word in the list (be sure to build a fresh testset before checking each word, since our all test has now become a destructive test):

    for word in wordlist:
        testset = set(letters)
        if all(c in testset and not testset.remove(c) for c in word):
            matches.append(word)
    

    The final step is to sort the matches by descending length. We can pass a key function to sort. The builtin len would be good, but that would sort by ascending length. To change it to a descending sort, we use a lambda to give us not len, but -1 * len:

    matches.sort(key=lambda wd: -len(wd))
    

    Now you can just print out the longest word, at matches[0], or iterate over all matches and print them out.

    (I was surprised that this brute force approach runs so well. I used the 2of12inf.txt word list, containing over 80,000 words, and for a list of 10 characters, I get back the list of matches in about 0.8 seconds on my little 1.99GHz laptop.)

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

Sidebar

Related Questions

I have a string of arbitrary length, and starting at position p0, I need
I have some arbitrary pixel data that I want to save as a PNG.
So I've got some data. There are entities. Entities have an arbitrary number of
I have a list of some arbitrary objects, for the purpose of the example
I need a method that can have an arbitrary number of parameters. In C#
I just want a simple SVG image that has some arbitrary text on an
I have some arbitrary text that i'd like to convert into a table with
In my String, I can have an arbitrary number of words which are comma
I have an arbitrary array of lego bricks. I also have some figures made
I have an arbitrary XML document provided by a URL. I also have an

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.