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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:29:46+00:00 2026-06-05T06:29:46+00:00

Could anyone explain to me this permutation algo to me? I know it does

  • 0

Could anyone explain to me this permutation algo to me? I know it does pemutations, but i can not figure out why it works.

s = [1,2,3,4,5,6,7,8,9]  
def perm(s, i):
    if i == len(s):
        print s
    for j in range(i, len(s)):
        s[i], s[j] = s[j], s[i]
        perm(s, i + 1)
        s[i], s[j] = s[j], s[i]  //why it swaps back?      
  • 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-05T06:29:47+00:00Added an answer on June 5, 2026 at 6:29 am

    This function mutates the array s into each possible permutation then prints the permutation and reverses the mutation on the way back out.

    At each position i, it leaves all the entries with index less than i alone, tries all later values in position i, and uses recursion to find all permutations with that set of fixed values and each choice for what’s at position i. At the highest index, there are no remaining choices, but you’ve located one of the permutations, so it prints that as a result.

    I find it helpful in understanding this sort of recursion to put extra “debug prints” in the code at interesting points like the start and end of the function, and in this case at the swaps.

    It’s certainly not the most elegant Python (some of the debug prints should be extracted to functions), but running this version of your code with some of these “debug prints” added might be instructive.

    def perm(s, i):
        print '{padding}starting perm({list}, {index})'.format(list=s, index=i, padding=' '*i)
        if i == len(s):
            print s
        for j in range(i, len(s)):
            print '{padding}swapping s[{index1}]={entry1} and s[{index2}]={entry2}'.format(index1=i, entry1=s[i], index2=j, entry2=s[j], padding=' '*i)
            s[i], s[j] = s[j], s[i]
            perm(s, i + 1)
            print '{padding}swapping back s[{index1}]={entry1} and s[{index2}]={entry2}'.format(index1=i, entry1=s[i], index2=j, entry2=s[j], padding=' '*i)
            s[i], s[j] = s[j], s[i]
        print '{padding}returning from perm({list}, {index})'.format(list=s, index=i, padding=' '*i)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain how compilation works? I can't seem to figure out how compilation
Could anyone explain exactly what this method does in AndEngine? runOnUpdateThread(new Runnable() { @Override
Could anyone explain how Java executes this code? I mean the order of executing
Could anyone explain me what information should I put from this URL: http://webservices.amazon.com/onca/xml? Service=AWSECommerceService&
I was wondering if anyone could explain what this rule is saying: <IfModule mod_rewrite.c>
Could anyone please explain what does SomeClassname.class return in JAVA ?? I cant understand
I was wondering if anyone could explain this to me: <%@ Page Title=Log In
Could anyone explain why this won't compile? This loop keeps moving the ref. 'person'
Could anyone explain to me why this is returning TRUE ? There are 7
Could anyone explain this to me? I've seen quite a few DateFormatter issues around,

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.