static void Main(string[] args) { List <Demon> demon = new List <Demon>(); string numberPattern = @"[-+]?[0-9]+\.?[0-9]*"; Regex numberReg = new Regex(numberPattern); string[] demons = Console.ReadLine().Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (var demone in demons) { string filter = "0123456789+-*/."; int health = demone.Where(x => !filter.Contains(x)).Sum(x => x); double damage = CalculateDamage(numberReg, demone); Demon currentDemon = new Demon(demone, health, damage); demon.Add(currentDemon); } demon = demon.OrderBy(x => x.Name).ToList(); foreach (var item in demon) { Console.WriteLine(item); } }
static void Main(string[] args) { string patternName = @"[A-z]"; string patternDigits = @"[-+]?[0-9]+\.?[0-9]*"; Regex regex = new Regex(patternName); Regex regexDmg = new Regex(patternDigits); string[] demon = Console.ReadLine().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); List<Demon> demons = new List<Demon>(); for (int i = 0; i < demon.Length; i++) { string currentDemont = demon[i]; string filter = "0123456789/*+-."; int healt = currentDemont.Where(c => !filter.Contains(c)).Sum(c => c); double demage = CalculateDemge(regexDmg, currentDemont); Demon newDemon = new Demon(currentDemont, healt, demage); demons.Add(newDemon); } foreach (var sortedDemons in demons.OrderBy(x=>x.DemonName)) { Console.WriteLine(sortedDemons); } }
public static void Main() { var names = Console.ReadLine() .Split(',') .Select(x => x.Trim()) .ToArray(); var demons = new Dictionary <string, Demon>(); for (int i = 0; i < names.Length; i++) { var name = names[i]; var health = GetDemonHealth(name); var damage = GetDemonDamage(name); var demon = new Demon() { Health = health, Damage = damage }; demons.Add(name, demon); } foreach (var demon in demons.OrderBy(x => x.Key)) { Console.WriteLine("{0} - {1} health, {2:F2} damage", demon.Key, demon.Value.Health, demon.Value.Damage); } }
static void Main(string[] args) { string n = Console.ReadLine(); Regex name = new Regex(@"[a-zA-z0-9-\+\.\*\/]+"); MatchCollection names = name.Matches(n); List <string> input = new List <string>(); foreach (var i in names) { input.Add(i.ToString()); } Regex healthPat = new Regex(@"[^0-9\+\-\*\/\.]"); Regex dmgPat = new Regex(@"\+?\-?[\d+\.?\d?]+"); List <Demon> demons = new List <Demon>(); for (int i = 0; i < input.Count; i++) { int health = Health(healthPat, input[i]); double damage = Damage(dmgPat, input[i]); Demon demon = demons.FirstOrDefault(d => d.Name == input[i]); if (demon == null) { demons.Add(new Demon(input[i], health, damage)); } else { demon.Health = health; demon.Damage = damage; } } foreach (var item in demons.OrderBy(d => d.Name)) { Console.WriteLine($"{item.Name} - {item.Health} health, {item.Damage:f2} damage"); } }
static void Main(string[] args) { string[] names = Console.ReadLine().Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries); List <Demon> output = new List <Demon>(); foreach (var name in names) { if (name == "") { continue; } //string symbolPattern = @"[^0-9+\-*/\.]"; string symbolPattern = @"[^0-9\+\-\*\/\.]"; string digitPattern = @"([0-9]+\.[0-9]+)|[0-9]+|-([0-9]+\.[0-9]+)"; //string digitPattern = @"-?\d\.?\d*"; string operationPattern = @"[/*]"; var symbolMatches = Regex.Matches(name, symbolPattern); var digitMatches = Regex.Matches(name, digitPattern); var operationMatches = Regex.Matches(name, operationPattern); decimal totalHealth = 0; foreach (Match item in symbolMatches) { totalHealth += Convert.ToChar(item.Value); } decimal damage = 0; foreach (Match item in digitMatches) { damage += decimal.Parse(item.Value); } foreach (Match item in operationMatches) { if (item.Value == "*") { damage *= 2.0M; } else if (item.Value == "/") { damage /= 2.0M; } } Demon demon = new Demon(); demon.Name = name; demon.Healt = totalHealth; demon.Damage = damage; if (!output.Any(x => x.Name == name)) { output.Add(demon); } } foreach (var item in output.OrderBy(x => x.Name)) { Console.WriteLine($"{item.Name} - {item.Healt} health, {item.Damage:f2} damage"); } }