now using arrays... \n Mister Haslinger, please allow me to use lists! Its much easier that way!

This commit is contained in:
MarcUs7i 2024-09-18 20:43:13 +02:00
parent 064de099f8
commit 852c8ed1a1

View file

@ -38,8 +38,24 @@ public static class QualData
return Array.Empty<Match>();
}
//using lists to avoid resizing the array (Programmers are lazy)
var matches = new List<Match>();
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<Match>();
}
}
return matches;
//again, would be much easier with a list
/*var matches = new List<Match>();
foreach (var line in lines)
{
if (TryParseMatch(line, out Match? match))
@ -51,7 +67,7 @@ public static class QualData
return Array.Empty<Match>();
}
}
return matches.ToArray();
return matches.ToArray();*/
}
/// <summary>
@ -120,7 +136,33 @@ public static class QualData
/// <returns>An array of unique country names</returns>
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;*/
}
/// <summary>
@ -147,7 +189,30 @@ public static class QualData
/// <returns>Array of matches in which the supplied country participated</returns>
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;*/
}
/// <summary>