user192142
user192142

Reputation:

What is the way to apply extension methods?

When attempting to solve the problem

How many seven-element subsets (not repeatable) are there in a set of nine elements ?

I tried

IEnumerable<string> NineSet =new string[] {"a","b","c","d","e","f","g","h","i"};

var SevenSet = 
from first in NineSet
from second in NineSet
where first.CompareTo(second)< 0 && first.Count() + second.Count()==7
select new { first, second };

What is the problem that prevents me from attempting to use first.Count() and second.Count()? I did not check whether it is the best solution for the problem.

Upvotes: 0

Views: 127

Answers (3)

flq
flq

Reputation: 22829

As already stated, what you have written down will lead you to nowhere. This is a question of combinatorics. AFAIK there is nothing pre-made in the .NET framework to solve for you combinatorics problems, hence you will have to implement the correct algorithm. If you get stuck, there are solutions out there, e.g. http://www.codeproject.com/KB/recipes/Combinatorics.aspx, where you can look at the source to see what you need to do.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1499870

Well...

  • You haven't shown the error message, so it's hard to know what's wrong
  • Every element is of length exactly one, so I'm not sure what you're expecting to happen
  • As you know that first and second are strings, why aren't you using first.Length and second.Length?

As a side issue, I don't think this approach is going to solve the problem for you, I'm afraid...

Upvotes: 1

Kobi
Kobi

Reputation: 137997

first and second are strings, so you'll count their characters (this compiles, but intellisence hides it).
You're looking for something like NineSet.Count(first.Equals)

Upvotes: 1

Related Questions