Skip to content

Search Algorithm Required

Vishnu Reghu

Asked Jun 21, 2012, 4:42 PM EDT3 answers
Share
LinkedInX
Hi,

        I need a c# program ( search algorithm ), ie, When user types a character, the display should update to show all valid choices for the next character
and a list of possible matching stations.

Ex: User Input: D A R T, it should display DARTFORD and DARTMOUTH

Suggested outline is below:

C#
public class Suggestions
{
     HastSet<Character> nextLetters { get; set; }
     HashSet<String> stations { get; set; }


}


public class StationFinder
{
     private static String[] stations = new String[] {"LIVERPOOL LIME STREET", "BIRMINGHAM NEW STREET", "KINGSTON", " DARTFORD", "DARTMOUTH" };


     public Suggestions GetSuggestions( String userInput )
    {
          // TODO Compute result
          Suggestions result = new Suggestions();
          return result;
    }






}

Note: The above code snippet is a suggestion, it can be modified if wish.

Regards,
Vishnu

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi Vishnu. I thought I'd give it this a go. I created a simple form and added a combo box. I changed the DropDownStyle property to "Simple" so it looked like a text box. The only thing I used in your code was this line:

    private static String[] stations = new String[] {"LIVERPOOL LIME STREET", "BIRMINGHAM NEW STREET", "KINGSTON", " DARTFORD", "DARTMOUTH" };

    I added that in the class, but outside all methods so it was global. I changed the AutoCompleteMode of the combobox to "Suggest" and the AutoCompleteSource property to "CustomSource". Last I added a few lines of code to the form's constructor so it looked like this:
    Code
            public Form1()
            {
                InitializeComponent();
    
    
                foreach(string s in stations)
                {
                    comboBox1.AutoCompleteCustomSource.Add(s.Trim());
                }
            }

    One thing to note wsa that you have a space before DARTFORD. Didn't know if that was intentional, but I added .Trim() which took care of that. Anyway, I hope that helps. Always nice to accomplish something in just a few lines of code.
    0
  • 0
    Thanks for the reply. I am looking for a solution using HashSet. Is there any available ?
    0
  • 0
    Hi again Vishnu. HashSet is a way to eliminate redundancies. The method I used above is using built in methods to use autocomplete which takes care of redundancies. However, if you need to use a HashSet, it can be done by adding a couple lines of code:

    public partial class Form1 : Form
        {
            private static String[] stations = new String[] { "LIVERPOOL LIME STREET", "BIRMINGHAM NEW STREET", "KINGSTON", " DARTFORD", "DARTMOUTH", "BIRMINGHAM NEW STREET"};
           
            public Form1()
            {
                InitializeComponent();


                HashSet<string> stations_Hash = new HashSet<string>();
               
                foreach(string s in stations)
                {
                    stations_Hash.Add(s);
                }


                foreach (string sh in stations_Hash)
                {
                    comboBox1.AutoCompleteCustomSource.Add(sh.Trim());
                }
            }


        }

    Notice I added an extra  "BIRMINGHAM NEW STREET" to the array. However, it will only show up once in the autocomplete.

    I guess the real question is what controls are you wanting to use to display the data? Are you forced to use a text box maybe? I suppose if I understood the circumstances a little more, I might be more helpful. I hope this helps. 
    0