473,395 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Re: Generating letter combinations -- please help

I haven't let it run to completion, but how about below; it uses a
char[] rather than lots of strings to save the GC some work... it only
goes 5 levels deep on the stack, so shouldn't blow up...

Marc

static void Main()
{
char[] data = new char[5];
Loop(data, 0);
Console.WriteLine(count);
}
static int count;
static void Loop(char[] buffer, int level)
{
int nextLevel = level + 1;
for (char c = 'A'; c < 'Z'; c++)
{
buffer[level] = c;
if (nextLevel == buffer.Length)
{
Console.WriteLine(buffer);
count++;
}
else
{
Loop(buffer, nextLevel);
}
}
}
Oct 15 '08 #1
1 1622
On Oct 15, 3:26*pm, Marc Gravell <marc.grav...@gmail.comwrote:
I haven't let it run to completion, but how about below; it uses a
char[] rather than lots of strings to save the GC some work... it only
goes 5 levels deep on the stack, so shouldn't blow up...

Marc

* * * * static void Main()
* * * * {
* * * * * * char[] data = new char[5];
* * * * * * Loop(data, 0);
* * * * * * Console.WriteLine(count);
* * * * }
* * * * static int count;
* * * * static void Loop(char[] buffer, int level)
* * * * {
* * * * * * int nextLevel = level + 1;
* * * * * * for (char c = 'A'; c < 'Z'; c++)
* * * * * * {
* * * * * * * * buffer[level] = c;
* * * * * * * * if (nextLevel == buffer.Length)
* * * * * * * * {
* * * * * * * * * * Console.WriteLine(buffer);
* * * * * * * * * * count++;
* * * * * * * * }
* * * * * * * * else
* * * * * * * * {
* * * * * * * * * * Loop(buffer, nextLevel);
* * * * * * * * }
* * * * * * }
* * * * }
Marc,

This is tight but I like it. Yes, it works. I also added a
container to it (like a hastable ot arraylist to store the
enumeration). Say, if you had a array of chars say char[] myChars =
{'A', 'C', 'E', 'X', 'Y'}
or something liek that - do you think it possible to cycle through
them and not all the letters in the alphabet?

PS: Small issue, I changed:

for (char c = 'A'; c < 'Z'; c++)

to

for (char c = 'A'; c <= 'Z'; c++)
Oct 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Micheal Artindale | last post by:
I am looking at creating list of letter combinations. letters a-h 6 letters per combination letter can repeat its self in the combination, but not next to its self and, a series of letter can...
1
by: Videotron | last post by:
Thanks to James A. Fortune in this Newsgroups "Creating random letter combinations", I execute the code he proposes and there were 500,000 combinations generated. This is more than we need to give...
16
by: akameswaran | last post by:
Ok, this is really irritating me. I'm sure there are different ways of doing this - I'm interested in the algo, not the practical solution, I'm more trying to play with iterators and recursion. I...
8
by: Mir Nazim | last post by:
Hello, I need to write scripts in which I need to generate all posible unique combinations of an integer list. Lists are a minimum 12 elements in size with very large number of possible...
6
by: Jackie | last post by:
Hi, all, There are 50 folders in my hard driver C: C:\01.c:\02,...,c:\50 There are 4 pictures in each folder: 1.jpg,2.jpg,3.jpg,4.jpg For each folder, I want to print the 4 pictures into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.