private static string ProceessSearchQueries(Dictionary<string, string> phonebook) { var contactName = Console.ReadLine(); var queriesResult = new StringBuilder(); while (!string.IsNullOrEmpty(contactName)) { var contact = phonebook.Find(contactName); queriesResult.AppendLine(contact != null ? string.Format("{0} -> {1}", contact.Key, contact.Value) : string.Format("Contact {0} does not exist.", contactName)); contactName = Console.ReadLine(); } return queriesResult.ToString(); }
static void Main(string[] args) { Dictionary<String, Product> Catalog = new Dictionary<String, Product>(); Console.WriteLine("How many Products do you want to Catalog?"); int count; bool parse = Int32.TryParse(Console.ReadLine(), out count); while (!parse || count <= 0) { Console.WriteLine("Enter a valid input"); parse = Int32.TryParse(Console.ReadLine(), out count); } for (int i = 0; i < count;i++ ) { Console.WriteLine("Enter the Product Description :"); String descr = Console.ReadLine(); Console.WriteLine("Enter the Product Cost :"); string Cost = Console.ReadLine(); Console.WriteLine("Enter the Product Manufacturer:"); String manufacture = Console.ReadLine(); Console.WriteLine("Enter the Product Key:"); String key = Console.ReadLine(); Catalog.Add(key,new Product(descr, Cost, manufacture)); } Console.WriteLine("What Product-key you want to lookup?"); string response=Console.ReadLine (); Catalog[response].Print() ; }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (e.Argument == null) { e.Cancel = true; return; } if (words != null) { //обнулить слова foreach (var item in words) { item.ClearStr(); } //слова из базы по длине Dictionary<int, List<string>> listWords = new Dictionary<int, List<string>>(); for (int i = 0; i < words.Count; i++) { if (!listWords.ContainsKey(words[i].Length)) { listWords.Add(words[i].Length, WordsCountKey(words[i].Length)); } } lock (treeWord=new TreeCross(words)) { treeWord.listWords = listWords; treeWord.SetCrossWords(); words = treeWord.GetWords(); } } e.Result = words; }
public MainForm() { InitializeComponent(); dictionary = new Dictionary<string, string>(); FileStream file=null; StreamReader streamReader = null; try { file = new FileStream(Application.StartupPath + "\\OZHEGOV.TXT", FileMode.Open); streamReader = new StreamReader(file, ASCIIEncoding.Default); } catch (Exception) { MessageBox.Show("База ненайдена"); Application.ExitThread(); this.Close(); } string lineDictionary = string.Empty; dictionary = new Dictionary<string, string>(); while ((lineDictionary = streamReader.ReadLine()) != null) { string[] gg = Regex.Split(lineDictionary, @"\|\|\|"); dictionary.Add(gg[0], gg[1]); } streamReader.Close(); }
public void ShouldReturnCounterDifferentThan0() { var d = new Dictionary<string, int>(); d.Add("cats", 12); d.Add("dogs", 133); Assert.Equal(2, d.Count); }
public void ReturnsTrueForRemovingItem() { var data = new Dictionary<string, int> { { "first", 2 }, { "second", 3 } }; bool result = data.Remove("first"); Assert.True(result); Assert.Equal(2, data.Count); }
public void RemovingWorksIfConflictSituationsArise() { Dictionary<string, int> d = new Dictionary<string, int> { { "cats", 1 }, { "dogs", 1 }, { "mice", 1 } }; d.Remove("cats"); Assert.False(d.ContainsKey("cats")); Assert.True(d.ContainsKey("dogs")); Assert.Equal(2, d.Count); }
public void IsTrueIfRemovingAnItemWorks() { Dictionary<string, int> d = new Dictionary<string, int> { { "cats", 12 }, { "dogs", 13 } , { "mice", 15} }; d.Remove("cats"); Assert.False(d.ContainsKey("cats")); Assert.True(d.ContainsKey("dogs")); Assert.Equal(2, d.Count); }
public void ShouldReturnCounterNullForClear() { var d = new Dictionary<string, int>(); d.Add("cats", 12); d.Add("dogs", 133); d.Clear(); Assert.Equal(0, d.Count); }
public void ChecksIfAddFunctionWorksAfterRemove() { var data = new Dictionary<string, int> { { "first", 2 }, { "second", 3 } }; data.Remove("first"); data.Add("third", 6); Assert.True(data.ContainsKey("third")); Assert.False(data.ContainsKey("first")); }
public static Dictionary<string, State> GetStates() { var states = new Dictionary<string, State>(); var theState = new State("Montgomery", 123456, 123); states.Add("Alabama", theState); theState = new State("Juneau", 345234, 3234); states.Add("Alaska", theState); return states; }
private static void PrintGraph(Dictionary<string, List<string>> graph) { foreach (var node in graph) { Console.WriteLine( "{0} -> {1}", node.Key, string.Join(", ", node.Value)); } }
public void AddWorksAfterRemove() { Dictionary<string, int> d = new Dictionary<string, int> { { "cats", 12 }, { "dogs", 13 }, { "mice", 15 } }; d.Remove("cats"); d.Add("bunnies", 19); Assert.False(d.ContainsKey("cats")); Assert.True(d.ContainsKey("dogs")); Assert.True(d.ContainsKey("bunnies")); Assert.Equal(3, d.Count); }
public static void Main() { var phonebook = new Dictionary<string, string>(); ParseContactsData(phonebook); var queriesResult = ProceessSearchQueries(phonebook); Console.WriteLine(new string('-', 70)); Console.WriteLine(queriesResult); }
private static void ParseContactsData(Dictionary<string, string> phonebook) { var contactData = Console.ReadLine(); while (!string.IsNullOrEmpty(contactData) && contactData != "search") { var rowData = contactData.Split('-'); phonebook.AddOrReplace(rowData[0], rowData[1]); contactData = Console.ReadLine(); } }
// A dictionary is stored as a sequence of text lines //containing words and their explanations. //Write a program that enters a word and translates it by using the dictionary. Sample dictionary: // .NET – platform for applications from Microsoft //CLR – managed execution environment for .NET //namespace – hierarchical organization of classes static void Main(string[] args) { Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add(".NET", "platform for applications from Microsoft"); dict.Add("CLR", "managed execution environment for .NET"); dict.Add("namespace", "hierarchical organization of classes"); string word = Console.ReadLine(); if (dict.ContainsKey(word)) { Console.WriteLine(dict[word]); } }
static void Main(string[] args) { Dictionary<string, int> dic = new Dictionary<string, int>(); dic["りんご"] = 100; dic["みかん"] = 10; dic["やくそう"] = 3; Console.WriteLine(dic["りんご"]); Console.WriteLine(dic["みかん"]); Console.WriteLine(dic["やくそう"]); foreach (var key in dic.Keys) { Console.WriteLine("{0} : {1}", key, dic[key]); } }
static void Main(string[] args) { Dictionary<string, string> capitals = new Dictionary<string, string>(); capitals.Add("Alabama", "Montgomery"); capitals.Add("Alaska", "Juneau"); capitals.Add("Arizona", "Phoenix"); capitals.Add("Massachusetts", "Boston"); capitals.Add("Wyoming", "Cheyenne"); string capitalOfMass = capitals["Massachusetts"]; Console.WriteLine("The capital of Massachusetts is {0}", capitalOfMass); var theStates = State.GetStates(); var myState = theStates["Alaska"]; Console.WriteLine("the capital of Alaska is {0}, its population is {1}, and it is {2} square miles", myState.Captial, myState.Population, myState.Size ); }
/// <summary> /// Главная точка входа для приложения. /// </summary> static void Main() { Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("Троу [Throw]", "Сроу [θrəʊ]" ); dic.Add("Парсе [Parse]", "Парс [pɑːrs]"); dic.Add("Матч [Math]", "Мэс [mæθ]" ); dic.Add("Джет [Get]", "Гет [ɡet]" ); dic.Add("Трэй [Try]", "Трай [traɪ]"; dic.Add("Анчекид [Unchecked]", "Анчект [ʌnˈtʃekt]"); dic.Add("Имплой [Employee]", "Имплойи [ɪmˈplɔɪiː]"); foreach (KeyValuePair<string, string> kvp in dic) { MessageBox.Show(string.Format("Правильное название {0} это {1}", kvp.Key, kvp.Value)); } }
public static void Main() { var graph = new Dictionary<string, List<string>>(); string line = Console.ReadLine(); while (line != string.Empty) { string[] connection = line.Split(' '); string first = connection[0]; string second = connection[1]; if (!graph.ContainsKey(first)) { graph[first] = new List<string>(); } graph[first].Add(second); // Loop? if (first != second) { if (!graph.ContainsKey(second)) { graph[second] = new List<string>(); } graph[second].Add(first); } line = Console.ReadLine(); } foreach (var node in graph) { Console.Write(node.Key + " -> "); foreach (string neighbors in node.Value) { Console.Write(neighbors + " "); } Console.WriteLine(); } }
public static void Main(String[] args) { CheckDictionary completeDictionary = new CheckDictionary(); completeDictionary.LoadDictionary(); Dictionary<string, bool> inCorrectWords = new Dictionary<string, bool>(); Console.WriteLine("Enter full file path to test - Press enter to choose dummy.txt from debug/App_Data folder"); string filename = Console.ReadLine(); if (String.IsNullOrEmpty(filename)) { filename = ConfigurationSettings.AppSettings["FileToTest"]; } inCorrectWords = completeDictionary.GetInCorrectWords(filename); foreach (KeyValuePair<string, bool> s in inCorrectWords) { Console.WriteLine(s.Key); } Console.ReadKey(); }
static void Main(string[] args) { Dictionary<string, string> phonebook = new Dictionary<string, string>(); phonebook["John Smith"] = "+1 - 455-7888"; // key , value phonebook["Lisa Liska"] = "+1- 887-8999"; phonebook["Sam sam "] = "+1- 887-9877"; phonebook["Nakk"] = "+1-675-3422"; phonebook.Remove("John Smith"); foreach ( var entry in phonebook) { Console.WriteLine("{0} --> {1}",entry.Key,entry.Value); //entry.Key, entry.Value } Console.ReadLine(); }
public static void Main() { var chars = Console.ReadLine().ToCharArray(); var symbols = new Dictionary<char, int>(); foreach (var c in chars) { if (!symbols.ContainsKey(c)) { symbols[c] = 0; } symbols[c]++; } symbols.OrderBy(c => c.Key) .ToList() .ForEach(c => { Console.WriteLine("{0}: {1} time/s", c.Key, c.Value); }); }
public static void Main() { string input = Console.ReadLine(); Dictionary<char, int> charactersCount = new Dictionary<char, int>(); foreach (var character in input) { if (!charactersCount.ContainsKey(character)) { charactersCount[character] = 0; } charactersCount[character]++; } charactersCount .OrderBy(ch => ch.Key) .ToList() .ForEach(character => Console.WriteLine($"{character.Key}: {character.Value} time/s")); }
static void Main(string[] args) { var namesAndAges = new Dictionary<string, int>/*(StringComparer.CurrentCultureIgnoreCase)*/ { {"Jeremy", 35} }; namesAndAges.Add("Josh", 25); namesAndAges["Kristin"] = 39; //namesAndAges.Add("Jeremy", 30); // an exception namesAndAges["Jeremy"] = 30; if (namesAndAges.ContainsKey("Caleb")) { var foo = namesAndAges["Caleb"]; // } foreach (var kvp in namesAndAges) { //kvp.Key //kvp.Value } foreach (var key in namesAndAges.Keys) { } foreach (var value in namesAndAges.Values) { } var count = namesAndAges.Count; if (namesAndAges.ContainsKey("Josh")) { namesAndAges.Remove("Josh"); } namesAndAges.Clear(); Console.ReadLine(); }
static void Main() { Dictionary<string, List<string>> graph = new Dictionary<string, List<string>>(); string line = Console.ReadLine(); while (line != string.Empty) { string[] connection = line.Split(' '); string first = connection[0]; string second = connection[1]; if (!graph.ContainsKey(first)) { graph[first] = new List<string>(); } graph[first].Add(second); if (!graph.ContainsKey(second)) { graph[second] = new List<string>(); } graph[second].Add(first); line = Console.ReadLine(); } foreach (var node in graph) { Console.Write(node.Key + " -> "); for (int i = 0; i < node.Value.Count; i++) { Console.Write(node.Value[i] + " "); } Console.WriteLine(); } }
void Run() { Dictionary<int, int> myDict = new Dictionary<int, int>(); myDict.Add(0, 1); myDict.Add(1, 2); myDict.Add(2, 3); myDict.Add(3, 4); Console.WriteLine("Now it should print false"); Console.WriteLine(myDict.ContainsKey(4)); Console.WriteLine("Now it should print true"); Console.WriteLine(myDict.ContainsKey(3)); myDict.Remove(3); Console.WriteLine("Now it should print false"); Console.WriteLine(myDict.ContainsKey(3)); Console.WriteLine("Now it should print 2"); Console.WriteLine(myDict[1]); Dictionary<A, B> myDict2 = new Dictionary<A, B>(); A a1 = new A(); A a2 = new A(); myDict2.Add(a1, new B()); myDict2.Add(a2, new B()); Console.WriteLine("Now it should print false"); Console.WriteLine(myDict2.ContainsKey(new A())); Console.WriteLine("Now it should print true"); Console.WriteLine(myDict2.ContainsKey(a1)); myDict2.Remove(a1); Console.WriteLine("Now it should print false"); Console.WriteLine(myDict2.ContainsKey(a1)); Console.WriteLine("Now it should print \"Hello I'm B\""); myDict2[a2].f(); }
static void Main(string[] args) { Dictionary<char, string> certifications = new Dictionary<char, string>(); certifications.Add('P', "Passenger transport"); certifications.Add('H', "Hazardous materials"); certifications.Add('N', "Tank vehicles (liquids in bulk)"); certifications.Add('T', "Double/Triple trailers"); ShowCertifications(certifications); //search for certfications by key and confirm string theValue; //try get value //uses the out certifications.TryGetValue('H', out theValue); Console.WriteLine(theValue); if (certifications.ContainsKey('P')) Console.WriteLine("Has certfication 'P'"); //check certification for a specific value and confirm if (certifications.ContainsValue("Double/Triple trailers")) Console.WriteLine("Has certification 'T'"); //remove certification 'T' by key. certifications.Remove('T'); if (!certifications.ContainsValue("Double/Triple trailers")) Console.WriteLine("No longer has certification 'T'"); //remove all certifications certifications.Clear(); if (certifications.Count == 0) Console.WriteLine("All certifications removed"); Console.ReadLine(); }
static void Main(string[] args) { Dictionary<int, Person> dic = new Dictionary<int, Person>(); Dictionary<string, Person> dic2 = new Dictionary<string, Person>(); Person p1 = new Person("Frey"); Person p2 = new Person("Leon"); dic.Add(p1.PersonId, p1); dic.Add(p2.PersonId, p2); foreach (var item in dic) { Console.WriteLine(item.Key + " " + item.Value.Name); } dic2.Add("Frey", new Person()); dic2.Add("Leon", new Person()); foreach (var item in dic2) { Console.WriteLine(item.Key + " " + item.Value.PersonId); } if (dic2.ContainsKey("Leon")) { Console.WriteLine("Key Leon Exists"); } Console.WriteLine("Delete Dictionary"); dic.Clear(); Console.WriteLine(); Console.WriteLine(""); foreach (var item in dic) { Console.WriteLine(item.Key + " " + item.Value.Name); } }
public static void ShowCertifications(Dictionary<char,string> certs) { foreach (KeyValuePair<char, string> cert in certs) { Console.WriteLine("Key: " + cert.Key + " Value: " + cert.Value); Console.WriteLine(); } }