diff --git a/EM-Qual/QualData.cs b/EM-Qual/QualData.cs index 9aad1df..d87ed51 100644 --- a/EM-Qual/QualData.cs +++ b/EM-Qual/QualData.cs @@ -38,8 +38,24 @@ public static class QualData return Array.Empty(); } - //using lists to avoid resizing the array (Programmers are lazy) - var matches = new List(); + int index = 0; + Match[] matches = new Match[lines.Length]; + foreach(var line in lines) + { + if (TryParseMatch(line, out Match? match)) + { + matches[index++] = match!; + } + else + { + return Array.Empty(); + } + } + + return matches; + + //again, would be much easier with a list + /*var matches = new List(); foreach (var line in lines) { if (TryParseMatch(line, out Match? match)) @@ -51,7 +67,7 @@ public static class QualData return Array.Empty(); } } - return matches.ToArray(); + return matches.ToArray();*/ } /// @@ -120,7 +136,33 @@ public static class QualData /// An array of unique country names public static string[] ExtractUniqueCountryNames(Match[] matches) { - string[] countryNames = []; + int uniqueCount = 0; + string[] tempCountryNames = new string[matches.Length * 2]; + foreach (var match in matches) + { + TeamResult homeTeam = match.HomeTeam; + TeamResult guestTeam = match.GuestTeam; + if (!IsContained(homeTeam.TeamName, tempCountryNames)) + { + tempCountryNames[uniqueCount++] = homeTeam.TeamName; + } + if (!IsContained(guestTeam.TeamName, tempCountryNames)) + { + tempCountryNames[uniqueCount++] = guestTeam.TeamName; + } + } + + string[] countryNames = new string[uniqueCount]; + + for (int i = 0; i < uniqueCount; i++) + { + countryNames[i] = tempCountryNames[i]; + } + + return countryNames; + + //Would be much easier with a list, but unfortunately the task requires an array + /*string[] countryNames = []; foreach (var match in matches) { TeamResult homeTeam = match.HomeTeam; @@ -135,7 +177,7 @@ public static class QualData } } - return countryNames; + return countryNames;*/ } /// @@ -147,7 +189,30 @@ public static class QualData /// Array of matches in which the supplied country participated public static Match[] FilterMatchesByCountry(Match[] allMatches, string countryName) { - Match[] filteredMatches = []; + int count = 0; + foreach (var match in allMatches) + { + if (IsCountryMatch(match, countryName)) + { + count++; + } + } + + Match[] filteredMatches = new Match[count]; + + int index = 0; + for (int i = 0; i < allMatches.Length; i++) + { + if (IsCountryMatch(allMatches[i], countryName)) + { + filteredMatches[index++] = allMatches[i]; + } + } + + return filteredMatches; + + //Mister Haslinger, please allow me to use lists + /*Match[] filteredMatches = []; foreach (var match in allMatches) { if (IsCountryMatch(match, countryName)) @@ -157,7 +222,7 @@ public static class QualData } - return filteredMatches; + return filteredMatches;*/ } ///