From 27098835b41350afee6312a7448f1e09a4d48770 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 25 Nov 2021 16:30:28 +0200 Subject: [PATCH] feat: complete "Lab2.K2" --- Lab4.K2/Lab4.K2.csproj | 8 ++ Lab4.K2/Program.cs | 165 +++++++++++++++++++++++++++++++++++++++++ Lab4.K2/Tekstas.txt | 5 ++ Lab4.sln | 6 ++ 4 files changed, 184 insertions(+) create mode 100644 Lab4.K2/Lab4.K2.csproj create mode 100644 Lab4.K2/Program.cs create mode 100644 Lab4.K2/Tekstas.txt diff --git a/Lab4.K2/Lab4.K2.csproj b/Lab4.K2/Lab4.K2.csproj new file mode 100644 index 0000000..23df604 --- /dev/null +++ b/Lab4.K2/Lab4.K2.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + + diff --git a/Lab4.K2/Program.cs b/Lab4.K2/Program.cs new file mode 100644 index 0000000..7667f33 --- /dev/null +++ b/Lab4.K2/Program.cs @@ -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 wordsWith3Vowels = new List(); + + 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 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 wordsWithoutDigits = new List(); + 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"); + } + } +} + diff --git a/Lab4.K2/Tekstas.txt b/Lab4.K2/Tekstas.txt new file mode 100644 index 0000000..aa597a6 --- /dev/null +++ b/Lab4.K2/Tekstas.txt @@ -0,0 +1,5 @@ + .!?;:@ +bar fooo,@ !ba1 +bar bAaar baar bAoUr!! +bar foėo@ 1ba +bar? baer boar f!! diff --git a/Lab4.sln b/Lab4.sln index 70dafb1..e68a3ea 100644 --- a/Lab4.sln +++ b/Lab4.sln @@ -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