Share via

Test All combination

MiPakTeh 1,476 Reputation points
2022-03-10T10:46:18.59+00:00

Hi All ,

I want to generate all the length-3, length-2, length-1 combinations of integers or string from text file.

It possible complete by Loop method.?

text file = 5682,2114,5671,3335,5603
0251,1292,8111,7166,6149

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace TestFor_Other2  
{  
    public partial class Form1 : Form  
    {  
        List<string> Data1 = new List<string>();  
        List<string> Data2 = new List<string>();  
  
        public Form1()  
        {  
            InitializeComponent();  
            Data1.AddRange(File.ReadAllLines(@"C:\Users\family\Documents\Tool.txt"));  
  
        }  
  
            private void button1_Click(object sender, EventArgs e)  
        {  
            String st = "";  
  
  
            for (int ii = 0; ii < Data1.Count; ii++)  
            {  
                var StringTocheck = Data1[ii].Split(',').ToString();  
                {  
  
  
                    for (int i = 0; i < StringTocheck.Length; i++)  
                    {  
                        for (int j = 0; j < StringTocheck.Length; j++)  
                        {  
                            for (int k = 0; k < StringTocheck.Length; k++)  
                            {  
                                st += StringTocheck[i].ToString() + "    ";  
                                st += StringTocheck[i].ToString() + StringTocheck[j].ToString() + "    ";  
                                st += StringTocheck[i].ToString() + StringTocheck[j].ToString() + StringTocheck[k].ToString() + "    ";  
  
                            }  
                        }  
                    }  
                }  
                listBox1.Items.Add(st);  
  
            }  
  
        }  
  
        private void button2_Click(object sender, EventArgs e)  
        {  
  
        }  
    }  
}  



  
  
Developer technologies | C#
Developer technologies | 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.


Answer accepted by question author

Jack J Jun 25,306 Reputation points
2022-03-11T09:37:16.047+00:00

@MiPakTeh , based your description, you want to generate all the length-3, length-2, length-1 combinations of integers or string from text file.

I make a code example and you could refer to it.

 public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
            Data1.AddRange(File.ReadAllLines(@"Tool.txt"));  
        }  
        List<string> Data1 = new List<string>();  
        char[] b;  
        string newstring;  
     
        private void button1_Click(object sender, EventArgs e)  
        {  
            for (int ii = 0; ii < Data1.Count; ii++)  
            {  
                List<string> list1 = new List<string>();  
                List<string> list2 = new List<string>();  
                var arr = Data1[ii].Split(',');  
                foreach (var item in arr)  
                {  
                    IEnumerable<IEnumerable<char>> result =  
    GetPermutations(item.ToCharArray().AsEnumerable(), 2);  
                    foreach (var item1 in result)  
                    {  
                        b = item1.ToList().ToArray();  
                        Array.Sort(b);  
                        newstring = new string(b);  
                        if(!newstring.StartsWith("0"))  
                        {  
                            list1.Add(newstring);  
                        }  
  
                        //list1.Add(newstring);  
  
                    }  
  
                    IEnumerable<IEnumerable<char>> result1 =  
   GetPermutations(item.ToCharArray().AsEnumerable(), 3);  
                    foreach (var item1 in result1)  
                    {  
                        b = item1.ToList().ToArray();  
                        Array.Sort(b);  
                        newstring = new string(b);  
                        if (!newstring.StartsWith("0"))  
                        {  
                              
                            list2.Add(newstring);  
                        }  
                        else  
                        {  
                            newstring=newstring.Remove(0, 1);  
                            list1.Add(newstring);  
                        }  
                        
                    }  
                }  
  
  
  
                listBox1.Items.Add(Data1[ii]);  
                listBox1.Items.Add("number 2");  
                listBox1.Items.AddRange(list1.ToArray());  
                listBox1.Items.Add("number 3");  
                listBox1.Items.AddRange(list2.ToArray());  
  
            }  
            
        }  
  
        static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)  
        {  
            if (length == 1) return list.Select(t => new T[] { t });  
  
            return GetPermutations(list, length - 1)  
                .SelectMany(t => list.Where(e => !t.Contains(e)),  
                    (t1, t2) => t1.Concat(new T[] { t2 }));  
        }  
  
  
    }  

Result:

182233-image.png


If the answer is the right solution, please click "Accept Answer" and 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.

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-03-11T11:49:44.447+00:00

    If using .NET 6 (and if .NET Core 5, see the following class which once added to your project provides the same capabilities as .NET Core 6)

    private static void ChunkingBy2()  
    {  
        string input = "56822114567133355603";  
      
        List<char[]> output = input  
            .Chunk(2)  
            .Select(chunk => chunk).ToList();  
      
        StringBuilder builder = new();  
        foreach (var item in output)  
        {  
            builder.Append(new string(item.ToArray()) + " ");  
        }  
      
        Console.WriteLine(builder);  
        Console.WriteLine();  
    }  
    private static void ChunkingBy3()  
    {  
        string input = "56822114567133355603";  
      
        List<char[]> output = input  
            .Chunk(3)  
            .Select(chunk => chunk).ToList();  
      
        StringBuilder builder = new();  
        foreach (var item in output)  
        {  
            builder.Append(new string(item.ToArray()) + " ");  
        }  
      
        Console.WriteLine(builder);  
      
    }  
    

    182226-figure1.png

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.