04. Star Enigma*(80/100) Regular Expressions - Exercise
Първите два теста за жалост се чупят. Моля, ако някой има възможност нека да погледне втория regex. Първият си работи, както се и очаква. Вече няколко часа не успявам да открия къде е проблема.
https://softuni.bg/trainings/resources/officedocument/49595/regular-expressions-exercise-csharp-fundamentals-may-2020/2830
https://judge.softuni.bg/Contests/Compete/Index/1668#3
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
namespace text
{
class MainClass
{
public static void Main(string[] args)
{
var regexForKey = new Regex(@"[starSTAR]");
var regexForDecrypt = new Regex(@"[^@!:>-]*@(?<planet>[A-za-z]+)[^@!:>-]*:(?<population>[0-9]+)[^@!:>-]*!(?<attack>[AD])![^@!:>-]*->(?<soldierCount>[0-9]+)[^@!:>-]*");
var listAttackedPlanets = new List<string>();
var listDestroyedPlanets = new List<string>();
int linesNumber = int.Parse(Console.ReadLine());
for (int i = 0; i < linesNumber; i++)
{
string message = Console.ReadLine();
var matches = regexForKey.Matches(message);
if (regexForKey.IsMatch(message))
{
int key = matches.Count;
string decryptedMessage = string.Empty;
foreach (char symbol in message)
{
int decryptedSymbolInt = symbol - key;
char decryptedSymbol = (char)decryptedSymbolInt;
decryptedMessage += decryptedSymbol;
}
var newMatches = regexForDecrypt.Matches(decryptedMessage);
if (regexForDecrypt.IsMatch(decryptedMessage))
{
foreach (Match newmatch in newMatches)
{
string planet = newmatch.Groups["planet"].Value;
string attack = newmatch.Groups["attack"].Value;
if (attack == "A")
{
listAttackedPlanets.Add(planet);
}
else
{
listDestroyedPlanets.Add(planet);
}
}
}
}
}
Console.WriteLine($"Attacked planets: {listAttackedPlanets.Count()}");
foreach (var planet in listAttackedPlanets.OrderBy(x=>x))
{
Console.WriteLine($"-> {planet}");
}
Console.WriteLine($"Destroyed planets: {listDestroyedPlanets.Count()}");
foreach (var planet in listDestroyedPlanets.OrderBy(x=>x))
{
Console.WriteLine($"-> {planet}");
}
}
}
}
Axiomatik, thanks a lot!
With your regex my result in Judge is also 80/100. :(
With my new regex with some corrections in the begining and in the end (@"@(?<planet>[A-za-z]+)[^@!:>-]*:(?<population>[0-9]+)[^@!:>-]*!(?<attack>[A|D])![^@!:>-]*->(?<soldierCount>[0-9]+)") the result is also 80/100.
From my understanding [AD] and [A|D] are doing the same and I don't need "\" to escape the "-", because I placed "-" at the end.
Iam very confused and worried with these regular expressions.
The last three exercises (Star Enigma, Nether Realms and Extract Emails) are a bit tricky and I also had difficulties with them. More importantly, start training with the older exams that are available and watch exam preparation videos to see how they are being solved. Unfortunately, C# Advanced won't be easier and you just have to keep up with the new material.
Best,
At last... thanks to you I have 100/100. :) https://pastebin.com/NsvW6D7c
1) Judge is serching for "[A|D]" and it doesn't matter that [AD] and [A|D] are doing the same.
2) Judge dislikes this check up " if (regexForKey.IsMatch(message))
{
int key = matches.Count;
string decryptedMessage = string.Empty;
foreach (char symbol in message)
{
int decryptedSymbolInt = symbol - key;
char decryptedSymbol = (char)decryptedSymbolInt;
decryptedMessage += decryptedSymbol;
} "
Should be whitout if condition:
var matches = regexForKey.Matches(message);
int key = matches.Count;
string decryptedMessage = string.Empty;
foreach (char symbol in message)
{
int decryptedSymbolInt = symbol - key;
char decryptedSymbol = (char)decryptedSymbolInt;
decryptedMessage += decryptedSymbol;
}
For second if\else condition and the both are valid: "
MatchCollection newMatches = regexForDecrypt.Matches(decryptedMessage); with forech loop", like in my code.
"Match newMatch=regexForDecrypt.Match(decryptedMessage); whitout forech loop", like in your code.
I think your way is better than mine. If I don't need I collection it haven't reason to used it and to used forech loop for every match. :)
And thanks a lot for your advice. By the way I plan to spend 6 months with C# Advanced, 2 months with High Quality Code and design patterns : https://softuni.bg/trainings/2706/high-quality-code-december-2019 , 3 months with Data Structure (fundamentals and Advanced) and 3 months with Algorithms (fundamentals and Advanced), before I go to DataBases and Web. After that I will start with HTML, CSS , JavaScript and Agile course... and do my best to not give up.
All the best!
Eli