Exemple #1
0
    static void Main()
    {
        HashTable <string, int> 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("--------------------");
    }
Exemple #2
0
        public void Count_Empty_Add_Remove_ShouldWorkCorrectly()
        {
            // Arrange
            HashTable <string, int> hashTable = new HashTable <string, int>();

            // Assert
            Assert.AreEqual(0, hashTable.Count);

            // Act & Assert
            hashTable.Add("Peter", 555);
            hashTable.AddOrReplace("Peter", 555);
            hashTable.AddOrReplace("Ivan", 555);
            Assert.AreEqual(2, hashTable.Count);

            // Act & Assert
            hashTable.Remove("Peter");
            Assert.AreEqual(1, hashTable.Count);

            // Act & Assert
            hashTable.Remove("Peter");
            Assert.AreEqual(1, hashTable.Count);

            // Act & Assert
            hashTable.Remove("Ivan");
            Assert.AreEqual(0, hashTable.Count);
        }
    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("--------------------");
    }
        static void Main()
        {
            string input = Console.ReadLine();
            var phonebook = new HashTable<string, string>();

            while (input != "search")
            {
                string[] record = input.Split( new char[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
                phonebook.AddOrReplace(record[0], record[1]);
                input = Console.ReadLine();
            }
            
            input = Console.ReadLine();
            
            while (input != "")
            {
                if (phonebook.ContainsKey(input))
                {
                    Console.WriteLine("{0} -> {1}", input, phonebook[input]);
                }
                else
                {
                    Console.WriteLine("Contact {0} does not exist.", input);
                }

                input = Console.ReadLine();
            }
        }
Exemple #5
0
        public static void Main(string[] args)
        {
            var phonebook = new HashTable <string, string>();

            string line = Console.ReadLine();

            while (line != "search")
            {
                string[] parameters = line.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                string   name       = parameters[0];
                string   phone      = parameters[1];

                phonebook.AddOrReplace(name, phone);
                line = Console.ReadLine();
            }

            line = Console.ReadLine();
            while (!string.IsNullOrWhiteSpace(line))
            {
                if (phonebook.ContainsKey(line))
                {
                    Console.WriteLine("{0} -> {1}", line, phonebook[line]);
                }
                else
                {
                    Console.WriteLine("Contact {0} does not exist.", line);
                }
                line = Console.ReadLine();
            }
        }
Exemple #6
0
    private static void Phonebook()
    {
        var text      = Console.ReadLine();
        var phonebook = new HashTable <string, string>();

        while (text != "search")
        {
            var info   = text.Split('-');
            var name   = info[0];
            var number = info[1];

            phonebook.AddOrReplace(name, number);

            text = Console.ReadLine();
        }

        var searchedName = Console.ReadLine();

        while (!string.IsNullOrEmpty(searchedName))
        {
            if (phonebook.ContainsKey(searchedName))
            {
                Console.WriteLine($"{searchedName} -> {phonebook[searchedName]}");
            }
            else
            {
                Console.WriteLine($"Contact {searchedName} does not exist.");
            }

            searchedName = Console.ReadLine();
        }
    }
Exemple #7
0
    static void Main()
    {
        // Arrange
        var hashTable = new HashTable <DateTime, string>();
        var date      = new DateTime(1995, 7, 18);

        hashTable.Add(date, "Some value");

        // Act
        var containsKey = hashTable.ContainsKey(date);

        ///////////////////////////////////////////////////
        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("--------------------");
    }
    static void Main()
    {
        // Arrange
        var hashTable = new HashTable<DateTime, string>();
        var date = new DateTime(1995, 7, 18);
        hashTable.Add(date, "Some value");

        // Act
        var containsKey = hashTable.ContainsKey(date);

        ///////////////////////////////////////////////////
        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("--------------------");
    }
    public void AddOrReplace_WithDuplicates_ShouldWorkCorrectly()
    {
        // Arrange
        var hashTable = new HashTable<string, int>();

        // Act
        hashTable.AddOrReplace("Peter", 555);
        hashTable.AddOrReplace("Maria", 999);
        hashTable.AddOrReplace("Maria", 123);
        hashTable.AddOrReplace("Maria", 6);
        hashTable.AddOrReplace("Peter", 5);

        // Assert
        var expectedElements = new KeyValue<string, int>[]
        {
            new KeyValue<string, int>("Peter", 5),
            new KeyValue<string, int>("Maria", 6)
        };
        var actualElements = hashTable.ToList();
        CollectionAssert.AreEquivalent(expectedElements, actualElements);
    }
Exemple #10
0
        public void AddOrReplace_WithDuplicates_ShouldWorkCorrectly()
        {
            // Arrange
            HashTable <string, int> hashTable = new HashTable <string, int>();

            // Act
            hashTable.AddOrReplace("Peter", 555);
            hashTable.AddOrReplace("Maria", 999);
            hashTable.AddOrReplace("Maria", 123);
            hashTable.AddOrReplace("Maria", 6);
            hashTable.AddOrReplace("Peter", 5);

            // Assert
            KeyValue <string, int>[] expectedElements =
            {
                new KeyValue <string, int>("Peter", 5),
                new KeyValue <string, int>("Maria", 6)
            };
            List <KeyValue <string, int> > actualElements = hashTable.ToList();

            CollectionAssert.AreEquivalent(expectedElements, actualElements);
        }
Exemple #11
0
    public static void Main()
    {
        HashTable <string, string> phonebook = new HashTable <string, string>();
        Queue <string>             output    = new Queue <string>();

        string input = Console.ReadLine();

        while (input != "search")
        {
            string name        = input.Split('-')[0];
            string phoneNumber = input.Split('-')[1];

            phonebook.AddOrReplace(name, phoneNumber);
            input = Console.ReadLine();
        }

        input = Console.ReadLine();

        while (input != string.Empty)
        {
            KeyValue <string, string> contact = phonebook.Find(input);

            if (contact == null)
            {
                output.Enqueue(string.Format("Contact {0} does not exist.", input));
            }
            else
            {
                output.Enqueue(string.Format("{0} -> {1}", contact.Key, contact.Value));
            }

            input = Console.ReadLine();
        }

        foreach (string result in output)
        {
            Console.WriteLine(result);
        }
    }
    public void Count_Empty_Add_Remove_ShouldWorkCorrectly()
    {
        // Arrange
        var hashTable = new HashTable<string, int>();

        // Assert
        Assert.AreEqual(0, hashTable.Count);

        // Act & Assert
        hashTable.Add("Peter", 555);
        hashTable.AddOrReplace("Peter", 555);
        hashTable.AddOrReplace("Ivan", 555);
        Assert.AreEqual(2, hashTable.Count);

        // Act & Assert
        hashTable.Remove("Peter");
        Assert.AreEqual(1, hashTable.Count);

        // Act & Assert
        hashTable.Remove("Peter");
        Assert.AreEqual(1, hashTable.Count);

        // Act & Assert
        hashTable.Remove("Ivan");
        Assert.AreEqual(0, hashTable.Count);
    }
Exemple #13
0
 public bool AddOrReplace(TKey key, TValue value)
 {
     return(table.AddOrReplace(key, value));
 }