Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Зиновьева Милана #224

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
36 changes: 18 additions & 18 deletions cs/Chess/ChessProblem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Chess
using System.Drawing;

namespace Chess
{
public class ChessProblem
{
Expand All @@ -13,21 +15,22 @@ public static void LoadFrom(string[] lines)
// Определяет мат, шах или пат белым.
public static void CalculateChessStatus()
{
var isCheck = IsCheckForWhite();
var isCheck = IsCheck(PieceColor.White);
var hasMoves = false;
foreach (var locFrom in board.GetPieces(PieceColor.White))
{
foreach (var locTo in board.GetPiece(locFrom).GetMoves(locFrom, board))
{
var old = board.GetPiece(locTo);
board.Set(locTo, board.GetPiece(locFrom));
board.Set(locFrom, null);
if (!IsCheckForWhite())
var previousPosition = board.GetPiece(locTo);
using var tempMove = board.PerformTemporaryMove(locFrom, locTo);
if (!IsCheck(PieceColor.White))
hasMoves = true;
board.Set(locFrom, board.GetPiece(locTo));
board.Set(locTo, old);
}
}
SetChessStatus(isCheck, hasMoves);
}

private static void SetChessStatus(bool isCheck, bool hasMoves){
if (isCheck)
if (hasMoves)
ChessStatus = ChessStatus.Check;
Expand All @@ -37,21 +40,18 @@ public static void CalculateChessStatus()
}

// check — это шах
private static bool IsCheckForWhite()
private static bool IsCheck(PieceColor color)
{
var isCheck = false;
foreach (var loc in board.GetPieces(PieceColor.Black))
var oppositeColor = (color == PieceColor.White) ? PieceColor.Black : PieceColor.White;
foreach (var locFrom in board.GetPieces(oppositeColor))
{
var piece = board.GetPiece(loc);
var moves = piece.GetMoves(loc, board);
foreach (var destination in moves)
foreach (var locTo in board.GetPiece(locFrom).GetMoves(locFrom, board))
{
if (Piece.Is(board.GetPiece(destination),
PieceColor.White, PieceType.King))
isCheck = true;
if (Piece.Is(board.GetPiece(locTo),
color, PieceType.King))
return true;
}
}
if (isCheck) return true;
return false;
}
}
Expand Down
14 changes: 11 additions & 3 deletions cs/ControlDigit/ControlDigit.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>8</LangVersion>
<LangVersion>6</LangVersion>
<TargetFramework>net6</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="coverlet.collector" Version="6.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
59 changes: 56 additions & 3 deletions cs/ControlDigit/Snils/SnilsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework.Internal.Execution;
using System.Linq;

namespace ControlDigit
{
public static class SnilsExtensions
{
public static int CalculateSnils(this long number)
{
throw new NotImplementedException();
public static int CalculateSnils(this long number){
var dict = NumberDecompotion(number);
var M = SummDecompotion(dict);
var result = MakeControlDigit(M);
return result;
}

private static Dictionary<int, int> NumberDecompotion(this long number){
var counter = 1;
var dict = new Dictionary<int, int>();
while(number / 10 >= 1){
dict.Add(counter, (int)number % 10);
counter++;
number = number / 10;
}
dict.Add(counter, (int)number % 10);
return dict;
}

private static int SummDecompotion(Dictionary<int, int> dict){
var summ = 0;
foreach(var pair in dict){
summ += pair.Key * pair.Value;
}
return summ;
}

private static IEnumerable<int> MakeFactorsSnils(int length){
return Enumerable.Range(1, length);
}

private static IEnumerable<int> MakeFactorsUpc(int length){
return Enumerable.Range(1, length);
}

private static int CountControlDigit(IEnumerable<int> values, Func<int, IEnumerable<int>> evaluateFactors, int length){
var factors = evaluateFactors(length);
var summ = 0;
//for(int i = 0;i < length;i++){
//summ += factors[i] * values[i];
//}
var ggygu = factors.Zip(values)
}

private static int MakeControlDigit(int summ){
if (summ > 101)
summ = summ % 101;
if (summ < 100)
return summ;
else
return 0;
}
}
}
11 changes: 10 additions & 1 deletion cs/ControlDigit/Upc/UpcExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ public static class UpcExtensions
{
public static int CalculateUpc(this long number)
{
throw new NotImplementedException();
var numberToString = number.ToString();
var summ = 0;
for(int i = numberToString.Length - 1;i>=0;i-=2){
summ += int.Parse(numberToString[i].ToString()) * 3;
}
for(int i = numberToString.Length - 2;i>=0;i-=2){
summ += int.Parse(numberToString[i].ToString());
}
var M = (summ % 10 == 0)? 0: 10 - summ % 10;
return M;
}
}
}
97 changes: 97 additions & 0 deletions cs/Markdown/HelperFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
namespace Markdown;

public class HelperFunctions
{
private static readonly char[] forbiddenChars = ['_', '#'];

private static readonly char[] tagChars = ['_', '#', '\\'];

public static int FindCorrectCloseSymbolForItalic(string text, int startIndex)
{
for (int i = startIndex + 1; i < text.Length; i++)
{
if (text[i] == '_' && !IsPartOfDoubleUnderscore(text, i) && IsValidCloseSymbol(text, i))
return i;
}
return -1;
}

private static bool IsValidCloseSymbol(string text, int index)
{
if (index + 1 < text.Length && char.IsDigit(text[index + 1]))
return false;
if (index - 1 >= 0 && char.IsDigit(text[index - 1]))
{
if (index + 1 == text.Length || text.Substring(index + 1).All(char.IsWhiteSpace))
return true;

return false;
}
return true;
}

public static string ProcessNestedTag(ref string text)
{
Md md = new Md(['_', '\\']);
return md.Render(text);
}

public static bool ContainsOnlyDash(string text) =>
text.All(symbol => forbiddenChars.Contains(symbol));

public static bool ContainsOnlyHeading(string text) =>
text.All(symbol => symbol == '#');

public static bool ContainsOnlySpases(string text) =>
string.IsNullOrWhiteSpace(text) || string.IsNullOrEmpty(text);

private static bool IsPartOfDoubleUnderscore(string text, int index) =>
(index + 1 < text.Length && text[index + 1] == '_') ||
(index > 0 && text[index - 1] == '_');

public static bool ContainsUnderscore(string text) => text.Contains('_');

public static (List<int>, List<int>) GetUnderscoreIndexes(string text)
{
List<int> singleUnderscoreIndexes = new List<int>();
List<int> doubleUnderscoreIndexes = new List<int>();
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '_' && ((i + 1 < text.Length && text[i + 1] != '_') || i == text.Length - 1))
{
singleUnderscoreIndexes.Add(i);
}
if (i + 1 < text.Length && text[i] == '_' && text[i + 1] == '_')
{
doubleUnderscoreIndexes.Add(i);
i++;
}
}
return (singleUnderscoreIndexes, doubleUnderscoreIndexes);
}

public static bool HasUnpairedTags(List<int> indexes1, List<int> indexes2)
{
if (indexes1.Count < 2 || indexes2.Count < 2 || indexes1.Count % 2 == 1 || indexes2.Count % 2 == 1)
{
return false;
}
return true;
}

public static bool AreSegmentsIntersecting(int[] segment1, int[] segment2)
{
return segment1[1] >= segment2[0] && segment1[0] <= segment2[1];
}

public static bool AreSegmentsNested(int[] segment1, int[] segment2)
{
return (segment1[0] >= segment2[0] && segment1[1] <= segment2[1]) ||
(segment2[0] >= segment1[0] && segment2[1] <= segment1[1]);
}

public static string RemoveExtraSpaces(string input)
{
return string.Join(" ", input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return string.Join(" ", input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
return string.Join(" ", input.Split(' ', StringSplitOptions.RemoveEmptyEntries));

}
}
11 changes: 11 additions & 0 deletions cs/Markdown/Markdown.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
</PropertyGroup>
</Project>
51 changes: 51 additions & 0 deletions cs/Markdown/Md.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Markdown.Tags;

namespace Markdown;

public class Md
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Булы тут немного не к месту, хочется от них избавиться. Как вариант, можно добавить дефолтный хендлер для символов, которые не являются тегами, перестать опираться на tagChars и использовать handler.IsTagStart. Чтобы всё заработало, хендлеры должны быть расположены в списке в корректном порядке. Тогда получится, что для каждого символа, мы берем первый подходящий хендлер. Так же придется вынести хендлеры в конструктор, для гибкой конфигурации.

{
private static readonly List<ITagHandler> TagHandlers =
[
new BoldTag(),
new ItalicTag(),
new HeadingTag(),
new EscapeTag()
];
private readonly char[] tagChars;

public Md(char[]? customTagChars = null)
{
tagChars = customTagChars ?? ['_', '#', '\\'];
}

private bool CanBeTag(char symbol) => tagChars.Contains(symbol);

public string Render(string markdownString)
{
int index = 0;
while (index < markdownString.Length)
{
if (CanBeTag(markdownString[index]))
{
if (TryProcessTag(ref markdownString, ref index))
continue;
}
index++;
}

return markdownString;
}

private static bool TryProcessTag(ref string markdownString, ref int index)
{
foreach (var handler in TagHandlers)
{
if (handler.IsTagStart(markdownString, index))
{
index = handler.ProcessTag(ref markdownString, index);
return true;
}
}
return false;
}
}
Loading