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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
55 changes: 55 additions & 0 deletions cs/Markdown/HelperFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace Markdown;

public class HelperFunctions
{
private static readonly char[] forbiddenChars = ['_', '#'];
public static int ScreeningCheck(ref string text, int startIndex)
{
if (IsScreening(text, startIndex))
{
int endIndex = text.IndexOf('\\', startIndex + 1);
if (endIndex == -1)
return startIndex + 1;
text = text.Remove(startIndex - 1, 1).Remove(endIndex - 1, 1);
return endIndex - 1;
}
return startIndex;
}

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

private static bool IsScreening(string text, int index) =>
index - 1 >= 0 && text[index - 1] == '\\';

public static bool IsDigit(string text, int index) =>
index - 1 >= 0 && char.IsDigit(text[index - 1]);

public static string ProcessNestedTag(ref string text) =>
Md.Render(text);

public static bool ContainsWhiteSpaces(string text) =>
text.Contains(' ') || string.IsNullOrWhiteSpace(text);

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

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

private static bool IsSurroundedByWhitespaceOrDigit(string text, int index) =>
(index > 0 && char.IsWhiteSpace(text[index - 1])) ||
(index + 1 < text.Length && char.IsWhiteSpace(text[index + 1])) ||
(index > 0 && char.IsDigit(text[index - 1]));
}
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>
42 changes: 42 additions & 0 deletions cs/Markdown/Md.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Markdown.Tags;

namespace Markdown;

public class Md
{
private static readonly List<ITagHandler> TagHandlers =
[
new BoldTag(),
new ItalicTag(),
new HeadingTag()
];

public static string Render(string markdownString)
{
if (string.IsNullOrEmpty(markdownString) || HelperFunctions.ContainsOnlyDash(markdownString))
return markdownString;

int index = 0;
while (index < markdownString.Length)
{
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;
}
}
56 changes: 56 additions & 0 deletions cs/Markdown/Tags/BaseTagHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Markdown.Tags;

public abstract class BaseTagHandler : ITagHandler
{
protected abstract string Symbol { get; }
protected abstract string HtmlTag { get; }

public abstract bool IsTagStart(string text, int index);

public int ProcessTag(ref string text, int startIndex)
{
var newIndex = HelperFunctions.ScreeningCheck(ref text, startIndex);
if (newIndex != startIndex)
return newIndex;

int endIndex = FindEndIndex(text, startIndex);

if (endIndex == -1)
return startIndex + Symbol.Length;

string content = ExtractContent(text, startIndex, endIndex);
if (HelperFunctions.ContainsWhiteSpaces(content))
return startIndex + content.Length;

content = ProcessNestedTag(ref content);
string replacement = WrapWithHtmlTag(content);
text = ReplaceText(text, startIndex, endIndex, replacement);

return startIndex + replacement.Length;
}

protected virtual string ProcessNestedTag(ref string text)
{
return HelperFunctions.ProcessNestedTag(ref text);
}

protected virtual int FindEndIndex(string text, int startIndex)
{
return text.IndexOf(Symbol, startIndex + Symbol.Length);
}

protected virtual string ExtractContent(string text, int startIndex, int endIndex)
{
return text.Substring(startIndex + Symbol.Length, endIndex - startIndex - Symbol.Length);
}

protected virtual string WrapWithHtmlTag(string content)
{
return $"<{HtmlTag}>{content}</{HtmlTag}>";
}

protected virtual string ReplaceText(string text, int startIndex, int endIndex, string replacement)
{
return text.Substring(0, startIndex) + replacement + text.Substring(endIndex + Symbol.Length);
}
}
12 changes: 12 additions & 0 deletions cs/Markdown/Tags/BoldTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Markdown.Tags;
public class BoldTag : BaseTagHandler
{
protected override string Symbol => "__";
protected override string HtmlTag => "strong";

public override bool IsTagStart(string text, int index)
{
return index + 2 < text.Length && text.Substring(index, 2) == Symbol
&& !char.IsWhiteSpace(text[index + 1]);
}
}
26 changes: 26 additions & 0 deletions cs/Markdown/Tags/HeadingTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Markdown.Tags;
public class HeadingTag : BaseTagHandler
{
protected override string Symbol => "#";
protected override string HtmlTag => "h1";

public override bool IsTagStart(string text, int index)
{
return text[index].ToString() == Symbol;
}

protected override int FindEndIndex(string text, int startIndex)
{
int endIndex = text.IndexOf('\n', startIndex + 1);
return endIndex == -1 ? text.Length : endIndex;
}

protected override string ReplaceText(string text, int startIndex, int endIndex, string replacement)
{
if (endIndex < text.Length && text[endIndex] == '\n')
{
return text.Substring(0, startIndex) + replacement + '\n' + text.Substring(endIndex + 1);
}
return text.Substring(0, startIndex) + replacement + text.Substring(endIndex);
}
}
7 changes: 7 additions & 0 deletions cs/Markdown/Tags/ITagHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Markdown.Tags;
public interface ITagHandler
{
bool IsTagStart(string text, int index);

int ProcessTag(ref string text, int startIndex);
}
Loading