- Хроники. - -
Генерация строк перебором букв.
Posted By Ikutsin On 12 января 2012 @ 12:28 In .NET C# | Comments Disabled
[1]Это маленький, но полезный метод для генерации словарей путем перебора всех возможных последовательностей, может быть полезен для:
Для оптимизации памяти, по хорошему, нужно использовать yield.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace NameGen
{
class Program
{
static string alpha = "abcdefghijklmnopqrstuvwxyz";
static string num = "0123456789";
static void Main(string[] args)
{
var output = @"C:\__autoSeo_results\names";
File.Delete(output);
List<string> elements = Generate(alpha);
File.WriteAllLines(output, elements);
}
static List<string> Generate(string chars, int len = 3)
{
int size = (int) Math.Pow(chars.Length, len);
List<string> elements = new List<string>(size);
int[] counters = new int[len];
do
{
elements.Add(new string(counters.Select(x=>chars[x]).ToArray()));
counters[0]++;
for (int i = 0; i < len; i++)
{
if (counters[i] == chars.Length)
{
if (i + 1 == len) return elements;
counters[i + 1]++;
counters[i] = 0;
}
}
} while (true);
}
}
}
Article printed from Хроники.:
URL to article: /2164-generaciya-strok-pereborom-bukv
URLs in this post:
[1] Image: /wp-content/uploads/2012/01/55.jpg
Click here to print.
Copyright © 2008 Все, что меня окружает. All rights reserved.