Бесконечно перечислять все возможные комбинации из английского алфавита - C#
Формулировка задачи:
Пишу небольшую консольную программу, которая должна бесконечно перечислять все возможные комбинации из английского алфавита. Но почему то, оно не работает: окно пустое. Можете помочь?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NBProject
{
class Program
{
static string[] totalarray = new string[26] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
static void Main(string[] args)
{
while (true)
{
var totallist = new List<string>(totalarray);
var allcombi = allcombinations(totallist, new List<string>());
foreach (var lst in allcombi)
output(lst);
}
}
private static IEnumerable<List<string>> allcombinations(List<string> arg, List<string> awithout)
{
if (arg.Count == 1)
{
var result = new List<List<string>>();
result.Add(new List<string>());
result[0].Add(arg[0]);
return result;
}
else
{
var result = new List<List<string>>();
foreach (var first in arg)
{
var others0 = new List<string>(arg.Except(new string[1] { first }));
awithout.Add(first);
var others = new List<string>(others0.Except(awithout));
var combinations = allcombinations(others, awithout);
awithout.Remove(first);
foreach (var tail in combinations)
{
tail.Insert(0, first);
result.Add(tail);
}
}
return result;
}
}
private static void output(IEnumerable<string> arg)
{
foreach (var str in arg)
Console.Write(str);
Console.WriteLine();
}
}
}Решение задачи: «Бесконечно перечислять все возможные комбинации из английского алфавита»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static string[] totalarray = new string[4] { "a", "b", "c", "d" };
static void Main(string[] args)
{
var totallist = new List<string>(totalarray);
var allcombi = allcombinations(totallist, new List<string>());
foreach (var lst in allcombi)
output(lst);
Console.ReadKey();
}
private static IEnumerable<List<string>> allcombinations(List<string> arg, List<string> awithout)
{
if (arg.Count == 1)
{
var result = new List<List<string>>();
result.Add(new List<string>());
result[0].Add(arg[0]);
return result;
}
else
{
var result = new List<List<string>>();
foreach (var first in arg)
{
var others0 = new List<string>(arg.Except(new string[1] { first }));
awithout.Add(first);
var others = new List<string>(others0.Except(awithout));
var combinations = allcombinations(others, awithout);
awithout.Remove(first);
foreach (var tail in combinations)
{
tail.Insert(0, first);
result.Add(tail);
}
}
return result;
}
}
private static void output(IEnumerable<string> arg)
{
foreach (var str in arg)
Console.Write(str);
Console.WriteLine();
}
}
}