diff --git a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub.Tests/RicksMercilessEncryptorTests.cs b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub.Tests/RicksMercilessEncryptorTests.cs index 434f02aa..abfc3690 100644 --- a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub.Tests/RicksMercilessEncryptorTests.cs +++ b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub.Tests/RicksMercilessEncryptorTests.cs @@ -1,10 +1,80 @@ using System; +using System.Collections.Generic; +using System.Collections.Immutable; using Xunit; +using WubbaLubbaDubDub; namespace WubbaLubbaDubDub.Tests { public class RicksMercilessEncryptorTests { + [Fact] + public void SplitToLinesTest() + { + string[] expected = new[] {"line 1", "line2"}; + Assert.Equal(RicksMercilessEncryptor.SplitToLines("line 1\nline2"), expected); + } + + [Fact] + public void SplitToWordsTest() + { + string[] expected = new[] {"word", "and", "word", "and", "word"}; + Assert.Equal(RicksMercilessEncryptor.SplitToWords("word and word and word"), expected); + } + + [Fact] + public void GetLeftHalfTest() + { + Assert.Equal(RicksMercilessEncryptor.GetLeftHalf("string"), "str"); + Assert.Equal(RicksMercilessEncryptor.GetLeftHalf("strings"), "str"); + } + + [Fact] + public void GetRightHalfTest() + { + Assert.Equal(RicksMercilessEncryptor.GetRightHalf("string"), "ing"); + Assert.Equal(RicksMercilessEncryptor.GetRightHalf("strings"), "ings"); + } + + [Fact] + public void ReplaceTest() + { + Assert.Equal(RicksMercilessEncryptor.Replace("baccabbaccabbac", "bac", "cab"), "cabcabcabcabcab"); + } + [Fact] + public void CharsToCodesTest() + { + Assert.Equal(RicksMercilessEncryptor.CharsToCodes("АБВ"), "\\u0410\\u0411\\u0412"); + } + + [Fact] + public void GetReversedTest() + { + Assert.Equal(RicksMercilessEncryptor.GetReversed("string"), "gnirts"); + } + + [Fact] + public void InverseCaseTest() + { + Assert.Equal(RicksMercilessEncryptor.InverseCase("WorD"), "wORd"); + } + + [Fact] + public void ShiftIncTest() + { + Assert.Equal(RicksMercilessEncryptor.ShiftInc("АБВ"), "БВГ"); + } + + [Fact] + public void GetUsedObjectsTest() + { + List expected = new List(); + expected.Add(305402420); + Assert.Equal(RicksMercilessEncryptor.GetUsedObjects("1234:1234"), expected.ToImmutableList()); + Assert.Equal(RicksMercilessEncryptor.GetUsedObjects("1234:1234 //some stuff 1111:1111"), expected.ToImmutableList()); + Assert.Equal(RicksMercilessEncryptor.GetUsedObjects("1234:1234 /**/some stuff 1111:1111 \n 1212:1212 \n /**/"), + expected.ToImmutableList()); + } } } diff --git a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub.Tests/WubbaLubbaDubDub.Tests.csproj b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub.Tests/WubbaLubbaDubDub.Tests.csproj index aa2f23f5..c6980af2 100644 --- a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub.Tests/WubbaLubbaDubDub.Tests.csproj +++ b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub.Tests/WubbaLubbaDubDub.Tests.csproj @@ -2,14 +2,18 @@ netcoreapp2.0 - false + + + + + diff --git a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/Program.cs b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/Program.cs new file mode 100644 index 00000000..34f3574e --- /dev/null +++ b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/Program.cs @@ -0,0 +1,73 @@ +using System; +using System.Linq; +using System.Text; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Columns; +using BenchmarkDotNet.Configs; +using BenchmarkDotNet.Exporters.Csv; +using BenchmarkDotNet.Exporters.Json; +using BenchmarkDotNet.Loggers; +using BenchmarkDotNet.Running; + + + +namespace WubbaLubbaDubDub +{ + [Config(typeof(BenchmarkConfig))] + public class MyBenchmark + { + + private class BenchmarkConfig : ManualConfig + { + public BenchmarkConfig() + { + AddColumn(StatisticColumn.Mean); + AddColumn(StatisticColumn.Median); + AddColumn(StatisticColumn.StdDev); + AddColumn(StatisticColumn.Max); + AddLogger(ConsoleLogger.Default); + AddExporter(CsvExporter.Default); + UnionRule = ConfigUnionRule.AlwaysUseLocal; + } + } + + private readonly string data = new String('a', 1000); + + [Benchmark(Description = "String.Join()")] + public string TestJoin() + { + return String.Join("", data.ToCharArray()); + } + + [Benchmark(Description = "TestBuilder")] + public string TestBuilder() + { + StringBuilder testStringBuilder = new StringBuilder(""); + + foreach (var sym in data.ToCharArray()) + { + testStringBuilder.Append(sym); + } + + return testStringBuilder.ToString(); + } + + [Benchmark(Description = "Concat")] + public string TestConcat() + { + return String.Concat(data.ToCharArray()); + } + } + + public class Program + { + private static void Main() + { + var result = BenchmarkRunner.Run(); + } + } +} + + + + diff --git a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/RicksMercilessEncryptor.cs b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/RicksMercilessEncryptor.cs index 1063065b..526f2929 100644 --- a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/RicksMercilessEncryptor.cs +++ b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/RicksMercilessEncryptor.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using System.Globalization; +using System.Linq; +using System.Text.RegularExpressions; namespace WubbaLubbaDubDub { @@ -12,7 +15,7 @@ public static class RicksMercilessEncryptor public static string[] SplitToLines(this string text) { // У строки есть специальный метод. Давай здесь без регулярок - throw new NotImplementedException(); + return text.Split('\n'); } /// @@ -21,7 +24,7 @@ public static string[] SplitToLines(this string text) public static string[] SplitToWords(this string line) { // А вот здесь поиграйся с регулярками. - throw new NotImplementedException(); + return Regex.Replace(line, "[^a-zA-Z]", " ").Split(' '); } /// @@ -31,7 +34,7 @@ public static string[] SplitToWords(this string line) public static string GetLeftHalf(this string s) { // у строки есть метод получения подстроки - throw new NotImplementedException(); + return s.Substring(0, s.Length / 2); } /// @@ -40,7 +43,7 @@ public static string GetLeftHalf(this string s) /// public static string GetRightHalf(this string s) { - throw new NotImplementedException(); + return s.Substring(s.Length / 2); } /// @@ -49,7 +52,7 @@ public static string GetRightHalf(this string s) public static string Replace(this string s, string old, string @new) { // и такой метод у строки, очевидно, тоже есть - throw new NotImplementedException(); + return s.Replace(old, @new); } /// @@ -65,7 +68,18 @@ public static string CharsToCodes(this string s) FYI: локальную функцию можно объявлять даже после строки с return. То же самое можно сделать и для всех оставшихся методов. */ - throw new NotImplementedException(); + return String.Concat(ToCode(s.ToCharArray())); + + string[] ToCode(char[] symbols) + { + string[] output = new String[symbols.Length]; + for (int i = 0; i < symbols.Length; ++i) + { + output[i] = "\\u" + ((int) symbols[i]).ToString("X4"); + } + + return output; + } } /// @@ -77,7 +91,7 @@ public static string GetReversed(this string s) Собрать строку из последовательности строк можно несколькими способами. Один из низ - статический метод Concat. Но ты можешь выбрать любой. */ - throw new NotImplementedException(); + return String.Concat(s.ToCharArray().Reverse()); } /// @@ -90,7 +104,24 @@ public static string InverseCase(this string s) На минуту задержись здесь и посмотри, какие еще есть статические методы у char. Например, он содержит методы-предикаты для определения категории Юникода символа, что очень удобно. */ - throw new NotImplementedException(); + return String.Concat(ReverseCase(s.ToCharArray())); + + char[] ReverseCase(char[] symbols) + { + for (int i = 0; i < symbols.Length; ++i) + { + if (char.IsLower(symbols[i])) + { + symbols[i] = char.ToUpper(symbols[i]); + } + else + { + symbols[i] = char.ToLower(symbols[i]); + } + } + + return symbols; + } } /// @@ -99,7 +130,19 @@ public static string InverseCase(this string s) /// public static string ShiftInc(this string s) { - throw new NotImplementedException(); + + return String.Concat(ShiftSym(s.ToCharArray())); + + char[] ShiftSym(char[] symbols) + { + + for (int i = 0; i < symbols.Length; ++i) + { + symbols[i]++; + } + + return symbols; + } } @@ -117,7 +160,17 @@ public static IImmutableList GetUsedObjects(this string text) Задача на поиграться с регулярками - вся сложность в том, чтобы аккуратно игнорировать комментарии. Экспериментировать онлайн можно, например, здесь: http://regexstorm.net/tester и https://regexr.com/ */ - throw new NotImplementedException(); + text = Regex.Replace(text, "//[\\s\\S]*?(\n|$)", " "); + text = Regex.Replace(text, "/\\*\\*/([\\s\\S]*?)/\\*\\*/", " "); + HashSet output = new HashSet(); + Regex matches = new Regex("[\\dA-Fa-f]{4}:[\\dA-Fa-f]{4}", RegexOptions.Compiled); + foreach (var match in matches.Matches(text)) + { + String id_string = match.ToString(); + id_string = id_string.Replace(":",""); + output.Add(long.Parse(id_string, NumberStyles.HexNumber)); + } + return output.ToImmutableList(); } #endregion diff --git a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/WubbaLubbaDubDub.MyBenchmark-report.csv b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/WubbaLubbaDubDub.MyBenchmark-report.csv new file mode 100644 index 00000000..e4db65e0 --- /dev/null +++ b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/WubbaLubbaDubDub.MyBenchmark-report.csv @@ -0,0 +1,4 @@ +Mean;StdDev;Median;Max +22.995 μs;0.9327 μs;22.720 μs;25.307 μs +3.742 μs;0.0776 μs;3.719 μs;3.893 μs +9.941 μs;0.2040 μs;9.854 μs;10.367 μs diff --git a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/WubbaLubbaDubDub.csproj b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/WubbaLubbaDubDub.csproj index 5766db61..df2be339 100644 --- a/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/WubbaLubbaDubDub.csproj +++ b/course-2021-1/exercises/04-wubba-lubba-dub-dub/WubbaLubbaDubDub/WubbaLubbaDubDub/WubbaLubbaDubDub.csproj @@ -2,6 +2,12 @@ netcoreapp2.0 + false + + + + +