Permutations Array?

Jay-R Hayashi 206 Reputation points
2024-03-09T02:28:32.5766667+00:00

Good day all Master! please help me to fix my problem.1

This problem is a permutations process. If I click button OK it well shuffle and this display in the listBox1. Please take look the picture below.2

at this image as you can see it's do permutations number from the textbox above which is (01 02 03) and display in listBox1. The problem is if I insert number in textbox limit and I click button OK. The permutations number same 3 array, my expectation is 2 array only or any numbers display in the limit textbox. Please help me to fix my problem, thank you so much to all master.

----"" THE CODE BELOW IS CODE FOR BUTTON OK""---

private void Button6_Click(object sender, EventArgs e)

    {

        // Get the items.

        string[] items = txtItems.Text.Split(' ');

        // Generate the permutations.


  

        List<List<string>> results =

            GeneratePermutations<string>(items.ToList());

        // Display the results.

        listBox1.Items.Clear();

        foreach (List<string> combination in results)

        {

            listBox1.Items.Add(string.Join(" ",combination.ToArray()));

        }

        // Calculate the number of permutations.

        long num_permutations = Factorial(items.Length);

        txtNumPermutations.Text = num_permutations.ToString();

        // Check the result.

        //Debug.Assert(lstPermutations.Items.Count == num_permutations);

        Debug.Assert(listBox1.Items.Count == num_permutations);

    }

    // Generate permutations.

    private List<List<T>> GeneratePermutations<T>(List<T> items)

    {

        // Make an array to hold the

        // permutation we are building.

        T[] current_permutation = new T[items.Count];

        // Make an array to tell whether

        // an item is in the current selection.

        bool[] in_selection = new bool[items.Count];

        // Make a result list.

        List<List<T>> results = new List<List<T>>();

        // Build the combinations recursively.

        PermuteItems<T>(items, in_selection,

            current_permutation, results, 0);

        // Return the results.

        return results;

    }

    // Recursively permute the items that are

    // not yet in the current selection.

    private void PermuteItems<T>(List<T> items, bool[] in_selection,

        T[] current_permutation, List<List<T>> results, int next_position)

    {

        // See if all of the positions are filled.

        if (next_position == items.Count)

        {

            // All of the positioned are filled.

            // Save this permutation.

            results.Add(current_permutation.ToList());

        }

        else

        {

            // Try options for the next position.

            for (int i = 0; i < items.Count; i++)

            {

                if (!in_selection[i])

                {

                    // Add this item to the current permutation.

                    in_selection[i] = true;

                    current_permutation[next_position] = items[i];

                    // Recursively fill the remaining positions.

                    PermuteItems<T>(items, in_selection,

                        current_permutation, results, next_position + 1);

                    // Remove the item from the current permutation.

                    in_selection[i] = false;

                }

            }

        }

    }

    // Return n!

    private long Factorial(long n)

    {

        long result = 1;

        for (int i = 2; i <= n; i++) result *= i;

        return result;

    } 
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,375 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,249 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 31,976 Reputation points Microsoft Vendor
    2024-03-12T09:26:31.1366667+00:00

    Hi @Jay-R Hayashi , Welcome to Microsoft Q&A,

    I still don't understand your question. Are you pursuing such code? You can use C#'s LINQ library to accomplish this task.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace xxx
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                int[] arr = { 1, 2, 3, 4 };
                int limit = 2;//3
    
                var permutations = GetPermutations(arr, limit);
    
                foreach (var perm in permutations)
                {
                    Console.WriteLine(string.Join(", ", perm));
                }
                Console.ReadLine();
            }
    
            static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> items, int limit)
            {
                if (limit <= 0 || items == null)
                    yield break;
    
                var itemsList = items.ToList();
                var indices = Enumerable.Range(0, itemsList.Count);
    
                foreach (var indicesPerm in GetPermutations(indices, limit))
                {
                    yield return indicesPerm.Select(i => itemsList[i]);
                }
            }
    
            static IEnumerable<IEnumerable<int>> GetPermutations(IEnumerable<int> indices, int limit)
            {
                if (limit == 1)
                {
                    foreach (var index in indices)
                        yield return new[] { index };
                    yield break;
                }
    
                var indicesList = indices.ToList();
                var subPermutations = GetPermutations(indices, limit - 1);
    
                foreach (var index in indicesList)
                {
                    foreach (var subPerm in subPermutations)
                    {
                        if (!subPerm.Contains(index))
                        {
                            yield return subPerm.Append(index);
                        }
                    }
                }
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful