views:

345

answers:

3

Hello,

I've been playing with idea to make a script to generate 2-characters words from a given set of characters in my language. However, as I am not into re-inventing the wheel, do you know about such a script publicly available for C#?

+5  A: 

I'm not sure if I understood your question correctly, but this might help:

List<string> GetWords(IEnumberable<char> characters) {
    char[] chars = characters.Distinct().ToArray();
    List<string> words = new List<string>(chars.Length*chars.Length);
    foreach (char i in chars)
       foreach (char j in chars)
          words.Add(i.ToString() + j.ToString());
    return words;
}
Mehrdad Afshari
A: 

as I am not into re-inventing the wheel,

Bruwwwhahahahah. Let me guess, you're also not into doing homework? :)

no need for sarcasm, either you help, or you don't, isn't that the Modus operandi for SO? he had a question, a legit question.
balexandre
Sarcasm is best expressed in comments (can't be voted down :)
Cyril Gupta
Good point Cyril :)
you must be very fast to do your job.. I'm not so skilled, but I understand the power of good people and internet which gives me so-so good idea of how to manage it fast :)
Skuta
You could delete this and at least get a badge.
Mehrdad Afshari
This ludicrously simple question serves absolutely no purpose. If someone can't figure this out, they're in their 1st class. Will you also answer: help! how to add 1 and 2? Doing others' homework is not helpful. Mehrdad: I'd delete it if it got me a STAR, but for a lousy badge, I couldn't care less.
A: 

Are you talking about finding real two-character words that can be made from any combination of a list of characters?

In which case, you need to write an algorithm that can work out all the possible combinations from the letters provided, and for each combination, try it (and the reverse) against an IDictionary that acts like a real dictionary of real two-letter words.

Untested code:

IDictionary<string, string> dictionary = GetRealTwoLetterWordDictionary();
char[] availableChars = new char[] { 'a', 's', 't' };
string[] combinations = GetAllCombinations(availableChars);
IList<string> results = new List<string>();

foreach (string combination in combinations)
{
    if (dictionary.ContainsKey(combination)))
    {
     results.Add(combination);
    }

    string reversed = combination.Reverse();

    if (dictionary.ContainsKey(reversed)))
    {
     results.Add(reversed);
    }
}
Neil Barnwell