Quick question. I am writing a program to find all the permutations of a set of characters I input into the application. This part works perfectly. My problem is that I need to check all the permutations of the characters against a text file I use as a dictionary. Ex. If I input the characters TSTE the outputs givin are tset,ttse,ttes,tets,test,stte,stet,sett... I only want to print the valid words like tset,sett,stet,test,tets. where ttse,ttes,stte is not printed.
The code I have so far is as follows. I have been scracthing at the edges of my scull for the past few days and just cant seem to find a way to do it. Please if there is anything you can see that I have missed?
Thank you
static void Main(string[] args)
{
Console.BufferHeight = Int16.MaxValue - 1;
Console.WindowHeight = 40;
Console.WindowWidth = 120;
Permute p = new Permute();
var d = Read();
string line;
string str = System.IO.File.ReadAllText("Dictionary.txt");
while ((line = Console.ReadLine()) != null)
{
char[] c2 = line.ToArray();
p.setper(c2);
}
}
static Dictionary<string, string> Read()
{
var d = new Dictionary<string, string>();
using (StreamReader sr = new StreamReader("Dictionary.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string a = Alphabet(line);
string v;
if (d.TryGetValue(a, out v))
{
d[a] = v + "," + line;
}
else
{
d.Add(a, line);
}
}
}
return d;
}
static string Alphabet(string s)
{
char[] a = s.ToCharArray();
Array.Sort(a);
return new string(a);
}
static void Show(Dictionary<string, string> d, string w)
{
string v;
if (d.TryGetValue(Alphabet(w), out v))
{
Console.WriteLine(v);
}
else
{
Console.WriteLine("-----------");
}
}
}
class Permute
{
private void swap(ref char a, ref char b)
{
if (a == b) return;
a ^= b;
b ^= a;
a ^= b;
}
public void setper(char[] list)
{
int x = list.Length - 1;
go(list, 0, x);
}
public void go(char[] list1, int k, int m)
{
if (k == m)
{
Console.WriteLine(list1);
Console.WriteLine(" ");
}
else
{
for (int i = k; i <= m; i++)
{
swap(ref list1[k], ref list1[i]);
go(list1, k + 1, m);
swap(ref list1[k], ref list1[i]);
}
}
}
Although for the valid words are as I mentioned in another answer, not possible with your current state, I've make the code pretty clear for you.
It finally the code, is separated in three parts. They are
Permutable<T>
,ConsoleHelper
and theConsoleApplication1.Program
with an utilityPermutation
class.Permutable<T> (Permutable.cs)
ConsoleHelper (ConsoleHelper.cs)
ConsoleApplication1.Program & Permutation (Program.cs)
The
ConsoleHelper
class is only use for ceteral your resized console window, that you can test the program more comfortable. ThePermutable
class is what I fully re-designed from your original class; it now come withIEnumerable
to permute, and also a generic class.The
Permutation
class, is like a utility class, itself provide aFromFile
method as your originalRead
, and stores theDictionary<String, String>
as a property. The original methodAlphabet
is renamedGetSortedAlphabet
for more semantical.The
Program
class has four main staticProcessXXX
methods, they arechar
of the range from0x20
to0x7f
Enter
pressedExcape
pressedProcessLine
andProcessExit
Yes, I meant to make them have the same length of name. For more,
Program
class have two static properties;Permutation
for storing a instance of classPermutation
, andPrompt
is a string for prompting user.When your input is not defined in
"c:\dictionary.txt"
, the prompt of"Acceptable in file .."
will get you"<none>"
.The exit command is twice-Escape key press, you would see
and if you press
Enter
or other keys, it back to the initial state.That's all. Now it's time to discover the finally problem you've encountered. After you test with this program, you might describe you questions and issues more clearly.
Good luck!
Not possible with current state.
It's possible because
If the "dictionary.txt" was your input, then you did not define what are the valid words. You at leaset need another dictionary to define those valid words or an algorithm to decide what those are and what those are not.
If the input is from UI and "dictionary.txt" is the defined valid words, you don't need to the permute the input, just search the input from "dictionary.txt".
However, your
Read
method is improvable, likeThank you for all your feedback. I have finally figured it out and found a solution to the problem I was having. static void Main(string[] args) { Console.BufferHeight = Int16.MaxValue - 1;
you just missed Show(d, line); i loop.
change like this.
then .. you didn't required dictionary
before printing each word