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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:17:53+00:00 2026-06-12T09:17:53+00:00

Can someone explain to me how this code works which generates permutations? I see

  • 0

Can someone explain to me how this code works which generates permutations?

I see that it’s using recursive list building but I can’t quite get my brain around the exact mechanics.

public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> list)
{
    if (list.Count() == 1)
        return new List<IEnumerable<T>> { list };
    return list
        .Select((a, i1) => 
                    Permute(list.Where((b, i2) => i2 != i1))
                   .Select(b => (new List<T> { a }).Union(b)))
        .SelectMany(c => c);
}
  • 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-12T09:17:54+00:00Added an answer on June 12, 2026 at 9:17 am

    Firstly, I’d point out that this relies on the ordering of Union, which is unspecified, and only works if the values in the original collection are unique. Changing Union to Concat would fix that.

    I suspect it’s also frighteningly expensive. Anyway…

    You can understand it by induction. It definitely works for a single element collection, based on the if. So, let’s go from there.

    Suppose we start with the assumption that it works for a collection of size n like this:

    { e1, ... en }
    

    Now let’s see whether it works for a collection of size n + 1. The call will bypass the “if”, and go into the recursive return part. So we’re left with:

    list.Select((a, i1) => 
                    Permute(list.Where((b, i2) => i2 != i1))
                   .Select(b => (new List<T> { a }).Union(b)))
        .SelectMany(c => c);
    

    The SelectMany part just flattens a collection of collections – that’s straightforward. So let’s focus on the Select call. It’s saying…

    • For each element (a) in the original collection, which we know has index i1…
      • Work out the list except that element (that’s the list.Where(...) part)
      • For each permutation of that smaller list (that’s the Permute call)…
      • … generate a new list consisting of element a followed by the “current permutation” (That’s the new List(...).Union(b) part.)

    So if our collection was { x, y, z } we’d get:

    • Every collection of { y, z } prefixed by x
    • Every collection of { x, z } prefixed by y
    • Every collection of { x, y } prefixed by z

    That pretty much defines “every permutation”… so it works for n + 1.

    Given the base case and the induction step, it therefore works for collections of any size greater than or equal to 1.

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

Sidebar

Related Questions

I am newbie to jQuery, can someone explain what this code does: $(#currency form).submit(function(e)
Can someone explain to me what the bolded portions of this code are doing?
This code is not compilable. I can't find why in standard. Can someone explain?
Can someone write some sample code to explain this concept? I know what a
Can someone kindly explain how the following python code is working? This code is
Can someone explain the syntax in this block of code? Invoke((MethodInvoker) ( () =>
Someone gave me this code that works great. But I would really like to
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain this to me. I am pretty good with perl regular expressions
Can someone explain this to me? In particular the difference between: http://github.com/whymirror/greg and http://piumarta.com/software/peg/

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.