JulianJulianov

08.RegularExpressionsExercise-*Nether Realms

May 10th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.09 KB | None | 0 0
  1. 05. *Nether Realms
  2. Mighty battle is coming. In the stormy nether realms, demons are fighting against each other for supremacy in a duel from which only one will survive.
  3. Your job, however is not so exciting. You are assigned to sign in all the participants in the nether realm's mighty battle's demon book, which of course is sorted alphabetically.
  4. A demon's name contains his health and his damage.
  5. The sum of the asci codes of all characters (excluding numbers (0-9), arithmetic symbols ('+', '-', '*', '/') and delimiter dot ('.')) gives a demon's total health.
  6. The sum of all numbers in his name forms his base damage. Note that you should consider the plus '+' and minus '-' signs (e.g. +10 is 10 and -10 is -10). However, there are some symbols ('*' and '/') that can further alter the base damage by multiplying or dividing it by 2 (e.g. in the name "m15*/c-5.0", the base damage is 15 + (-5.0) = 10 and then you need to multiply it by 2 (e.g. 10 * 2 = 20) and then divide it by 2 (e.g. 20 / 2 = 10)).
  7. So, multiplication and division are applied only after all numbers are included in the calculation and in the order they appear in the name.
  8. You will get all demons on a single line, separated by commas and zero or more blank spaces. Sort them in alphabetical order and print their names along their health and damage.
  9. Input
  10. The input will be read from the console. The input consists of a single line containing all demon names separated by commas and zero or more spaces in the format: "{demon name}, {demon name}, … {demon name}"
  11. Output
  12. Print all demons sorted by their name in ascending order, each on a separate line in the format:
  13. "{demon name} - {health points} health, {damage points} damage"
  14. Constraints
  15. • A demon's name will contain at least one character
  16. • A demon's name cannot contain blank spaces ' ' or commas ','
  17. • A floating point number will always have digits before and after its decimal separator
  18. • Number in a demon's name is considered everything that is a valid integer or floating point number (with dot '.' used as separator). For example, all these are valid numbers: '4', '+4', '-4', '3.5', '+3.5', '-3.5'
  19. Examples
  20. Input                     Output                                            Comments
  21. M3*ph-0.5*s-0.5t0.0       M3ph-0.5s-0.5t0.0** - 524 health, 8.00 damage     M3ph-0.5s-0.5t0.0**:
  22.                                                                            Health = 'M' + 'p' + 'h' + 's' + 't' = 524 health.
  23.                                                                            Damage = (3 + (-0.5) + (-0.5) + 0.0) * 2 * 2 = 8 damage.
  24. Input                     Output                                            Comments
  25. M3ph1st0**, Azazel        Azazel - 615 health, 0.00 damage                  Azazel:
  26.                          M3ph1st0** - 524 health, 16.00 damage             Health - 'A' + 'z' + 'a' + 'z' + 'e' + 'l' = 615 health.
  27.                                                                             Damage - no digits = 0 damage.
  28.                                                                            M3ph1st0**:
  29.                                                                            Health - 'M' + 'p' + 'h' + 's' + 't' = 524 health.
  30.                                                                            Damage - (3 + 1 + 0) * 2 * 2 = 16 damage.
  31.  
  32. Gos/ho                    Gos/ho - 512 health, 0.00 damage 
  33.  
  34. using System;
  35. using System.Linq;
  36. using System.Collections.Generic;
  37. using System.Text.RegularExpressions;
  38.  
  39. public class Program
  40. {
  41.     public static void Main()
  42.     {                          //Друг начин Split(new char[]{ ',', ' '}, StringSplitOptions.RemoveEmptyEntries).ToArray();
  43.         var demonsData = Console.ReadLine().Split(',').Select(x => x.Trim())/*.OrderBy(x => x)*/.ToArray();
  44.                       //Така се премахват всички видове спейсове(\t,' ',\n)!//Ако не ползвам сортирани речници: OrderBy(x => x)
  45.        var listDemonsHealths = new SortedDictionary<string, int>();
  46.        var listDemonsDamages = new SortedDictionary<string, double>();
  47.  
  48.        for (int i = 0; i < demonsData.Length; i++)
  49.        {
  50.            var pattern1 = @"(?<allCharacters>[^\s+\-*\/.\d])|(?<Digits>(\+|\-)?\d+(\.\d+)?)";
  51.            var matchDemonsData = Regex.Matches(demonsData[i], pattern1);
  52.  
  53.            var health = 0;
  54.            var damage = 0.0;
  55.              //Вариант ако не разчитам на регулярните изрази!
  56.              //health = demonsData[i].Where(x => !char.IsDigit(x)&& x != '+'&& x != '-'&& x != '*'&& x != '/'&& x != '.').Sum(x => x);
  57.            foreach (Match demon in matchDemonsData)
  58.            {                                                                                                
  59.                try
  60.                {
  61.                    health += demon.Groups["allCharacters"].Value[0];
  62.                }
  63.                catch
  64.                {
  65.                    damage += double.Parse(demon.Groups["Digits"].Value);
  66.                }
  67.            }
  68.            listDemonsHealths.Add(demonsData[i], health);
  69.  
  70.            if (damage == 0)
  71.            {
  72.                listDemonsDamages.Add(demonsData[i], damage);
  73.                continue;
  74.            }
  75.            else
  76.            {
  77.                var pattern2 = @"(?<MultiplyOrDivide>[*]|[\/])";
  78.                var matchMultiplyOrDivide = Regex.Matches(demonsData[i], pattern2);
  79.  
  80.                foreach (Match item in matchMultiplyOrDivide)
  81.                {
  82.                    var multiplyOrDivide = item.Value;
  83.  
  84.                    if (multiplyOrDivide == "*")
  85.                    {
  86.                        damage *= 2;
  87.                    }
  88.                    else
  89.                    {
  90.                        damage /= 2;
  91.                    }
  92.                }
  93.                listDemonsDamages.Add(demonsData[i], damage);
  94.            }
  95.        }
  96.        foreach (var item in listDemonsHealths)
  97.        {
  98.            Console.WriteLine($"{item.Key} - {item.Value} health, {listDemonsDamages[item.Key]:F2} damage");
  99.        }
  100.     }
  101. }
Add Comment
Please, Sign In to add comment