I´m trying to get all permutations of words in a textbox, my code :
IEnumerable<string> GetPermutation(string s)
{
if (s.Length > 1)
{
return from c in s
from p in GetPermutation(s.Remove(s.IndexOf(c), 1))
select c + p;
}
else
{
return new string[] { s };
}
}
my doubt is to call this function I tried this :
string words = txt_search.Text;
string[] source = words.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string a in source)
{
Response.Write(GetPermutation(a));
}
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
(a) your subject says "all
combinations" and "without repetition".
(b) your code example has a method named GetPermutation.
do you mean combinations or permutations?
also, what you you mean by all? if you have for example five words, do you mean just "taken 5 at a time", or do you mean ""taken 5 at a time, 4 at a time, 3 at a time, ..."?
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
where n is positive, start with a List<T> of the
n objects, example: red green pink green yellow gold pink pink
order the List<T>, example: gold green green pink
pink pink red yellow
reduce the ordered List<T> to remove duplicates, example: gold green pink red yellow
this gives you nthings "taken one at a time".
BTW, you've removed the duplicates from your original List<T>, so now we have only to deal with
5things.
next, use the duplicate freeList<T>: iterate (loop) through it to build a list of 5things "taken two at a time", 5things "taken three at a time", ...
5things "taken five at a time",
g.
P.S.: just curious, is this a homework problem?
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Mojorz
Member
3 Points
22 Posts
Get All permutations of words without repetition in an array of strings
Nov 24, 2012 11:39 AM|LINK
Hi
I´m trying to get all permutations of words in a textbox, my code :
IEnumerable<string> GetPermutation(string s) { if (s.Length > 1) { return from c in s from p in GetPermutation(s.Remove(s.IndexOf(c), 1)) select c + p; } else { return new string[] { s }; } }string words = txt_search.Text; string[] source = words.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string a in source) { Response.Write(GetPermutation(a)); }Any help will be appreciated
Thanks
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Get All permutations of words without repetition in an array of strings
Nov 24, 2012 03:11 PM|LINK
@ Mojorz welcome to forums.asp.net.
Mojorz, you are doing well in that at least you've shown us your code.
Questions:
what test data have your tried? what results did you get?
please see this link http://forums.asp.net/post/5219397.aspx which i think will help you help yourself.
g.
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Get All permutations of words without repetition in an array of strings
Nov 24, 2012 03:38 PM|LINK
@ Mojorz
imho, your question needs some clarification. see http://weblogs.asp.net/gerrylowry/archive/2012/11/10/clarity-is-important-both-in-question-and-in-answer.aspx.
Let me explain.
(a) your subject says "all combinations" and "without repetition".
(b) your code example has a method named GetPermutation.
do you mean combinations or permutations?
also, what you you mean by all? if you have for example five words, do you mean just "taken 5 at a time", or do you mean ""taken 5 at a time, 4 at a time, 3 at a time, ..."?
g.
Mojorz
Member
3 Points
22 Posts
Re: Get All permutations of words without repetition in an array of strings
Nov 24, 2012 03:53 PM|LINK
Hi
Sorry I mean permutations when I posted this I was a little confused about what to use to implement this .
And for example for five words the result should bem 5 at a time , 4 , 3 and so on.
I´ve changed the topic sorry for the bad clarity .
Thanks
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Get All permutations of words without repetition in an array of strings
Nov 24, 2012 05:11 PM|LINK
@ Mojorz
Thank you for clarifying ...
TIMTOWTDI =. there is more than one way to do it
my initial approach would be to pick or create a few simple examples, i.e., test data.
if i did not have a sufficient understanding of permutations, i'd use Google to find links like http://en.wikipedia.org/wiki/Permutation.
now, it's important to think about this: duplicates affect your result.
if i have three coloured items, red1, green2, green3, where green2 and green3 are indistinguishable,
(a) three at a time, but, remember, i'll need to discard the duplicates:
red1 green2 green3
red1 green3 green2 <== discard, it's indistinguishable from red1 green2 green3
green2 red1 green3
green2 green3 red1
green3 red1 green2 <== discard, it's indistinguishable from green2 red1 green3
green3 green2 red1 <== discard, it's indistinguishable from green2 green3 red1
result:
red1 green2 green3
green2 red1 green3
green2 green3 red1
Mojorz, this suggests a strategy, in pseudo code:
where n is positive, start with a List<T> of the n objects, example: red green pink green yellow gold pink pink
order the List<T>, example: gold green green pink pink pink red yellow
reduce the ordered List<T> to remove duplicates, example: gold green pink red yellow
this gives you n things "taken one at a time".
BTW, you've removed the duplicates from your original List<T>, so now we have only to deal with 5 things.
next, use the duplicate free List<T>: iterate (loop) through it to build a list of 5 things "taken two at a time", 5 things "taken three at a time", ... 5 things "taken five at a time",
g.
P.S.: just curious, is this a homework problem?
Mojorz
Member
3 Points
22 Posts
Re: Get All permutations of words without repetition in an array of strings
Nov 25, 2012 10:35 AM|LINK
Thanks a lot gerrylowry
This exactly what I need , I ´m working on a solution now, I will post it here as soons as it works properly.
It is for my final projet
.
Once again thanks for your time, your explanation and links were great.
Wish you the best