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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:15:53+00:00 2026-05-23T03:15:53+00:00

I have found some algorithms online to generate derangements in Python but they’re all

  • 0

I have found some algorithms online to generate derangements in Python but they’re all exponential in complexity and as a result I can’t get them to converge with a set of 26 elements (the alphabet)!

So I’m trying to find a way to improve the following code (source here):

def derangement(vs):
    l = [None for x in vs]
    sol = set()
    sol.add(tuple(l))
    for v in vs:
        sol1 = set()
        for s in sol:
            for (i, v1) in enumerate(s):
                if not v1 and v != vs[i]:
                    s1 = list(s)
                    s1[i] = v
                    sol1.add(tuple(s1))
        sol = sol1
    return list(sol)

If anyone is curious this is for a bruteforce substitution cipher solver. I’m trying to see how long it takes to bruteforce a cipher!

  • 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-23T03:15:53+00:00Added an answer on May 23, 2026 at 3:15 am

    As permutation algorithms are Ω(n!) nothing will make your code converge. This may be faster, but that means nothing for things of that complexity:

    import itertools
    def derangement(x):
        p = itertools.permutations(x)
        return (i for i in p if not any(i[k] == x[k] for k in range(len(x))))
    

    It’s a lazy iterator. If you need all values (I doubt you need) just list() it

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

Sidebar

Related Questions

I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I have found some info on the subject ( like this link) , but
After googling a bit I have found some tips about how to get online
I'm trying add vertical lines to my grid. I have found some examples but
I have found some libraries or web services in PHP that does the job.
I'm currently working on project with Haskell, and have found myself some trouble. I'm
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
I've done some research into server push with javascript and have found the general
Recently, I found myself having to write up some concerns I have about race
I have found some similar questions like this one, however there are so many

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.