示例#1
0
        //           Questions
        //1. The complexity is linear: O(n)
        //2. The complexity is likely Quadratic: O(n^2)
        //3. Using a dictionary instead

        static void Main(string[] args)
        {
            // Creates a new file reader, which loads a file of words
            // into both a list and a dictionary
            PracticeExerciseFileReader reader = new PracticeExerciseFileReader();

            // Get the two data structures needed for the exercise
            List <String>             wordList       = reader.WordList;
            Dictionary <String, bool> wordDictionary = reader.WordDictionary;

            //I had to add an extra list here to change the values of the doublewords to true
            List <String> doubleList = new List <string>();

            // *********************
            // Put your code between here...

            //asks for imput to use lists or dictionary
            Console.WriteLine("Would you like to search using the <L>ist or the <D>ictionary?");
            string answer = Console.ReadLine();

            answer = answer.ToUpper();
            char answerChar = answer[0];

            //checks each word to see if it's a double of whatever item is, then prints it and adds 1 to numDoubleWords to print how many there were
            if (answerChar == 'L')
            {
                int numDoubleWords = 0;
                foreach (string item in wordList)
                {
                    string doubleWord = item + item;
                    if (wordList.Contains(doubleWord))
                    {
                        Console.WriteLine(doubleWord);
                        numDoubleWords++;
                    }
                }
                Console.WriteLine("There were " + numDoubleWords + " double words.");
            }
            //Adds each doubleword to a list then sets each of those words' values to true. Then calls Search()
            else if (answerChar == 'D')
            {
                int numDoubleWords = 0;
                foreach (KeyValuePair <string, bool> item in wordDictionary)
                {
                    string doubleWord = item.Key + item.Key;
                    if (wordDictionary.ContainsKey(doubleWord))
                    {
                        Console.WriteLine(doubleWord);
                        numDoubleWords++;
                        doubleList.Add(doubleWord);
                    }
                }
                Console.WriteLine("there were " + numDoubleWords + " double words.");
                //I had to make a seperate list to store the words, since I couldn't change them in the Foreach loop
                for (int i = 0; i < doubleList.Count; i++)
                {
                    wordDictionary[doubleList[i]] = true;
                }

                Search(wordDictionary);
            }
            else
            {
                Console.WriteLine("Incorrect entry.");
                Console.WriteLine();
                Main(args);
            }

            // ...and here.
            // *********************

            // All done, wait for user input to keep the window open
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            //QUESTIONS
            // 1.) O(n)
            // 2.) O(n(n))
            // 3.) Probably by using a dictionary


            // Creates a new file reader, which loads a file of words
            // into both a list and a dictionary
            PracticeExerciseFileReader reader = new PracticeExerciseFileReader();

            // Get the two data structures needed for the exercise
            List <String>             wordList       = reader.WordList;
            Dictionary <String, bool> wordDictionary = reader.WordDictionary;

            // *********************
            // Put your code between here...

            //String to store user input
            string input;

            do
            {
                string doubleWord;
                Console.WriteLine("Choose a collection to search (\"L\" for List, \"D\" for Dictionary).");
                Console.WriteLine("Enter \"E\" to exit.");
                input = Console.ReadLine().Trim().ToUpper();


                if (input == "L")
                {
                    foreach (string word in wordList)
                    {
                        doubleWord = word + word;
                        bool doesContain = wordList.Contains(doubleWord);
                        if (doesContain == true)
                        {
                            Console.WriteLine(doubleWord);
                        }
                    }
                }
                else if (input == "D")
                {
                    foreach (string word in wordList)
                    {
                        doubleWord = word + word;
                        bool doesContain = wordDictionary.ContainsKey(doubleWord);
                        if (doesContain == true)
                        {
                            wordDictionary["doubleWord"] = true;
                            Console.WriteLine(doubleWord);
                        }
                    }
                    Console.WriteLine();

                    Console.WriteLine("Enter a word to search the dictionary for its doubled form: ");
                    string dictInput    = Console.ReadLine().Trim().ToLower();
                    string doubleInput  = dictInput + dictInput;
                    bool   inputContain = wordDictionary.ContainsKey(doubleInput);
                    if (inputContain == true)
                    {
                        Console.WriteLine("Doubled word found in dictionary!");
                    }
                    else
                    {
                        Console.WriteLine("Doubled word not found.");
                    }
                    Console.WriteLine();
                }
            }while (input != "E");



            // ...and here.
            // *********************
        }