tags:

views:

811

answers:

7

Hi all, just wondering if anyone had an algorithm lying around that printed all possible combos from 0000 to 9999 (trying to crack code into old phone and new to learning C#)...Million thanks. Bela

+25  A: 

Why complicate matters?

for (Int32 index = 0; index < 10000; index++)
    Console.Out.WriteLine(index.ToString("0000"));

Since you're commenting that you're outputting to a label, with linefeeds between each value, here's a better way:

List<String> values = new List<String>();
for (Int32 index = 0; index < 10000; index++)
    values.Add(index.ToString("0000"));
label1.Text = String.Join(
    Environment.NewLine,
    values.ToArray());

Try that and see if it gives you what you want.

Lasse V. Karlsen
Simple thing that works ;o)
Cédric Rup
Thanks! But for some reason this is going into an infinite loop (I think, cause the program just freezes)
The only girl in IT
Not with the code I posted, so you must've made an error when you wrote it down into your own program. Please post the code that freezes in your question.
Lasse V. Karlsen
@Lasse: what is wrong with Console.WriteLine? (same thing only shorter)
Brian Rasmussen
For that matter, what's wrong with `int` instead of `Int32`?
Eamon Nerbonne
It's just what I have in my code standard. I use the system types, not the C# reserved words, since that goes hand in hand with methods (GetInt32) in various runtime classes, and I always write .In. or .Out. on the Console class.
Lasse V. Karlsen
We haven't learnt about Console.WriteLine, I'm just outputting to a label, sorry guys, think I'm way to basic for you...
The only girl in IT
This is the code: for (int index = 0; index < 10000; index++) { label1.Text+= index.ToString("0000")+"\n"; }
The only girl in IT
I doubt it freezes, but it might take some time, since manipulating a string like that build 10000 temporary string. I'll post a better way.
Lasse V. Karlsen
+2  A: 

Do you want to count to 9999?

Makach
I wanna get all possible combos
The only girl in IT
and how is that different from counting?
Brian Rasmussen
+1  A: 
for (int index = 0; index < 10000; index++)
{
    Console.WriteLine(index);
}
A: 

Like Lasse said, simply print them in sequence. But you can do even simpler with Linq;

Enumerable.Range(0, 9999).ToList().ForEach(Console.WriteLine);

You could avoid the "ToList" as well, if you had a helper function (which I normally would have in a Utilities class);

public static void ForEach<T>(this IEnumerable<T> elements, Action<T> action)
{
    foreach (var element in elements) { action(element); }
}

Note that this utility method is not strictly required for it to work, but would greatly diminish the memory requirements (since ToList actcually creates a list of all the numbers in memory).

Digitalex
So you are that guy, the guy that shoots moskitos with missile launchers and AK47... I didn't know.
Clement Herreman
I downvoted you for suggesting a 'ForEach' extension, which is completely useless. The 'foreach' keyword is good enough, and this extension would only be confusing.
Noon Silk
I see your point, but it's a matter of personal opinion I think. Personally, I prefer one-liners for simple things like these.
Digitalex
Why do you need ToList ?
Konstantin Spirin
Also you are not keeping starting zeroes in your solution.
Konstantin Spirin
@ToList is needed for .ForEach - or you'll need something like MoreLinq...
Eamon Nerbonne
Good point, would be like others here have pointed out; in my example would be ForEach(x => Console.WriteLine("{0:0000}", x))
Digitalex
Eamon; or like the util-function I wrote. I find this handy for a lot of scenarios, and always put it in some Utilities class in my projects.
Digitalex
`Enumerable.Range(0,9999)` does not include the number 9999
Eamon Nerbonne
Eamon: indeed, you're right. I'll leave it as an exercise to the reader to correct this... :)
Digitalex
+10  A: 

WTF entry:

Console.WriteLine("0000"); 
Console.WriteLine("0001"); 
Console.WriteLine("0002"); 
Console.WriteLine("0003"); 

// snip everything in the middle

Console.WriteLine("9998"); 
Console.WriteLine("9999");

For those of you who are lacking of humor, don't try this at home.

Ngu Soon Hui
That's actually incorrect, she has indicated it should be 4 digit aligned.
Fix the thing so that it is 4 digit aligned.
Ngu Soon Hui
An answer worthy of question.
Pavel Minaev
Sad thing is that I have seen something like that in the wild ...
Brian Rasmussen
Note that for a complete WTF answer you should actually post all those lines here (you'll get a scrollbar for the code field), but then leave out a random line, just for kicks.
Lasse V. Karlsen
I can prompt how to make this task easier. Try this:Console.Write(@"0000000100020003....99989999");
macropas
Oh, my God, I forget about "\n"
macropas
The Real WTF answer would be to have the for loop contain a (switch i) { case 0: Console.WriteLine("0000"); case 1: Console.WriteLine("0001"); /* etc */ } (ref: http://thedailywtf.com/Articles/The_FOR-CASE_paradigm.aspx)
Greg Beech
@Greg, that's still not proper WTF: I don't see `throw` anywhere in there...
Pavel Minaev
A: 

if you want to print the leading zeros too:

for(int iC1 = 0; iC1 < 10000; iC1++)
{
 if(iC1 < 10)
 {
   Console.WriteLine("000" + iC1.ToString());
 }
 else if(iC1 < 100)
 {
   Console.WriteLine("00" + iC1.ToString());
 }
 else if(iC1 < 1000)
 {
   Console.WriteLine("0" + iC1.ToString());
 }
 else
 {
   Console.WriteLine(iC1.ToString());
 }
}

I know this solution is not elegant, but it's easyer to understand this way.

btw: you have to compile this as C# Console Application, not as Windows Forms Application.

Emiswelt
How is this easier to understand than just index.ToString("0000") or String.Format("{0:0000}",index)?
Why would you do this instead of the format specifier "0000" as in the existing answer that was posted half an hour ago?
Greg Beech
Geez thanks! I never expected so many answers so quickly! I will try this later, being called to dinner and have to go! Thanks again!
The only girl in IT
Because when I was a C# beginner, I had some troubles understanding format strings etc. I thought it may be easier to understand...
Emiswelt
+2  A: 

I think Digitalex's solution is the most elegant but too memory-consuming.

Here's a better option:

foreach (var number in Enumerable.Range(0, 10000).Select(i => i.ToString("0000")))
    Console.WriteLine(number);
Konstantin Spirin