Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ConsoleApp
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class Program
- {
- public static void Main()
- {
- SortedDictionary<string, SortedDictionary<string, int>> dic = new SortedDictionary<string, SortedDictionary<string, int>>();
- bool isNset = false;
- int n = 0;
- int count = 0;
- while (true)
- {
- string log = Console.ReadLine();
- if (!isNset)
- {
- isNset = true;
- if (IsValidDurationOrCount(log))
- {
- n = int.Parse(log);
- }
- else
- {
- Console.WriteLine("Invalid count number");
- isNset = false;
- }
- }
- else
- {
- var data = log.Split(' ');
- if (!AnyNonLatinLetter(data[1]) && IsValidIpAddress(data[0]) && IsValidDurationOrCount(data[2]))
- {
- if (dic.ContainsKey(data[1]))
- {
- SortedDictionary<string, int> val = dic[data[1]];
- if (!val.ContainsKey(data[0]))
- {
- var srL = dic[data[1]];
- srL.Add(data[0], int.Parse(data[2]));
- dic[data[1]] = srL;
- }
- else
- {
- int newDuration = val[data[0]] + int.Parse(data[2]);
- val[data[0]] = newDuration;
- dic[data[1]] = val;
- }
- }
- else
- {
- dic.Add(data[1], new SortedDictionary<string, int>() { { data[0], int.Parse(data[2]) } });
- }
- }
- count++;
- if (count == n)
- {
- foreach (var elements in dic)
- {
- int duration = 0;
- string addresses = null;
- var firstVal = elements.Value.First();
- foreach (var el in elements.Value)
- {
- duration += el.Value;
- if (firstVal.Key == el.Key && firstVal.Value == el.Value)
- {
- addresses += el.Key;
- }
- addresses += ", " + el.Key;
- }
- Console.WriteLine(elements.Key + ": " + duration.ToString() + " [" + addresses + "]");
- }
- }
- }
- }
- }
- private static bool IsValidDurationOrCount(string v)
- {
- int number = int.Parse(v);
- return number >= 1 && number <= 1000;
- }
- public static bool AnyNonLatinLetter(string name)
- {
- foreach (var item in name)
- {
- bool flag = IsLatinLetter(item);
- if (!flag)
- {
- return true;
- }
- }
- //is length between 1 and 20;
- if (!(name.Length >= 1 && name.Length <= 20))
- {
- return true;
- }
- return false;
- }
- public static bool IsLatinLetter(char c)
- {
- return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
- }
- private static bool IsValidIpAddress(string IP)
- {
- var abcd = IP.Split('.');
- if (abcd.Count() == 4)
- {
- foreach (var item in abcd)
- {
- int number = int.Parse(item);
- if (!(number >= 0 && number <= 255))
- {
- return false;
- }
- }
- return true;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement