1
0

feat: complete "Lab2.K2"

This commit is contained in:
Rokas Puzonas 2021-11-25 16:30:28 +02:00
parent 3c08b04264
commit 27098835b4
4 changed files with 184 additions and 0 deletions

8
Lab4.K2/Lab4.K2.csproj Normal file
View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>

165
Lab4.K2/Program.cs Normal file
View File

@ -0,0 +1,165 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.IO;
namespace Lab4.K2
{
static class TaskUtils
{
public static bool NoDigits(string line)
{
return !Regex.IsMatch(line, @"\d");
}
public static int NumberDifferentVowelsInLine(string line)
{
string vowels = "AEIYOUaeiyouĄąĘęĖėĮįŲųŪū";
string pattern = string.Format(@"[{0}]", Regex.Escape(vowels));
return Regex.Matches(line, pattern).Count;
}
// Note: returns words including punctuation after them
public static MatchCollection MatchByWords(string line, string punctuation)
{
string pattern = string.Format(@"[^{0}]+[{0}]*", Regex.Escape(punctuation));
return Regex.Matches(line, pattern);
}
public static string FindWord1Line(string line, string punctuation)
{
List<string> wordsWith3Vowels = new List<string>();
foreach (Match match in MatchByWords(line, punctuation))
{
int vowelCount = NumberDifferentVowelsInLine(match.Value);
if (vowelCount == 3)
{
wordsWith3Vowels.Add(match.Value);
}
}
string longestWord = "";
foreach (string word in wordsWith3Vowels)
{
if (word.Length > longestWord.Length)
{
longestWord = word;
}
}
return longestWord;
}
public static string EditLine(string line, string punctuation, string word)
{
// 1. Find given word in line
MatchCollection words = MatchByWords(line, punctuation);
int wordPosition = -1;
for (int i = 0; i < words.Count; i++)
{
if (words[i].Value == word)
{
wordPosition = i;
break;
}
}
if (wordPosition == -1) return line;
// 2. Move found word to front
StringBuilder builder = new StringBuilder();
builder.Append(words[wordPosition]);
for (int i = 0; i < words.Count; i++)
{
if (i != wordPosition)
{
builder.Append(words[i]);
}
}
// 3. Profit
return builder.ToString();
}
public static Match MatchLastWord(string line, string punctuation)
{
string pattern = string.Format(@"[^{0}]+[{0}]*$", Regex.Escape(punctuation));
return Regex.Match(line, pattern);
}
public static string FindWord2Line(string line, string punctuation)
{
Match lastWord = MatchLastWord(line, punctuation);
if (NoDigits(lastWord.Value))
{
return lastWord.Value;
}
else
{
return "";
}
}
public static IEnumerable<string> ReadByLines(string filename)
{
string line;
using (StreamReader reader = new StreamReader(filename, Encoding.UTF8))
{
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
}
public static void PerformTask(string fd, string fr)
{
List<string> wordsWithoutDigits = new List<string>();
string punctuation = null;
using (StreamWriter writer = File.CreateText(fr))
{
foreach (string line in ReadByLines(fd))
{
if (punctuation == null)
{
punctuation = line;
continue;
}
string longestWord = TaskUtils.FindWord1Line(line, punctuation);
if (longestWord != "")
{
writer.WriteLine(TaskUtils.EditLine(line, punctuation, longestWord));
}
else
{
writer.WriteLine(line);
}
string wordWithoutDigits = TaskUtils.FindWord2Line(line, punctuation);
if (wordWithoutDigits != "")
{
wordsWithoutDigits.Add(wordWithoutDigits);
}
}
char[] punctuationChars = punctuation.ToCharArray();
foreach (string word in wordsWithoutDigits)
{
writer.WriteLine(word.Trim(punctuationChars));
}
}
}
}
class Program
{
static void Main(string[] args)
{
TaskUtils.PerformTask("Tekstas.txt", "RedTekstas.txt");
}
}
}

5
Lab4.K2/Tekstas.txt Normal file
View File

@ -0,0 +1,5 @@
.!?;:@
bar fooo,@ !ba1
bar bAaar baar bAoUr!!
bar foėo@ 1ba
bar? baer boar f!!

View File

@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.AddSurname", "Lab4.Add
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.RemoveVowels", "Lab4.RemoveVowels\Lab4.RemoveVowels.csproj", "{5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.K2", "Lab4.K2\Lab4.K2.csproj", "{BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -49,6 +51,10 @@ Global
{5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AD87F42-DD6D-4ADF-B1D5-66553BAA4BCC}.Release|Any CPU.Build.0 = Release|Any CPU
{BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE0E7198-95F7-4EFA-BFE1-CA5BBE7798D2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE