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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:28:00+00:00 2026-06-16T03:28:00+00:00

I have a method that outputs all the permutations of an array using a

  • 0

I have a method that outputs all the permutations of an array using a recursive function:

    /// <summary>
    /// Yields a sequence of all permutations in lexical order
    /// </summary>
    /// <typeparam name="T">Type of item in the input sequence</typeparam>
    /// <param name="input">The initial sequence</param>
    /// <returns>A sequence of all permutations in lexical order</returns>
    public IEnumerable<IEnumerable<T>> Permute<T>(IEnumerable<T> input) 
    {
        var list = input.ToList();
        list.Sort(); // into lexical order

        if (list.Count > 2)
        {
            foreach (var item in list)
            {
                var itemArray = new[] {item};
                T[] otherItems = list.Except(itemArray).ToArray();
                foreach (var permutation in Permute(otherItems))
                    yield return itemArray.Concat(permutation).ToArray();
            }
        }
        else 
        {
            yield return new[] {list[0], list[1]};
            yield return new[] {list[1], list[0]};
        }
    }

However, when I run this function in an NUnit test, it terminates far earlier than I think it should:

    [Test]
    public void Can_print_all_permutations()
    {
        foreach (var p in Permute("123456789"))
        {
            Console.WriteLine(new string(p.ToArray()));
        }
    }

Here’s the last lines printed by the test (I separated them by commas for posting purposes):

349527816,
349527861,
349528167,
349528176,
349528617,
3

The abrupt termination makes me think that buffering and flushing by the console is a component of the issue, but the last line that should be printed is 987654321, so I feel like the loop is terminating early.

If I include a calculation in the loop, it terminates even earlier (in the 24… range).

Is there something in my implementation that explains this behavior? Am I pushing the limits of the stack?

  • 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-16T03:28:01+00:00Added an answer on June 16, 2026 at 3:28 am

    Most likely test infrastructure you are using have some sort of “maximum time for test to run” restriction. Enumerating all permutations does take non-trivial amount of time.

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

Sidebar

Related Questions

I have a method that I'm using to output all the class names in
I am using c#. I have a method that outputs multiple values. I am
I have an .aspx file that outputs an image using the following methods: Server.MapPath(somefile.png)
I have a method that outputs a varying number of arrays like so: [[unidentified
I have a class that has an output() method which returns a matplotlib Figure
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have method that returns module path of given class name def findModulePath(path, className):
I have a method that receives something and it needs to determine the type
I have a method that uses ARC and takes an NSError pointer and I

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.