//You are given a sequence of strings, each on a new line. Every odd line on the console is representing a resource (e.g. Gold, Silver, //Copper, and so on), and every even – quantity. Your task is to collect the resources and print them each on a new line. //Print the resources and their quantities in format: //{resource} –> {quantity} //The quantities inputs will be in the range[1 … 2 000 000 000] static void Main(string[] args) { SortedDictionery <string, int> mining = new SortedDictionery <string, int>(); int counter = 3; string toAdd = ""; int toAddInt = 0; while (true) { string value = Console.ReadLine(); if (value == "stop") { break; } if ((counter % 2 == 0) == false) { toAdd = value; counter++; } else { toAddInt = int.Parse(value); if (mining.ContainsKey(toAdd) == false) { mining.Add(toAdd, toAddInt); } else { mining[toAdd] = mining[toAdd] + toAddInt; } toAdd = ""; toAddInt = 0; counter++; } } foreach (var item in mining) { Console.WriteLine($"{item.Key} -> {item.Value}"); } }
//You are given a sequence of strings, each on a new line, until you receive the “stop” command. The first string is the name of a person. //On the second line, you will receive their email. Your task is to collect their names and emails, and remove emails whose domain ends with //"us" or "uk" (case insensitive). Print: //{name} – > {email} static void Main(string[] args) { SortedDictionery <string, string> emailBook = new SortedDictionery <string, string>(); int counter = 3; string toAddName = ""; string toAddMail = ""; while (true) { string value = Console.ReadLine(); if (value == "stop") { break; } if ((counter % 2 == 0) == false) { toAddName = value; counter++; } else { toAddMail = value; string toCheck = (toAddMail[toAddMail.Length - 2]) + toAddMail[toAddMail.Length - 1].ToString(); if (toCheck != "us" && toCheck != "uk") { if (emailBook.ContainsKey(toAddName) == false) { emailBook.Add(toAddName, toAddMail); } } counter++; toAddMail = ""; toAddName = ""; } } foreach (var item in emailBook) { Console.WriteLine($"{item.Key} -> {item.Value}"); } }
//You are given a sequence of people and for every person what cards he draws from the deck. The input will be separate lines in the format: //• {personName}: {PT, PT, PT,… PT} //Where P(2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A) is the power of the card and T(S, H, D, C) is the type.The input ends when a "JOKER" is //drawn.The name can contain any ASCII symbol except ':'. The input will always be valid and in the format described, there is no need to //check it. //A single person cannot have more than one card with the same power and type, if they draw such a card they discard it. The people are //playing with multiple decks.Each card has a value that is calculated by the power multiplied by the type. Powers 2 to 10 have the same //value and J to A are 11 to 14. Types are mapped to multipliers the following way (S -> 4, H-> 3, D -> 2, C -> 1). //Finally print out the total value each player has in his hand in the format: //• {personName}: {value} static void Main(string[] args) { SortedDictionery <string, List <string> > hands = new SortedDictionery <string, List <string> >(); while (true) { string[] value = Console.ReadLine().Split(new char[] { ':', ',' }, StringSplitOptions.RemoveEmptyEntries); if (value[0] == "JOKER") { break; } string player = value[0]; List <string> cards = new List <string>(); for (int i = 1; i < value.Length; i++) { cards.Add(value[i]); } if (hands.ContainsKey(player) == false) { hands.Add(player, cards); } else { hands[player].AddRange(cards); } } foreach (var item in hands) { string person = item.Key; //тук в момента стои един от тримата играчи List <string> personHands = item.Value.Distinct().ToList(); int result = Checker(personHands); Console.WriteLine($"{person}: {result}"); } }
//Write a program that receives some info from the console about people and their phone numbers. Each entry should have just one name and //one number (both of them strings). //On each line, you will receive some of the following commands: //• A {name // } {phone //} – adds entry to the phonebook.In case of trying to add a name that is already in the phonebook you should change the existing phone number with the new one provided. //• S { name} – searches for a contact by given name and prints it in format "{name} -> {number}". In case the contact isn't found, print "Contact {name} does not exist.". //• END – stop receiving more commands. static void Main(string[] args) { SortedDictionery <string, string> phonebook = new SortedDictionery <string, string>(); List <string> value = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToList(); while (value[0] != "END") { string command = value[0]; if (command == "A") { string name = value[1]; string number = value[2]; if (phonebook.ContainsKey(name) == false) { phonebook.Add(name, ""); } phonebook[name] = number; } else if (command == "S") { string name = value[1]; if (phonebook.ContainsKey(name)) { phonebook.TryGetValue(name, out string dicValue).ToString(); Console.WriteLine($"{name} -> {dicValue}"); } else { Console.WriteLine($"Contact {name} does not exist."); } } value.Clear(); value = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToList(); } }
//So many people! It’s hard to count them all. But that’s your job as a statistician. You get raw data for a given city and you need to //aggregate it. //On each input line, you’ll be given data in format: "city|country|population". There will be no redundant whitespaces anywhere in the //input.Aggregate the data by country and by city and print it on the console. //For each country, print its total population and on separate lines, the data for each of its cities. Countries should be ordered by their //total population in descending order and within each country, the cities should be ordered by the same criterion. //If two countries/cities have the same population, keep them in the order in which they were entered. Check out the examples; follow the //output format strictly! //Input //• The input data should be read from the console. //• It consists of a variable number of lines and ends when the command "report" is received. //• The input data will always be valid and in the format described.There is no need to check it explicitly. //Output //• The output should be printed on the console. //• Print the aggregated data for each country and city in the format shown below. static void Main(string[] args) { SortedDictionery <string, SortedDictionery <string, long> > answer = new SortedDictionery <string, SortedDictionery <string, long> >(); while (true) { List <string> value = Console.ReadLine().Split("|", StringSplitOptions.RemoveEmptyEntries).ToList(); if (value[0] == "report") { break; } string country = value[1]; string city = value[0]; long population = long.Parse(value[2]); if (answer.ContainsKey(country) == false) { //ако първият dict не съъдържа country добавяме и country и city и population answer.Add(country, new SortedDictionery <string, long>()); answer[country].Add(city, population); } else { if (answer[country].ContainsKey(city) == false) { answer[country].Add(city, population); } else { answer[country][city] += population; } } } SortedDictionery <string, long> countryPopulation = new SortedDictionery <string, long>(); SortedDictionery <string, SortedDictionery <string, long> > sorted = new SortedDictionery <string, SortedDictionery <string, long> >(); SortedDictionery <string, long> citySorted = new SortedDictionery <string, long>(); foreach (var item in answer) { string theCountry = item.Key; countryPopulation.Add(theCountry, 0); foreach (var token in item.Value) { string cityToAdd = token.Key; long totalPopulation = token.Value; countryPopulation[theCountry] += totalPopulation; citySorted[cityToAdd] = totalPopulation; } } SortedDictionary <string, long> result = countryPopulation.OrderByDescending(x => x.Value).ToSortedDictionery(pair => pair.Key, pair => pair.Value); foreach (var item in result) { string res1 = item.Key; long rest2 = item.Value; Console.WriteLine($"{res1} (total population: {rest2})"); foreach (var token in answer) { foreach (var t in token.Value.OrderByDescending(x => x.Value)) { if (token.Key == res1) { Console.WriteLine($"=>{t.Key}: {t.Value}"); } } } } }