tags:

views:

77

answers:

3

When an attempt 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 when attempting to use first.Count() ,second.Count() ? (I did not check whether it is the feasible solution for the problem).

A: 

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)

Kobi
A: 

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...

Jon Skeet
I have to select seven string element so i used first.Count()(),second.Count() ( I thought it will return the number of string in array) As i stated in the problem i have to select possible non-repeatable sub-element from nine elements. Won't length return number of characters in a single string ?
Here seven-elements mean {"a","b","c","d","e",'f","g"} , {"a","b","c","d","e","g","h"},ect.. I am interested in finding such sub-set of seven elements.Hope i explained what i really wanted.I am ready to explain if you still need additional information.
+2  A: 

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.

flq