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 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 found this code online and im trying to compile it, but gcc keeps
I have been looking all over google to find some answers to my questions
I have the code below that I have found. I am trying to learn
I have quite an specific question about image recognition. I have to write a
I have to code an object detector (in this case, a ball) using OpenCV.
I am making a mobile app to find anagrams and partial matches. Mobile is
I am writing an iPad app that presents user documents similar to the way
I noticed that every browser renders the border-style: inset; property differently, so I had
I decided that I wanted a search field on my website, the thing is

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.