Every 2 Letter Combo for Array of Strings

  • Thread starter Thread starter sloan
  • Start date Start date
S

sloan

string[] array = new string[3] {"A","B","C"};


Given the above data, I'm trying to find C# code which will produce




A,A
B,A
C,A
A,B
B,B
C,B
A,C
B,C
C,C


Its been a long week, and I'm brain dead, and googled out.


Thanks for any help.
 
Erm, loop?

string[] array = { "A", "B", "C" };
for(int i = 0; i < array.Length; i++)
for (int j = 0; j < array.Length; j++)
{
Console.WriteLine("{0},{1}", array[j], array);
}

You could do something more exciting, but that should do...

Marc
 
sloan said:
string[] array = new string[3] {"A","B","C"};

Given the above data, I'm trying to find C# code which will produce

A,A
B,A
C,A
A,B
B,B
C,B
A,C
B,C
C,C


Its been a long week, and I'm brain dead, and googled out.

LINQ makes this really sweet (IMO):

using System;
using System.Linq;

class Test
{
static void Main()
{
string[] array = new string[3] {"A","B","C"};

var combos =
from first in array
from second in array
select new { First = first, Second = second };

foreach (var combo in combos)
{
Console.WriteLine("{0},{1}",
combo.First,
combo.Second);
}
}
}

There are alternatives, of course:

foreach (string first in array)
{
foreach (string second in array)
{
Console.WriteLine ("{0},{1}", first, second);
}
}
 
Like I said dude....brain dead.

I know that feeling;-p

Currently in "crunch"; I don't expect to be getting to sleep any time
soon...

Marc
 
Something more exciting being (if the OP is using .NET 3.5/C# 3.0):

string[] array = { "A", "B", "C" };

IEnumerable<string> query =
from a in array
from b in array
select a + "," + b // Could be string.Format("{0},{1}", a, b) as well if
you wish.

foreach (string item in query)
{
Console.WriteLine(item);
}
 
Nicholas,Jon....

Yeah, that was more along the lines. I wasn't super clear.


Part of the issue (and why I'm brain fried) is that I"ve been in TSQL for
the last 2 days.


//tsql mindset thinking
select
alph1.AlphabetLabel , alph2.AlphabetLabel ,
, TheSame =
case when alph1.AlphabetLabel = alph2.AlphabetLabel then 0
else 1
end
from
dbo.Alphabet alph1 cross join dbo.Alphabet alph2


Thanks for the linq equivalents.






Jon Skeet said:
sloan said:
string[] array = new string[3] {"A","B","C"};

Given the above data, I'm trying to find C# code which will produce

A,A
B,A
C,A
A,B
B,B
C,B
A,C
B,C
C,C


Its been a long week, and I'm brain dead, and googled out.

LINQ makes this really sweet (IMO):

using System;
using System.Linq;

class Test
{
static void Main()
{
string[] array = new string[3] {"A","B","C"};

var combos =
from first in array
from second in array
select new { First = first, Second = second };

foreach (var combo in combos)
{
Console.WriteLine("{0},{1}",
combo.First,
combo.Second);
}
}
}

There are alternatives, of course:

foreach (string first in array)
{
foreach (string second in array)
{
Console.WriteLine ("{0},{1}", first, second);
}
}
 
(laughs)

I actually deliberately stopped myself posting the LINQ version, aware
(over-cautious) that LINQ isn't yet fully mainstream... oh well ;-p

Marc
 
Well, in SQL, you don't have to explicitly state CROSS JOIN anymore (and
I do believe it is actually not recommended now, but I'd have to look that
up), you can just do:

select
a1.AlphabetLabel as A1Label, a2.AlphabetLabel as A2Label,
case a1.AlphabetLabel when a2.AlphabetLabel then 0 else 1 end as NotSame
from
Alphabet as a1, Alphabet as a2

Consequently, in LINQ, you can get the flag with:

IEnumerable<string> query =
from a in array
from b in array
select new { A1Label = a, A2Label = b, NotSame = (a != b) };


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

sloan said:
Nicholas,Jon....

Yeah, that was more along the lines. I wasn't super clear.


Part of the issue (and why I'm brain fried) is that I"ve been in TSQL for
the last 2 days.


//tsql mindset thinking
select
alph1.AlphabetLabel , alph2.AlphabetLabel ,
, TheSame =
case when alph1.AlphabetLabel = alph2.AlphabetLabel then 0
else 1
end
from
dbo.Alphabet alph1 cross join dbo.Alphabet alph2


Thanks for the linq equivalents.






Jon Skeet said:
sloan said:
string[] array = new string[3] {"A","B","C"};

Given the above data, I'm trying to find C# code which will produce

A,A
B,A
C,A
A,B
B,B
C,B
A,C
B,C
C,C


Its been a long week, and I'm brain dead, and googled out.

LINQ makes this really sweet (IMO):

using System;
using System.Linq;

class Test
{
static void Main()
{
string[] array = new string[3] {"A","B","C"};

var combos =
from first in array
from second in array
select new { First = first, Second = second };

foreach (var combo in combos)
{
Console.WriteLine("{0},{1}",
combo.First,
combo.Second);
}
}
}

There are alternatives, of course:

foreach (string first in array)
{
foreach (string second in array)
{
Console.WriteLine ("{0},{1}", first, second);
}
}
 
IEnumerable said:
    ...
    select new { A1Label = a, A2Label = b, NotSame = (a != b) };

(minor; just for the OP really) - you'd need "var query" here because
of the anonymous type in the projection

Marc
 
Nicholas said:
Well, in SQL, you don't have to explicitly state CROSS JOIN anymore (and
I do believe it is actually not recommended now, but I'd have to look that
up), you can just do:

select
a1.AlphabetLabel as A1Label, a2.AlphabetLabel as A2Label,
case a1.AlphabetLabel when a2.AlphabetLabel then 0 else 1 end as NotSame
from
Alphabet as a1, Alphabet as a2
CROSS JOIN was introduced in SQL-92 to solve some ambiguity problems with
the comma notation and increase clarity (before this you couldn't use them,
so it's not a case of "not having to state it anymore"; the comma notation
came first). I'm not aware of any explicit recommendation for or against,
and either syntax will work fine.

However, it's still a very good idea to write out the CROSS JOIN explicitly,
since it's the most uncommon form of join (even if it's the most natural
one, mathematically speaking) and alerting your fellow developer to its use
is a good idea. An "accidental" cross join is not a good thing to have (as
even moderately-sized tables will produce enormous result sets) which is
exactly why ANSI SQL introduced explicit syntax for them.
 
However, it's still a very good idea to write out the CROSS JOIN explicitly,
since it's the most uncommon form of join (even if it's the most natural
one, mathematically speaking) and alerting your fellow developer to its use
is a good idea. An "accidental" cross join is not a good thing to have (as
even moderately-sized tables will produce enormous result sets) which is
exactly why ANSI SQL introduced explicit syntax for them.

This reminded me of a terrible, terrible joke I inflicted on Chris
Mullins recently. Why does the compiler complain about this query
expression?

var query = from x in new MemoryStream()
from y in new FileStream("foo.txt")
select x+y;

(Bonus points for the most appropriate compiler error.)
 
sloan said:
string[] array = new string[3] {"A","B","C"};

Given the above data, I'm trying to find C# code which will produce

A,A
B,A
C,A
A,B
B,B
C,B
A,C
B,C
C,C

If it should be reusable you should go for recursion.

Example below.

Arne

================================================

using System;

namespace E
{
public class Combiner
{
public delegate void Process(string comb);
private static void Combine(string[] elms, int len, Process p, string
comb, int ix)
{
if(ix < len)
{
for(int i = 0; i < elms.Length; i++)
{
Combine(elms, len, p, comb + elms, ix + 1);
}
}
else
{
p(comb);
}
}
public static void Combine(string[] elms, int len, Process p)
{
Combine(elms, len, p, "", 0);
}
public static void Main(string[] args)
{
Combiner.Combine(new string[] { "A", "B", "C" }, 2, delegate(string
comb) { Console.WriteLine(comb); });
Console.ReadKey();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top