using System.Collections.Generic;
internal class TaskSolver
public static void Main(string[] args)
TestGenerateWordsFromWord();
TestGetPreviousMaxDigital();
TestSearchQueenOrHorse();
Console.WriteLine("All Test completed!");
public static List<string> GenerateWordsFromWord(string word, List<string> wordDictionary)
IEnumerable<char> letters = word.Where(p=> true);
List<char> tmpList = new List<char>(letters);
IOrderedEnumerable<string> res = new List<string>(wordDictionary
if (tmpList.Contains(ch))
tmpList = new List<char>(letters);
public static int MaxLengthTwoChar(string word)
List<string> combinations = new List<string>();
List<char> allLeters = new List<char>();
foreach (char ch in word)
if (!allLeters.Contains(ch))
FillSet(ref combinations,ref allLeters, allLeters.Count);
for (int i = 0; i < combinations.Count(); i++)
foreach (char ch in word)
if ((ch == combinations[i][0]) || (ch == combinations[i][1]))
if ((isAlternativeString(altWord)) && (altWord.Length > maxLength))
maxLength = altWord.Length;
public static void FillSet(ref List<string> set, ref List<char> Letters, int n)
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
set.Add((Letters[i].ToString() + Letters[j].ToString()));
public static bool isAlternativeString(string str)
for (int i = 0; i < n-1; i++)
if (str[i] == str[i + 1])
public static long GetPreviousMaxDigital(long value)
List<char> chars = value.ToString().Select(p => p).ToList();
for (int i = l-2; i >= 0; i--)
for (int j = i + 1; j < l; j++)
if ((chars[j] > t) && (chars[j] < chars[i]))
(chars[i],chars[ind]) = (chars[ind], chars[i]);
chars.Sort(i + 1, l - i - 1, Comparer<char>.Create((x,y) => (y-x)));
foreach (char ch in chars)
long s = Int64.Parse(str);
if (s == value || (str[0] == '0'))
public static List<int> SearchQueenOrHorse(char[][] gridMap)
int yLen = gridMap.GetLength(0);
int xLen = gridMap[0].GetLength(0);
int[,] intGridMap = new int[yLen,xLen];
for (int i = 0; i < yLen; i++)
for (int j = 0; j < xLen; j++)
if (gridMap[i][j] == 's')
else if(gridMap[i][j] == 'e')
intGridMap[i,j] = 100000;
int[,] intGridMap2 = (int[,])intGridMap.Clone();
HorseMove(ref intGridMap,cX,cY, xLen,yLen,1);
QueenMove(ref intGridMap2,cX,cY, xLen,yLen,1);
int res1 = intGridMap[endY,endX];
int res2 = intGridMap2[endY,endX];
return new List<int>{res1, res2};
public static int QueenMove(ref int[,] gridMap, int x, int y, int xL, int yL, int counter)
while ((tY < yL) && (tX < xL) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
QueenMove(ref gridMap,tX, tY, xL, yL, counter+1);
while ((tY < yL) && (tX < xL) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
QueenMove(ref gridMap,tX, tY, xL, yL, counter+1);
while ((tY >= 0) && (tX < xL) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
QueenMove(ref gridMap,tX, tY, xL, yL, counter+1);
while ((tY >= 0) && (tX >= 0) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
QueenMove(ref gridMap,tX, tY, xL, yL, counter+1);
while ((tY >= 0) && (tX >= 0) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
QueenMove(ref gridMap,tX, tY, xL, yL, counter+1);
while ((tY >= 0) && (tX >= 0) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
QueenMove(ref gridMap,tX, tY, xL, yL, counter+1);
while ((tY >= 0) && (tX < xL) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
QueenMove(ref gridMap,tX, tY, xL, yL, counter+1);
while ((tY < yL) && (tX >= 0) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
QueenMove(ref gridMap,tX, tY, xL, yL, counter+1);
public static int HorseMove(ref int[,] gridMap, int x, int y, int xL, int yL, int counter)
if ((tY < yL) && (tX < xL) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
HorseMove(ref gridMap,tX, tY, xL, yL, counter+1);
if ((tY < yL) && (tX < xL) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
HorseMove(ref gridMap,tX, tY, xL, yL, counter+1);
if ((tY >= 0) && (tX >= 0) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
HorseMove(ref gridMap,tX, tY, xL, yL, counter+1);
if ((tY >= 0) && (tX >= 0) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
HorseMove(ref gridMap,tX, tY, xL, yL, counter+1);
if ((tY >= 0) && (tX < xL) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
HorseMove(ref gridMap,tX, tY, xL, yL, counter+1);
if ((tY >= 0) && (tX < xL) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
HorseMove(ref gridMap,tX, tY, xL, yL, counter+1);
if ((tY < yL) && (tX >= 0) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
HorseMove(ref gridMap,tX, tY, xL, yL, counter+1);
if ((tY < yL) && (tX >= 0) && (gridMap[tY,tX] != -1))
int num = gridMap[tY,tX];
gridMap[tY,tX] = counter;
HorseMove(ref gridMap,tX, tY, xL, yL, counter+1);
public static long CalculateMaxCoins(int[][] mapData, int idStart, int idFinish)
List<List<KeyValuePair<int, int>>> connections = new List<List<KeyValuePair<int, int>>>();
for (int i = 0; i < mapData.GetLength(0); i++)
while ((connections.Count < mapData[i][0]+1) || (connections.Count < mapData[i][1]+1))
connections.Add(new List<KeyValuePair<int, int>>());
connections[mapData[i][0]].Add(new KeyValuePair<int, int>(mapData[i][1],mapData[i][2]));
connections[mapData[i][1]].Add(new KeyValuePair<int, int>(mapData[i][0],mapData[i][2]));
int len = connections.Count;
int[] weights = new int[len];
bool[] marks = new bool[len];
for (int i = 0; i < len; i++)
for (int i = 0; i < len; i++)
for (int j = 0; j < len; j++)
if (!marks[j] && ((v == -1) || ((float)1/weights[j] > (float)1/weights[v])))
if((v == -1) || (weights[v] == INF))
for (int j = 0; j < connections[v].Count; j++)
int to = connections[v][j].Key;
int ln = connections[v][j].Value;
if ((weights[to] < 0) || ((float)1/(weights[v] + ln)) < ((float)1/weights[to]))
weights[to] = (weights[v] + ln);
int n = weights[idFinish];
private static void TestGenerateWordsFromWord()
var wordsList = new List<string>
"кот", "ток", "око", "мимо", "гром", "ром", "мама",
"рог", "морг", "огр", "мор", "порог", "бра", "раб", "зубр"
AssertSequenceEqual( GenerateWordsFromWord("арбуз", wordsList), new []{ "бра", "зубр", "раб"} );
AssertSequenceEqual( GenerateWordsFromWord("лист", wordsList), new List<string>() );
AssertSequenceEqual( GenerateWordsFromWord("маг", wordsList), new List<string>() );
AssertSequenceEqual( GenerateWordsFromWord("погром", wordsList), new List<string>{ "гром", "мор", "морг", "огр", "порог", "рог", "ром"} );
private static void TestMaxLengthTwoChar()
AssertEqual(MaxLengthTwoChar("beabeeab"), 5);
AssertEqual(MaxLengthTwoChar("eqerarbereqevfdsarqerjwujfsbvvzxffsakfjkeapqefrkzxcvdaafqpdvsavkjzdafdafakkczoasdfcvzxckrfqwerivxcblgdfgw"), 6);
AssertEqual(MaxLengthTwoChar("а"), 0);
AssertEqual(MaxLengthTwoChar("ab"), 2);
private static void TestGetPreviousMaxDigital()
AssertEqual(GetPreviousMaxDigital(21), 12l);
AssertEqual(GetPreviousMaxDigital(531), 513l);
AssertEqual(GetPreviousMaxDigital(1027), -1l);
AssertEqual(GetPreviousMaxDigital(2071), 2017l);
AssertEqual(GetPreviousMaxDigital(207034), 204730l);
AssertEqual(GetPreviousMaxDigital(135), -1l);
AssertEqual(GetPreviousMaxDigital(9320125), 9315220l);
AssertEqual(GetPreviousMaxDigital(9123456789), 8997654321l);
AssertEqual(GetPreviousMaxDigital(1205731),1205713l);
AssertEqual(GetPreviousMaxDigital(135295723912632228), 135295723912628322l);
private static void TestSearchQueenOrHorse()
new[] {'s', '#', '#', '#', '#', '#'},
new[] {'#', 'x', 'x', 'x', 'x', '#'},
new[] {'#', '#', '#', '#', 'x', '#'},
new[] {'#', '#', '#', '#', 'x', '#'},
new[] {'#', '#', '#', '#', '#', 'e'},
AssertSequenceEqual(SearchQueenOrHorse(gridA), new []{3, 2});
new[] {'s', '#', '#', '#', '#', 'x'},
new[] {'#', 'x', 'x', 'x', 'x', '#'},
new[] {'#', 'x', '#', '#', 'x', '#'},
new[] {'#', '#', '#', '#', 'x', '#'},
new[] {'x', '#', '#', '#', '#', 'e'},
AssertSequenceEqual(SearchQueenOrHorse(gridB), new []{-1, 3});
new[] {'s', '#', '#', '#', '#', 'x'},
new[] {'x', 'x', 'x', 'x', 'x', 'x'},
new[] {'#', '#', '#', '#', 'x', '#'},
new[] {'#', '#', '#', 'e', 'x', '#'},
new[] {'x', '#', '#', '#', '#', '#'},
AssertSequenceEqual(SearchQueenOrHorse(gridC), new []{2, -1});
AssertSequenceEqual(SearchQueenOrHorse(gridD), new []{-1, 1});
AssertSequenceEqual(SearchQueenOrHorse(gridE), new []{1, -1});
new[] {'x', '#', '#', 'x'},
new[] {'#', 'x', 'x', '#'},
new[] {'#', 'x', '#', 'x'},
new[] {'e', 'x', 'x', 's'},
new[] {'#', 'x', 'x', '#'},
new[] {'x', '#', '#', 'x'},
AssertSequenceEqual(SearchQueenOrHorse(gridF), new []{-1, 5});
new[] {'#', '#', '#', '#', '#', '#', '#', '#', '#', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
new[] {'#', '#', '#', '#', '#', '#', '#', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
new[] {'#', '#', '#', '#', '#', '#', '#', 'x', 'x', '#', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
new[] {'#', 's', '#', '#', '#', '#', '#', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
new[] {'#', '#', '#', '#', '#', '#', 'x', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
new[] {'#', '#', '#', '#', '#', 'x', 'x', '#', '#', '#', '#', 'x', '#', '#', '#', '#', '#', '#', '#', '#'},
new[] {'#', '#', '#', '#', '#', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
new[] {'#', '#', '#', '#', '#', 'x', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
new[] {'#', '#', '#', '#', '#', '#', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
new[] {'#', '#', '#', '#', '#', '#', 'x', '#', '#', '#', '#', '#', '#', '#', 'x', 'x', 'x', 'x', 'x', 'x'},
new[] {'#', '#', '#', '#', 'x', 'x', 'x', '#', '#', '#', '#', '#', '#', '#', 'x', '#', '#', '#', '#', 'x'},
new[] {'#', '#', '#', '#', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#', 'x', 'e', '#', '#', '#', '#'},
new[] {'#', '#', '#', '#', 'x', 'x', '#', '#', '#', '#', '#', '#', '#', '#', 'x', '#', '#', '#', '#', '#'},
new[] {'x', 'x', 'x', 'x', 'x', 'x', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', 'x', '#', '#', '#'},
AssertSequenceEqual(SearchQueenOrHorse(gridG), new []{8, 6});
private static void TestCalculateMaxCoins()
AssertEqual(CalculateMaxCoins(mapA, 0, 3), 11l);
AssertEqual(CalculateMaxCoins(mapB, 0, 5), -1l);
AssertEqual(CalculateMaxCoins(mapC, 0, 5), 19l);
AssertEqual(CalculateMaxCoins(mapD, 0, 5), 12l);
private static void Assert(bool value)
throw new Exception("Assertion failed");
private static void AssertEqual(object value, object expectedValue)
if (value.Equals(expectedValue))
throw new Exception($"Assertion failed expected = {expectedValue} actual = {value}");
private static void AssertSequenceEqual<T>(IEnumerable<T> value, IEnumerable<T> expectedValue)
if (ReferenceEquals(value, expectedValue))
throw new ArgumentNullException(nameof(value));
if (expectedValue is null)
throw new ArgumentNullException(nameof(expectedValue));
var valueList = value.ToList();
var expectedValueList = expectedValue.ToList();
if (valueList.Count != expectedValueList.Count)
throw new Exception($"Assertion failed expected count = {expectedValueList.Count} actual count = {valueList.Count}");
for (var i = 0; i < valueList.Count; i++)
if (!valueList[i].Equals(expectedValueList[i]))
throw new Exception($"Assertion failed expected value at {i} = {expectedValueList[i]} actual = {valueList[i]}");