A greatestDistanceBetweenWords() fn nem működik helyesen, javítani kell
This commit is contained in:
@@ -147,6 +147,88 @@ public static class Szoveg{
|
||||
{
|
||||
return wordsWithUniqeCharactersOnly.OrderByDescending(s => s.Length).First();
|
||||
}
|
||||
|
||||
public static int GreatestDistanceBetweenWords(string input)
|
||||
{
|
||||
int maxDistance = 0;
|
||||
int? firstIndex = null;
|
||||
|
||||
int firstEndIndex = 0;
|
||||
int? secondIndex = null;
|
||||
int secondStartIndex = 0;
|
||||
int secondEndIndex = 0;
|
||||
|
||||
for (int i = 1; i < input.Length - 2; i++)
|
||||
{
|
||||
// új névelő megtalálva
|
||||
if (input[i] == 'A' && input[i - 1] == ' ' && input[i + 1] == ' ')
|
||||
{
|
||||
if (firstIndex == null)
|
||||
{
|
||||
firstIndex = i;
|
||||
firstEndIndex = i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (secondIndex == null && firstIndex != null)
|
||||
{
|
||||
secondIndex = i;
|
||||
secondStartIndex = i;
|
||||
secondEndIndex = i;
|
||||
}
|
||||
|
||||
// kettő közötti távolság
|
||||
int distance = Convert.ToInt32(secondStartIndex - firstEndIndex - 1);
|
||||
if (distance > maxDistance)
|
||||
{
|
||||
maxDistance = distance;
|
||||
}
|
||||
|
||||
firstIndex = secondIndex;
|
||||
|
||||
firstEndIndex = secondEndIndex;
|
||||
|
||||
secondIndex = i;
|
||||
secondStartIndex = i;
|
||||
secondEndIndex = i;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input [i] == 'A' && input[i + 1] == 'Z' && input[i - 1] == ' ' && input[i + 2] == ' '){
|
||||
if (firstIndex == null)
|
||||
{
|
||||
firstIndex = i;
|
||||
firstEndIndex = i + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (secondIndex == null && firstIndex != null)
|
||||
{
|
||||
secondIndex = i;
|
||||
secondStartIndex = i;
|
||||
secondEndIndex = i + 1;
|
||||
}
|
||||
|
||||
// kettő közötti távolság
|
||||
int distance = Convert.ToInt32(secondStartIndex - firstEndIndex - 1);
|
||||
if (distance > maxDistance)
|
||||
{
|
||||
maxDistance = distance;
|
||||
}
|
||||
|
||||
firstIndex = secondIndex;
|
||||
|
||||
firstEndIndex = secondEndIndex;
|
||||
|
||||
secondIndex = i;
|
||||
secondStartIndex = i;
|
||||
secondEndIndex = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return maxDistance;
|
||||
}
|
||||
}
|
||||
|
||||
public class Program
|
||||
@@ -178,11 +260,20 @@ public static class Szoveg{
|
||||
|
||||
// feladat 2 a
|
||||
|
||||
Szoveg.ReadDataFromFile(@"..\szoveg.txt");
|
||||
Szoveg.ReadDataFromFile(@"..\testinput.txt");
|
||||
|
||||
/*
|
||||
Szoveg.wordsWithUniqeCharactersOnly = Szoveg.WordsWithUniqeCharactersOnly();
|
||||
|
||||
System.Console.WriteLine($"{Szoveg.LongestStringWithUniqueCharacters()}");
|
||||
*/
|
||||
|
||||
// feladat 2 b
|
||||
string fullTextInSingleString = string.Join(" ", Szoveg.fullTextInListOfString);
|
||||
|
||||
int value = Szoveg.GreatestDistanceBetweenWords(fullTextInSingleString);
|
||||
|
||||
System.Console.WriteLine($"{value}");
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user