Esempio n. 1
0
        public void Add_Unique_Adds()
        {
            HashTable<string, int> table = new HashTable<string, int>();
            List<int> added = new List<int>();

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(i, table.Count, "The count was incorrect");

                int value = _rng.Next();
                string key = value.ToString();

                // this ensure we should never throw on Add
                while (table.ContainsKey(key))
                {
                    value = _rng.Next();
                    key = value.ToString();
                }

                table.Add(key, value);
                added.Add(value);
            }

            // now make sure all the keys and values are there
            foreach (int value in added)
            {
                Assert.IsTrue(table.ContainsKey(value.ToString()), "ContainsKey returned false?");
                Assert.IsTrue(table.ContainsValue(value), "ContainsValue returned false?");

                int found = table[value.ToString()];
                Assert.AreEqual(value, found, "The indexed value was incorrect");
            }
        }
        public static void Main()
        {
            var grades = new HashTable<string, int>();
            Console.WriteLine("Grades:" + string.Join(",", grades));
            Console.WriteLine("--------------------");

            grades.Add("Peter", 3);
            grades.Add("Maria", 6);
            grades["George"] = 5;

            Console.WriteLine("Grades:" + string.Join(",", grades));
            Console.WriteLine("--------------------");

            grades.AddOrReplace("Peter", 33);
            grades.AddOrReplace("Tanya", 4);
            grades["George"] = 55;
            Console.WriteLine("Grades:" + string.Join(",", grades));
            Console.WriteLine("--------------------");

            Console.WriteLine("Keys: " + string.Join(", ", grades.Keys));
            Console.WriteLine("Values: " + string.Join(", ", grades.Values));
            Console.WriteLine("Count = " + string.Join(", ", grades.Count));
            Console.WriteLine("--------------------");

            grades.Remove("Peter");
            grades.Remove("George");
            grades.Remove("George");
            Console.WriteLine("Grades:" + string.Join(",", grades));
            Console.WriteLine("--------------------");

            Console.WriteLine("ContainsKey[\"Tanya\"] = " + grades.ContainsKey("Tanya"));
            Console.WriteLine("ContainsKey[\"George\"] = " + grades.ContainsKey("George"));
            Console.WriteLine("Grades[\"Tanya\"] = " + grades["Tanya"]);
            Console.WriteLine("--------------------");

            grades.Add("Maria1", 5);
            grades.Add("Maria12", 6);
            grades.Add("Peter 13", 5);
            grades.Add("Peter 14", 5);
            grades.Add("George 1", 6);
            grades.Add("George 11", 6);
            grades.Add("George 12", 5);
            grades.Add("George 2", 5);
            grades.Add("George 3", 5);
            grades.Add("Maria 3", 5);
            //grades.Add("Maria 2", 5);

            Console.WriteLine("\nCollisions deep: {0}\n--------------------", grades.CalculateCollisionsDeep());
            Console.WriteLine("\nHashTable preview: \n--------------------\n{0}", grades);
        }
        public void Remove_Found()
        {
            HashTable<string, int> table = new HashTable<string, int>();
            for (int i = 0; i < 100; i++)
            {
                table.Add(i.ToString(), i);
            }

            for (int i = 0; i < 100; i++)
            {
                Assert.IsTrue(table.ContainsKey(i.ToString()), "The key was not found in the collection");
                Assert.IsTrue(table.Remove(i.ToString()), "The value was not removed (or remove returned false)");
                Assert.IsFalse(table.ContainsKey(i.ToString()), "The key should not have been found in the collection");
            }
        }
        static void Main()
        {
            string stringText = Console.ReadLine();
            var symbolsColection = new HashTable<char, int>();

            for (int i = 0; i < stringText.Length; i++)
            {
                char symbol = stringText[i];
                if (!symbolsColection.ContainsKey(symbol))
                {
                    symbolsColection.Add(symbol, 1);
                }
                else
                {
                                       symbolsColection[symbol]++;
                }
            }

            var sortedSymbolsColection = symbolsColection.OrderBy(s => s.Key);

            foreach (var symbol in sortedSymbolsColection)
            {
                Console.WriteLine("{0}: {1} time/s", symbol.Key, symbol.Value);
            }
        }
Esempio n. 5
0
        public static void Main(string[] args)
        {
            Console.WriteLine ("Phonebook. Available commands:");
            Console.WriteLine ("");
            Console.WriteLine ("- a [name]|[phone] : Adds new contact.");
            Console.WriteLine ("- s [name] : Searches for specific name.");
            Console.WriteLine ("- q : quit.");
            Console.WriteLine ("");
            var phoneBook = new HashTable<string, string> (50);
            char command;
            do {
                string input = Console.ReadLine();
                command = input[0];
                if(input.Length<=2) {
                    continue;
                }
                var arguments = input.Substring(2);
                switch(command) {
                case 'a':
                    var pair = arguments.Split('|');
                    phoneBook[pair[0]] = pair[1];
                    break;
                case 's':
                    if( phoneBook.ContainsKey(arguments)) {
                        Console.WriteLine("{0} -> {1}", arguments, phoneBook[arguments]);
                    } else {
                        Console.WriteLine("Contact {0} does not exist.", arguments);
                    }
                    break;
                }

            } while ('q' != command);
        }
Esempio n. 6
0
 public static void Main(string[] args)
 {
     Console.WriteLine ("Enter some text");
     var input = Console.ReadLine ();
     var charsDictionary = new HashTable<string, int>(100);
     var charStrings = new List<string>(input.Length);
     foreach(var aChar in input) {
         charStrings.Add(aChar.ToString());
     }
     foreach (var charString in charStrings) {
         if (charsDictionary.ContainsKey (charString)) {
             charsDictionary [charString]++;
         } else {
             charsDictionary [charString] = 1;
         }
     }
     foreach (var charString in charsDictionary) {
         Console.WriteLine ("{0}: {1} time/s", charString.Key, charString.Value);
     }
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter end to stop program.");
            Console.WriteLine("Please enter search to star search phonenumbers.");
            var phonebook = new HashTable<string, string>();
            string inputText;
            while (true)
            {
                inputText = Console.ReadLine();

                if (inputText.Equals("end"))
                {
                    return;
                }

                if (inputText.Equals("search"))
                {
                    SearchNumbers(phonebook);
                    return;
                }

                string[] splitText = inputText.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                string inputName = splitText[0].Trim();

                if (splitText.Count() > 1)
                {
                    string phonenumber = splitText[1].Trim();

                    if (!phonebook.ContainsKey(inputName))
                    {
                        phonebook.Add(inputName, phonenumber);
                    }
                    else
                    {
                        phonebook[inputName] = phonenumber;
                    }
                }
            }
        }
 public static void SearchNumbers(HashTable<string, string> phonebook)
 {
     string inputName;
     while (true)
     {
         inputName = Console.ReadLine();
         if (inputName.Equals("end"))
         {
             return;
         }
         if (phonebook.ContainsKey(inputName))
         {
             string phonenumber;
             phonebook.TryGetValue(inputName, out phonenumber);
             Console.WriteLine("{0} -> {1}", inputName, phonenumber);
         }
         else
         {
             Console.WriteLine("Contact {0} does not exist.", inputName);
         }
     }
 }
Esempio n. 9
0
        public void Add_Duplicate_Throws()
        {
            HashTable<string, int> table = new HashTable<string, int>();
            List<int> added = new List<int>();

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(i, table.Count, "The count was incorrect");

                int value = _rng.Next();
                string key = value.ToString();

                // this ensure we should never throw on Add
                while (table.ContainsKey(key))
                {
                    value = _rng.Next();
                    key = value.ToString();
                }

                table.Add(key, value);
                added.Add(value);
            }

            // now make sure each attempt to re-add throws
            foreach (int value in added)
            {
                try
                {
                    table.Add(value.ToString(), value);
                    Assert.Fail("The Add operation should have thrown with the duplicate key");
                }
                catch (ArgumentException)
                {
                    // correct!
                }
            }
        }
        public static void Main()
        {
            var testHashtable = new HashTable<int, string>();
            testHashtable.Add(1, "hello");
            testHashtable.Add(2, "Bye");
            testHashtable.Add(3, "cat");
            testHashtable.Add(4, "dog");
            testHashtable.Add(5, "monkey");
            testHashtable.Add(6, "Luffy");
            testHashtable.Add(8, "eight");
            PrintingHashTable.Print(testHashtable);

            Console.WriteLine(testHashtable.ContainsKey(8));
            Console.WriteLine(new string('-', 50));

            Console.WriteLine(testHashtable.Find(6));
            Console.WriteLine(new string('-', 50));

            testHashtable.Remove(8);
            PrintingHashTable.Print(testHashtable);

            testHashtable.Clear();
            PrintingHashTable.Print(testHashtable);
        }
        public void Enumerate_Values_Populated()
        {
            HashTable<int, string> table = new HashTable<int, string>();

            List<string> values = new List<string>();
            for (int i = 0; i < 100; i++)
            {
                int value = _rng.Next();
                while (table.ContainsKey(value))
                {
                    value = _rng.Next();
                }

                values.Add(value.ToString());
                table.Add(value, value.ToString());
            }

            foreach (string value in table.Values)
            {
                Assert.IsTrue(values.Remove(value), "The key was missing from the values collection");
            }

            Assert.AreEqual(0, values.Count, "There were left over values in the value collection");
        }