From e2e2b08f56e7c172bfea91635ccc2748c17361f6 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Tue, 8 Feb 2022 18:30:34 +0300 Subject: [PATCH 01/35] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e58af8a..17e1cb0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Tprogramming_42_2020 -Master branch :) \ No newline at end of file +Ханаева Валерия From c7a4c7d962775a13cb41e31b9a8798df4882ddc9 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 16:07:17 +0300 Subject: [PATCH 02/35] Create CyclicShift --- module3/CyclicShift | 74 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 module3/CyclicShift diff --git a/module3/CyclicShift b/module3/CyclicShift new file mode 100644 index 0000000..90d10b0 --- /dev/null +++ b/module3/CyclicShift @@ -0,0 +1,74 @@ +using System; +using System.Linq; + +namespace CourseApp.Module3 +{ + public class CyclicShift + { + public static int Rabin_Karp(string f, string n) + { + if (f == n) + { + return 0; + } + + n = string.Concat(Enumerable.Repeat(n, 2)); + + long w = 13; + long b = 1; + long t = 100000000; + + long first_hash = 0; + long second_hash = 0; + long xt = 1; + + foreach (char i in v.Reverse()) + { + first_hash = (first_hash + (i * b)) % t; + b = (b * w) % t; + } + + b = 1; + + for (int i = v.Length - 1; i >= 0; i--) + { + second_hash = (second_hash + (n[i] * b)) % t; + b = (b * w) % t; + } + + for (int i = 0; i < f.Length - 1; i++) + { + xt = (xt * w) % t; + } + + for (int i = 1; i < n.Length - f.Length + 1; i++) + { + if (first_hash == second_hash) + { + return i - 1; + } + + second_hash = w * (second_hash - (n[i - 1] * xt)); + second_hash += n[i + f.Length - 1]; + second_hash = second_hash % t; + + if ((second_hash < 0 && t > 0) || (second_hash > 0 && t < 0)) + { + second_hash += t; + } + } + + return -1; + } + + public static void EnterValues() + { + string r = Console.ReadLine(); + string q = Console.ReadLine(); + + int result = Rabin_Karp(r, q); + + Console.WriteLine(result); + } + } +} From c2b43d3f94eefcab83b1a7aec46f9b790706020c Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 16:07:49 +0300 Subject: [PATCH 03/35] Create CircularStr --- module3/CircularStr | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 module3/CircularStr diff --git a/module3/CircularStr b/module3/CircularStr new file mode 100644 index 0000000..0706d50 --- /dev/null +++ b/module3/CircularStr @@ -0,0 +1,45 @@ +using System; + +namespace CourseApp.Module3 +{ + public class CircularStr + { + public static int[] Prefix_function(string a) + { + int[] res = new int[a.Length]; + res[0] = 0; + + for (int i = 0; i < a.Length - 1; i++) + { + int j = res[i]; + + while (j > 0 && a[i + 1] != a[j]) + { + j = res[j - 1]; + } + + if (a[i + 1] == a[j]) + { + res[i + 1] = j + 1; + } + else + { + res[i + 1] = 0; + } + } + + return res; + } + + public static void EnterVal() + { + string a = Console.ReadLine(); + + int[] prefixs = Prefix_function(a); + + int result = a.Length - prefixs[a.Length - 1]; + + Console.WriteLine(result); + } + } +} From 0571e8d9f31936b14cfb116517a67e2c8e51adcb Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 16:10:19 +0300 Subject: [PATCH 04/35] Create FindSubstr --- module3/FindSubstr | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 module3/FindSubstr diff --git a/module3/FindSubstr b/module3/FindSubstr new file mode 100644 index 0000000..eb55b41 --- /dev/null +++ b/module3/FindSubstr @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module3 +{ + public class FindSubstr + { + public static long Get_hash(string w, int b, int q, int t) + { + long res = 0; + for (int i = 0; i < b; i++) + { + res = ((res * t) + w[i]) % q; + } + + return res; + } + + public static void Rabin_karp(string e, string k, int o, int v) + { + long ht = Get_hash(k, k.Length, o, v); + + long hs = Get_hash(e, k.Length, o, v); + + long xt = 1; + + for (int i = 0; i < k.Length; i++) + { + xt = (xt * v) % o; + } + + for (int i = 0; i <= e.Length - k.Length; i++) + { + if (ht == hs) + { + Console.Write("{0} ", i); + } + + if (i + k.Length < e.Length) + { + hs = ((hs * v) - (e[i] * xt) + e[i + k.Length]) % o; + hs = (hs + o) % o; + } + } + } + + public static void EnterValues() + { + string e = Console.ReadLine(); + string k = Console.ReadLine(); + + int o = 67953405; + int v = 26; + + Rabin_karp(e, k, o, v); + } + } +} From a9936dae8df6c902102371d56a5b4c7b6d6864b8 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 16:11:17 +0300 Subject: [PATCH 05/35] Create PeriodStr --- module3/PeriodStr | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 module3/PeriodStr diff --git a/module3/PeriodStr b/module3/PeriodStr new file mode 100644 index 0000000..233a323 --- /dev/null +++ b/module3/PeriodStr @@ -0,0 +1,52 @@ +using System; + +namespace CourseApp.Module3 +{ + public class PeriodStr + { + public static int[] PrefixFunc(string k) + { + int[] res = new int[k.Length]; + res[0] = 0; + + for (int i = 0; i < k.Length - 1; i++) + { + int j = res[i]; + + while (j > 0 && k[i + 1] != k[j]) + { + j = res[j - 1]; + } + + if (k[i + 1] == k[j]) + { + res[i + 1] = j + 1; + } + else + { + res[i + 1] = 0; + } + } + + return res; + } + + public static void EnterVal() + { + string k = Console.ReadLine(); + + int[] prefixs = PrefixFunc(k); + + int result = k.Length - prefixs[k.Length - 1]; + + if (k.Length % result == 0) + { + Console.WriteLine(k.Length / result); + } + else + { + Console.WriteLine(1); + } + } + } +} From 20c4a630a9415c43c2378acd9f8fd36c71861568 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 16:14:25 +0300 Subject: [PATCH 06/35] Create MinSeg --- module4/MinSeg | 137 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 module4/MinSeg diff --git a/module4/MinSeg b/module4/MinSeg new file mode 100644 index 0000000..36693dd --- /dev/null +++ b/module4/MinSeg @@ -0,0 +1,137 @@ +using System; + +public class MinSeg +{ + public static void GetMinSeg() + { + string sFirst = Console.ReadLine(); + string[] sFirstVal = sFirst.Split(' '); + int[] arrayFirst = new int[2]; + for (int i = 0; i < 2; i++) + { + arrayFirst[i] = int.Parse(sFirstVal[i]); + } + + string sSecond = Console.ReadLine(); + string[] sSecondVal = sSecond.Split(' '); + int[] arraySecond = new int[arrayFirst[0]]; + for (int i = 0; i < arrayFirst[0]; i++) + { + arraySecond[i] = int.Parse(sSecondVal[i]); + } + + for (int i = 0; i < arrayFirst[1]; i++) + { + while (Deque.Empty() == false && arraySecond[i] < arraySecond[Deque.Front()]) + { + Deque.Pop_Front(); + } + + Deque.Push_Front(i); + } + + for (int i = arrayFirst[1]; i < arrayFirst[0]; i++) + { + Console.WriteLine(arraySecond[Deque.Back()]); + + while (Deque.Empty() == false && Deque.Back() <= i - arrayFirst[1]) + { + Deque.Pop_Back(); + } + + while (Deque.Empty() == false && arraySecond[i] < arraySecond[Deque.Front()]) + { + Deque.Pop_Front(); + if (Deque.Size() == 0) + { + Deque.Clear(); + } + } + + Deque.Push_Front(i); + } + + Console.WriteLine(arraySecond[Deque.Back()]); + } + + private class Deque + { + private static int[] buffer = new int[100000001]; + private static int front = 0; + private static int back = 100000001 - 1; + private static int size = 0; + + public static void Push_Back(int t) + { + back++; + if (back == 100000001) + { + back = 0; + } + + buffer[back] = t; + size++; + } + + public static void Push_Front(int t) + { + front--; + if (front < 0) + { + front = 100000001 - 1; + } + + buffer[front] = t; + size++; + } + + public static void Pop_Back() + { + back--; + if (back < 0) + { + back = 100000001 - 1; + } + + size--; + } + + public static void Pop_Front() + { + front++; + if (front == 100000001) + { + front = 0; + } + + size--; + } + + public static void Clear() + { + front = 0; + back = 100000001 - 1; + size = 0; + } + + public static int Front() + { + return buffer[front]; + } + + public static int Back() + { + return buffer[back]; + } + + public static int Size() + { + return size; + } + + public static bool Empty() + { + return size == 0; + } + } +} From b6907e61d5b1cbff15cfb81419b1a2962b507889 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 16:15:59 +0300 Subject: [PATCH 07/35] Create NearSmall --- module4/NearSmall | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 module4/NearSmall diff --git a/module4/NearSmall b/module4/NearSmall new file mode 100644 index 0000000..2cd7715 --- /dev/null +++ b/module4/NearSmall @@ -0,0 +1,81 @@ +using System; + +public class NearSmal +{ + public static void GetNearestSmaller() + { + var numElem = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sVal = s.Split(' '); + var arrElem = new int[numElem]; + var ansArr = new int[numElem]; + for (int i = 0; i < numElem; i++) + { + arrElem[i] = int.Parse(sVal[i]); + } + + int ind = numElem - 1; + while (ind >= 0) + { + while (Stack.Empty() == false && arrElem[Stack.Back()] >= arrElem[ind]) + { + Stack.Pop(); + } + + if (Stack.Empty() == true) + { + ansArr[ind] = -1; + } + else + { + ansArr[ind] = Stack.Back(); + } + + Stack.Push(ind); + + ind--; + } + + for (int i = 0; i < numElem; i++) + { + Console.Write(ansArr[i] + " "); + } + } + + private class Stack + { + private static int[] buff = new int[100000001]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buff[top] = a; + } + + public static void Pop() + { + top--; + } + + public static int Size() + { + return top + 1; + } + + public static bool Empty() + { + return top == -1; + } + + public static void Clear() + { + top = -1; + } + + public static int Back() + { + return buff[top]; + } + } +} From 0c7ac590a485432185cd6a430b623ca012dd1e06 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 16:16:44 +0300 Subject: [PATCH 08/35] Create Psp --- module4/Psp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 module4/Psp diff --git a/module4/Psp b/module4/Psp new file mode 100644 index 0000000..8b9f4e8 --- /dev/null +++ b/module4/Psp @@ -0,0 +1,68 @@ +using System; + +namespace CourseApp.Module4 +{ + public class Psp + { + public static void GetPsp() + { + string parenthes = Console.ReadLine(); + int count = 0; + + for (int i = 0; i < parenthes.Length; i++) + { + if (parenthes[i] == '(') + { + Stack.Push(parenthes[i]); + } + else if (Stack.Empty() == false && parenthes[i] == ')') + { + Stack.Pop(); + } + else + { + count += 1; + } + } + + Console.WriteLine(count + Stack.Size()); + } + + private class Stack + { + private static int[] buf = new int[100000001]; + private static int tp = -1; + + public static void Push(int a) + { + tp++; + buf[tp] = a; + } + + public static void Pop() + { + tp--; + } + + public static int Size() + { + return tp + 1; + } + + public static bool Empty() + { + return tp == -1; + } + + public static void Clr() + { + tp = -1; + } + + public static int Bak() + { + return buf[tp]; + } + } + } +} From 4f710fac001e5f573684d37f5e7e49245ee50a10 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 16:17:54 +0300 Subject: [PATCH 09/35] Create SORT --- module4/SORT | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 module4/SORT diff --git a/module4/SORT b/module4/SORT new file mode 100644 index 0000000..aaccf36 --- /dev/null +++ b/module4/SORT @@ -0,0 +1,94 @@ +using System; + +namespace CourseApp.Module4 +{ + public class SORT + { + public static void GetSorting() + { + var numbWag = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sVal = s.Split(' '); + var arrWag = new int[numbWag]; + var ansArr = new int[numbWag]; + for (int i = 0; i < numbWag; i++) + { + arrWag[i] = int.Parse(sVal[i]); + } + + int megaKrut = 0; + int iamGrut = 0; + + while (megaKrut != numbWag) + { + if (Stack.Empty() == true || (iamGrut < numbWag && arrWag[iamGrut] < Stack.Back())) + { + Stack.Push(arrWag[iamGrut]); + iamGrut++; + } + else + { + ansArr[megaKrut] += Stack.Back(); + Stack.Pop(); + megaKrut++; + } + } + + bool isAnswer = true; + + for (int i = 0; i < ansArr.Length - 1; i++) + { + if (ansArr[i] > ansArr[i + 1]) + { + isAnswer = false; + } + } + + if (isAnswer == true) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + private class Stack + { + private static int[] buffer = new int[100000001]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buffer[top] = a; + } + + public static void Pop() + { + top--; + } + + public static int Size() + { + return top + 1; + } + + public static bool Empty() + { + return top == -1; + } + + public static void Clear() + { + top = -1; + } + + public static int Back() + { + return buffer[top]; + } + } + } +} From bcf1edd954ecc974d7690ec83932a0f8e16ea77f Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 17:17:18 +0300 Subject: [PATCH 10/35] Create BinarTreeBranch --- module5/BinarTreeBranch | 139 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 module5/BinarTreeBranch diff --git a/module5/BinarTreeBranch b/module5/BinarTreeBranch new file mode 100644 index 0000000..5211c51 --- /dev/null +++ b/module5/BinarTreeBranch @@ -0,0 +1,139 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module5.Task_1 +{ + public class BinarTreeBranch + { + private Node root; + private List size; + + public static void BinaryTreeBranchMethod() + { + string s = Console.ReadLine(); + + string[] sValues = s.Split(' '); + + var tree = new BinarTreeBranch(); + + for (int i = 0; i < sValues.Length - 1; i++) + { + tree.Insert(int.Parse(sValues[i])); + } + + tree.FindLastElem(); + } + + public void Insert(int n) + { + root = InnerInsert(n, root); + } + + public List GetValues() + { + size = new List(); + InnerTraversal(root); + return size; + } + + private bool InnerFind(int n, Node root) + { + if (root == null) + { + return false; + } + + if (n == root.Data) + { + return true; + } + else if (n < root.Data) + { + return InnerFind(n, root.Left); + } + else + { + return InnerFind(n, root.Right); + } + } + + public static void BinaryTreeMethod() + { + throw new NotImplementedException(); + } + + private bool Find(int n) + { + return InnerFind(n, root); + } + + private void FindLastElem() + { + LastElem(root); + } + + private void LastElem(Node value) + { + if (value == null) + { + return; + } + + LastElem(value.Left); + + if ((value.Left != null && value.Right == null) || (value.Right != null && value.Left == null)) + { + Console.WriteLine(value.Data); + } + + LastElem(value.Right); + } + + private Node InnerInsert(int n, Node root) + { + if (root == null) + { + return new Node(n); + } + + if (root.Data > n) + { + root.Left = InnerInsert(n, root.Left); + } + else if (root.Data < n) + { + root.Right = InnerInsert(n, root.Right); + } + + return root; + } + + private void InnerTraversal(Node node) + { + if (node == null) + { + return; + } + + InnerTraversal(node.Left); + size.Add(node.Data); + InnerTraversal(node.Right); + } + + internal class Node + { + public Node(int n) + { + Data = n; + Left = null; + Right = null; + } + + public Node Left { get; set; } + + public Node Right { get; set; } + + public int Data { get; set; } + } + } +} From bafb9f3e8841782293718fe28b088f60ac1bb5f3 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 17:25:06 +0300 Subject: [PATCH 11/35] Create IsTreeBalanced --- module5/IsTreeBalanced | 90 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 module5/IsTreeBalanced diff --git a/module5/IsTreeBalanced b/module5/IsTreeBalanced new file mode 100644 index 0000000..5cecbe1 --- /dev/null +++ b/module5/IsTreeBalanced @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module5.Task_1 +{ + public class IsTreeBalanced + { + private Node root; + + public static void IsTreeBalancedMethod() + { + string s = Console.ReadLine(); + + string[] values = s.Split(' '); + + var tree = new IsTreeBalanced(); + + for (int i = 0; i < values.Length - 1; i++) + { + tree.Insert(int.Parse(values[i])); + } + + if (tree.IsBalanced(tree.root)) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + public void Insert(int data) + { + root = InnerInsert(data, root); + } + + private Node InnerInsert(int data, Node root) + { + if (root == null) + { + return new Node(data); + } + + if (root.Data > data) + { + root.Left = InnerInsert(data, root.Left); + } + else if (root.Data < data) + { + root.Right = InnerInsert(data, root.Right); + } + + return root; + } + + private bool IsBalanced(Node node) + { + int lh; + + int rh; + + if (node == null) + { + return true; + } + + lh = Height(node.Left); + rh = Height(node.Right); + + if (Math.Abs(lh - rh) <= 1 && IsBalanced(node.Left) + && IsBalanced(node.Right)) + { + return true; + } + + return false; + } + + private int Height(Node node) + { + if (node == null) + { + return 0; + } + + return 1 + Math.Max(Height(node.Left), Height(node.Right)); + } + } +} From 2c6c6976741790838666e912b0928e17db0169e3 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 18:50:02 +0300 Subject: [PATCH 12/35] Create Bubble_Sort --- module2/Bubble_Sort | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 module2/Bubble_Sort diff --git a/module2/Bubble_Sort b/module2/Bubble_Sort new file mode 100644 index 0000000..bf9097c --- /dev/null +++ b/module2/Bubble_Sort @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class Bubble_Sort + { + public static void Bubble_Sort_Method() + { + int n = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + int[] array = new int[n]; + for (int i = 0; i < n; i++) + { + array[i] = int.Parse(sValues[i]); + } + + bool swap = false; + for (int i = 0; i < array.Length - 1; i++) + { + for (int j = 0; j < array.Length - i - 1; j++) + { + if (array[j] > array[j + 1]) + { + (array[j], array[j + 1]) = (array[j + 1], array[j]); + string result = string.Join(" ", array); + Console.WriteLine(result); + swap = true; + } + } + } + + if (swap == false) + { + Console.WriteLine(0); + } + } + public static void Main() + { + Bubble_Sort_Method(); + } + } +} From 9c0f661bc24f69b605275e9f0b658f3c533070ef Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 19:10:40 +0300 Subject: [PATCH 13/35] Create InversionCount --- module2/InversionCount | 82 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 module2/InversionCount diff --git a/module2/InversionCount b/module2/InversionCount new file mode 100644 index 0000000..007c4fd --- /dev/null +++ b/module2/InversionCount @@ -0,0 +1,82 @@ +using System; + +namespace CourseApp.Module2 +{ + public class InversionCount + { + private static long inversionCount = 0; + + public static void Start() + { + int v = int.Parse(Console.ReadLine()); + if (v > 1) + { + string temp = Console.ReadLine(); + string[] values = temp.Split(' '); + int[] array = new int[v]; + for (int i = 0; i < values.Length; i++) + { + array[i] = int.Parse(values[i]); + } + + int[] result = Sort(array, 0, v); + Console.WriteLine(inversionCount); + } + else + { + Console.WriteLine(0); + } + } + + public static int[] Sort(int[] array, int firstInd, int lastInd) + { + if (lastInd - firstInd == 1) + { + int[] res = new int[1]; + res[0] = array[firstInd]; + return res; + } + + int w = (firstInd + lastInd) / 2; + + int[] left = Sort(array, firstInd, w); + int[] right = Sort(array, w, lastInd); + + return Merge(left, right); + } + + public static int[] Merge(int[] left, int[] right) + { + int i = 0; + int j = 0; + int[] result = new int[left.Length + right.Length]; + + for (int n = 0; n < result.Length; n++) + { + if (i == left.Length) + { + result[n] = right[j]; + j++; + } + else if (j == right.Length) + { + result[n] = left[i]; + i++; + } + else if (left[i] <= right[j]) + { + result[n] = left[i]; + i++; + } + else + { + result[n] = right[j]; + j++; + inversionCount += left.Length - i; + } + } + + return result; + } + } +} From 8e71ea776b872233d9c3318af08211d98e2cb6fc Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 19:12:47 +0300 Subject: [PATCH 14/35] Create MergeSort --- module2/MergeSort | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 module2/MergeSort diff --git a/module2/MergeSort b/module2/MergeSort new file mode 100644 index 0000000..48d50e4 --- /dev/null +++ b/module2/MergeSort @@ -0,0 +1,82 @@ +using System; + +namespace CourseApp.Module2 +{ + public class MergeSort + { + public static void Main() + { + MergeSortMethod(); + } + public static void MergeSortMethod() + { + int[] arr = InputParse(); + + int[] sortedArr = ArrSort(ref arr, 0, arr.Length); + + Console.WriteLine("{0}", string.Join(" ", sortedArr)); + } + + private static int[] Merge(ref int[] left, ref int[] right) + { + int i = 0; + int j = 0; + int[] add = new int[left.Length + right.Length]; + for (int k = 0; k < add.Length; k++) + { + if (i == left.Length) + { + add[k] = right[j]; + j++; + } + else if (j == right.Length || left[i] <= right[j]) + { + add[k] = left[i]; + i++; + } + else + { + add[k] = right[j]; + j++; + } + } + + return add; + } + + private static int[] ArrSort(ref int[] arr, int begin, int end) + { + if (end - begin == 1) + { + int[] res = new int[1]; + res[0] = arr[begin]; + return res; + } + + int mid = (begin + end) / 2; + + int[] left = ArrSort(ref arr, begin, mid); + int[] right = ArrSort(ref arr, mid, end); + + int[] sort = Merge(ref left, ref right); + + Console.WriteLine("{0} {1} {2} {3}", begin + 1, end, sort[0], sort[^1]); + + return Merge(ref left, ref right); + } + + private static int[] InputParse() + { + int n = int.Parse(Console.ReadLine()); + int[] arr = new int[n]; + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + for (int i = 0; i < n; i++) + { + arr[i] = int.Parse(sValues[i]); + } + + return arr; + } + } +} From c350b7aaf7f861c5645d49b1207ae29703a99fbb Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 19:18:39 +0300 Subject: [PATCH 15/35] Create NumberDif --- module2/NumberDif | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 module2/NumberDif diff --git a/module2/NumberDif b/module2/NumberDif new file mode 100644 index 0000000..c007e2a --- /dev/null +++ b/module2/NumberDif @@ -0,0 +1,77 @@ +using System; + +namespace CourseApp.Module2 +{ + public class NumberDif + { + public static void Count_Diff_Method() + { + int number = int.Parse(Console.ReadLine()); + string[] value = Console.ReadLine().Split(' '); + int[] array = new int[number]; + + for (int i = 0; i < number; ++i) + { + array[i] = int.Parse(value[i]); + } + + QuickSort(array, 0, number); + + int count = 0; + + for (int i = 1; i < number; i++) + { + if (array[i] != array[i - 1]) + { + count++; + } + } + + Console.Write(count + 1); + } + + public static int Partition(int[] array, int leftInd, int rightInd) + { + int i = leftInd; + int j = rightInd - 1; + int w = array[(leftInd + rightInd - 1) / 2]; + + while (i <= j) + { + while (array[i] < w) + { + i++; + } + + while (array[j] > w) + { + j--; + } + + if (i >= j) + { + break; + } + + (array[i], array[j]) = (array[j], array[i]); + i++; + j--; + } + + return j + 1; + } + + public static void QuickSort(int[] array, int leftInd, int rightInd) + { + if (rightInd - leftInd <= 1) + { + return; + } + + int v = Partition(array, leftInd, rightInd); + + QuickSort(array, leftInd, v); + QuickSort(array, v, rightInd); + } + } +} From e101e13db46adfe701967eff9911c080b5a0c534 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 19:19:52 +0300 Subject: [PATCH 16/35] Create PairSort --- module2/PairSort | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 module2/PairSort diff --git a/module2/PairSort b/module2/PairSort new file mode 100644 index 0000000..38b17f1 --- /dev/null +++ b/module2/PairSort @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class PairSort + { + public static void Main() + { + PairSortMethod(); + } + public static void PairSortMethod() + { + int[,] arr = InputParse(); + + int n = arr.Length / 2; + + SortArrFirst(ref arr, ref n); + SortArrSecond(ref arr, ref n); + Show(ref arr, ref n); + } + + private static void SortArrFirst(ref int[,] arr, ref int n) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n - i - 1; j++) + { + if (arr[j + 1, 1] > arr[j, 1]) + { + Swap(ref arr[j, 0], ref arr[j + 1, 0]); + Swap(ref arr[j, 1], ref arr[j + 1, 1]); + } + } + } + } + + private static void SortArrSecond(ref int[,] arr, ref int n) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n - i - 1; j++) + { + if (arr[j + 1, 1] == arr[j, 1] && arr[j + 1, 0] < arr[j, 0]) + { + Swap(ref arr[j, 0], ref arr[j + 1, 0]); + Swap(ref arr[j, 1], ref arr[j + 1, 1]); + } + } + } + } + + private static int[,] InputParse() + { + int n = int.Parse(Console.ReadLine()); + int[,] arr = new int[n, 2]; + for (int i = 0; i < n; i++) + { + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + for (int j = 0; j < 2; j++) + { + arr[i, j] = int.Parse(sValues[j]); + } + } + + return arr; + } + + private static void Show(ref int[,] arr, ref int n) + { + string result = null; + for (int i = 0; i < n; i++) + { + result = arr[i, 0] + " " + arr[i, 1]; + Console.WriteLine(result); + } + } + + private static void Swap(ref int left, ref int right) + { + int temp = right; + right = left; + left = temp; + } + } +} From aa9843b7104fb908a8303166d84672d63fe77294 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 19:20:34 +0300 Subject: [PATCH 17/35] Create RadixSort --- module2/RadixSort | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 module2/RadixSort diff --git a/module2/RadixSort b/module2/RadixSort new file mode 100644 index 0000000..d6e2f43 --- /dev/null +++ b/module2/RadixSort @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class RadixSort + { + public static void RadSort(string[] arr_string) + { + int numb_phaze = 1; + int rank = arr_string[0].Length; + + Console.WriteLine("Initial array:"); + Console.WriteLine("{0}", string.Join(", ", arr_string)); + + foreach (var i in Enumerable.Range(0, Convert.ToInt32(Math.Ceiling(Convert.ToDouble(-1 - (rank - 1)) / -1))).Select(x_1 => rank - 1 + (x_1 * -1))) + { + Console.WriteLine("**********"); + Console.WriteLine("Phase {0}", numb_phaze); + ulong n; + List[] arrayList = new List[10]; + for (n = 0; n < 10; n++) + { + arrayList[n] = new List(); + } + + for (int j = 0; j < arr_string.Length; j++) + { + int k = int.Parse(arr_string[j].Substring(rank - numb_phaze, 1)); + arrayList[k].Add(arr_string[j]); + } + + for (n = 0; n < 10; n++) + { + if (arrayList[n].Count == 0) + { + Console.WriteLine("Bucket " + n + ": empty"); + } + else + { + Console.WriteLine("Bucket " + n + ": {0}", string.Join(", ", arrayList[n])); + } + } + + int l = 0; + + for (n = 0; n < 10; n++) + { + for (int j = 0; j < arrayList[n].Count; j++) + { + arr_string[l] = arrayList[n][j]; + l++; + } + } + + numb_phaze++; + } + + Console.WriteLine("**********"); + Console.WriteLine("Sorted array:"); + Console.Write("{0}", string.Join(", ", arr_string)); + } + + public static void Start() + { + ulong m = ulong.Parse(Console.ReadLine()); + string[] arr_string = new string[m]; + for (ulong i = 0; i < m; i++) + { + arr_string[i] = Console.ReadLine(); + } + + RadSort(arr_string); + } + } +} From 5c8dd2e80001c79e022948136e975ee5e3b20a45 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 19:21:20 +0300 Subject: [PATCH 18/35] Create Warehouse --- module2/Warehouse | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 module2/Warehouse diff --git a/module2/Warehouse b/module2/Warehouse new file mode 100644 index 0000000..7cb372e --- /dev/null +++ b/module2/Warehouse @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class Warehouse + { + public static void Count_Sort(int[] orders_arr, int[] products_arr, int n) + { + int[] arr = new int[n + 1]; + for (int i = 0; i < orders_arr.Length; i++) + { + arr[orders_arr[i]]++; + } + + int pos = 0; + int index = 0; + int[] ord = new int[n]; + string[] answer = new string[n]; + for (int i = 0; i < arr.Length; i++) + { + if (arr[i] != 0) + { + ord[pos++] = arr[i]; + if (products_arr[index] >= ord[index]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + + index++; + } + } + } + + public static void Input_Values() + { + int products_len = int.Parse(Console.ReadLine()); + string products_string = Console.ReadLine(); + string[] parseValues = products_string.Split(' '); + int[] products_arr = new int[products_len]; + for (int i = 0; i < products_len; i++) + { + products_arr[i] = int.Parse(parseValues[i]); + } + + int orders_len = int.Parse(Console.ReadLine()); + string orders_string = Console.ReadLine(); + parseValues = orders_string.Split(' '); + int[] orders_arr = new int[orders_len]; + for (int i = 0; i < orders_len; i++) + { + orders_arr[i] = int.Parse(parseValues[i]); + } + + Count_Sort(orders_arr, products_arr, products_len); + } + } +} From e246850c539d8a59ab0ca1107034570c5d7c5467 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Fri, 3 Jun 2022 19:24:05 +0300 Subject: [PATCH 19/35] Create AplusB --- module1/AplusB | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 module1/AplusB diff --git a/module1/AplusB b/module1/AplusB new file mode 100644 index 0000000..d34cbd3 --- /dev/null +++ b/module1/AplusB @@ -0,0 +1,18 @@ +using System; + +namespace CourseApp.Module1 +{ + public class AplusB + { + public static void Calculate() + { + string s = Console.ReadLine(); + string[] values = s.Split(' '); + int a = int.Parse(values[0]); + int b = int.Parse(values[1]); + Console.WriteLine($"{a + b}"); + } + } +} +AplusB.cs +1 кб From 1c97d1963ff4ae78b5f15e31b24271103dcf9ae6 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:34:44 +0300 Subject: [PATCH 20/35] Create InversionCountTest --- CourseApp.Tests/Module2/InversionCountTest | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 CourseApp.Tests/Module2/InversionCountTest diff --git a/CourseApp.Tests/Module2/InversionCountTest b/CourseApp.Tests/Module2/InversionCountTest new file mode 100644 index 0000000..05df36f --- /dev/null +++ b/CourseApp.Tests/Module2/InversionCountTest @@ -0,0 +1,52 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class InversionCountTest : IDisposable + { + private const string Inp1 = @"1 +1"; + + private const string Out1 = @"0"; + + private const string Inp2 = @"5 +5 4 3 2 1"; + + private const string Out2 = @"10"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + InversionCount.Start(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From b5eda64683770d9113600f2dde4e34b4741e3c39 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:35:35 +0300 Subject: [PATCH 21/35] Create MergeTest --- CourseApp.Tests/Module2/MergeTest | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 CourseApp.Tests/Module2/MergeTest diff --git a/CourseApp.Tests/Module2/MergeTest b/CourseApp.Tests/Module2/MergeTest new file mode 100644 index 0000000..cdefa3d --- /dev/null +++ b/CourseApp.Tests/Module2/MergeTest @@ -0,0 +1,63 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class MergeSortTest : IDisposable + { + private const string Inp1 = @"5 +5 4 3 2 1"; + + private const string Out1 = @"1 2 4 5 +4 5 1 2 +3 5 1 3 +1 5 1 5 +1 2 3 4 5"; + + private const string Inp2 = @"1 +1"; + + private const string Out2 = @"1"; + + private const string Inp3 = @"2 +3 1"; + + private const string Out3 = @"1 2 1 3 +1 3"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + [InlineData(Inp3, Out3)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + MergeSort.MergeSortMethod(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From d95065ff1317e0dc8ba0565c9d157c1aee70563a Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:36:23 +0300 Subject: [PATCH 22/35] Create NumberDifTest --- CourseApp.Tests/Module2/NumberDifTest | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 CourseApp.Tests/Module2/NumberDifTest diff --git a/CourseApp.Tests/Module2/NumberDifTest b/CourseApp.Tests/Module2/NumberDifTest new file mode 100644 index 0000000..bafda0d --- /dev/null +++ b/CourseApp.Tests/Module2/NumberDifTest @@ -0,0 +1,46 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class NumberDifTest : IDisposable + { + private const string Inp1 = @"5 +1 0 1 2 0"; + + private const string Out1 = @"3"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + NumberDif.Count_Diff_Method(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From ec135c8466013212be42373c798b659ce8a09860 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:37:05 +0300 Subject: [PATCH 23/35] Create PairSortTest --- CourseApp.Tests/Module2/PairSortTest | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 CourseApp.Tests/Module2/PairSortTest diff --git a/CourseApp.Tests/Module2/PairSortTest b/CourseApp.Tests/Module2/PairSortTest new file mode 100644 index 0000000..d0fe17b --- /dev/null +++ b/CourseApp.Tests/Module2/PairSortTest @@ -0,0 +1,60 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class PairSortTest : IDisposable + { + private const string Inp1 = @"3 +20 80 +30 90 +25 90"; + + private const string Out1 = @"25 90 +30 90 +20 80"; + + private const string Inp2 = @"3 +101 80 +305 90 +200 14"; + + private const string Out2 = @"305 90 +101 80 +200 14"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + PairSort.PairSortMethod(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From ed6a061e035111293a914f47b68ba0ba6429e8da Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:38:41 +0300 Subject: [PATCH 24/35] Create RadixSortTest --- CourseApp.Tests/Module2/RadixSortTest | 82 +++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 CourseApp.Tests/Module2/RadixSortTest diff --git a/CourseApp.Tests/Module2/RadixSortTest b/CourseApp.Tests/Module2/RadixSortTest new file mode 100644 index 0000000..cd6b731 --- /dev/null +++ b/CourseApp.Tests/Module2/RadixSortTest @@ -0,0 +1,82 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class RadixSortTest : IDisposable + { + private const string Inp1 = @"9 +12 +32 +45 +67 +98 +29 +61 +35 +09"; + + private const string Out1 = @"Initial array: +12, 32, 45, 67, 98, 29, 61, 35, 09 +** +Phase 1 +Bucket 0: empty +Bucket 1: 61 +Bucket 2: 12, 32 +Bucket 3: empty +Bucket 4: empty +Bucket 5: 45, 35 +Bucket 6: empty +Bucket 7: 67 +Bucket 8: 98 +Bucket 9: 29, 09 +** +Phase 2 +Bucket 0: 09 +Bucket 1: 12 +Bucket 2: 29 +Bucket 3: 32, 35 +Bucket 4: 45 +Bucket 5: empty +Bucket 6: 61, 67 +Bucket 7: empty +Bucket 8: empty +Bucket 9: 98 +** +Sorted array: +09, 12, 29, 32, 35, 45, 61, 67, 98"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + RadixSort.Start(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From cbbef55ae89a2a3d1c785163a510ccbcaec5b24d Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:39:40 +0300 Subject: [PATCH 25/35] Create WarehouseTest --- CourseApp.Tests/Module2/WarehouseTest | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 CourseApp.Tests/Module2/WarehouseTest diff --git a/CourseApp.Tests/Module2/WarehouseTest b/CourseApp.Tests/Module2/WarehouseTest new file mode 100644 index 0000000..2349487 --- /dev/null +++ b/CourseApp.Tests/Module2/WarehouseTest @@ -0,0 +1,52 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class WarehouseTest : IDisposable + { + private const string Inp1 = @"5 +1 50 3 4 3 +16 +1 2 3 4 5 1 3 3 4 5 5 5 5 5 4 5"; + + private const string Out1 = @"yes +no +no +no +yes"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + Warehouse.Input_Values(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From d8cbabb92c57f00b564431552e7347507b5337f4 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:41:48 +0300 Subject: [PATCH 26/35] Create CircularStrTest --- CourseApp.Tests/Module3/CircularStrTest | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 CourseApp.Tests/Module3/CircularStrTest diff --git a/CourseApp.Tests/Module3/CircularStrTest b/CourseApp.Tests/Module3/CircularStrTest new file mode 100644 index 0000000..b6daba3 --- /dev/null +++ b/CourseApp.Tests/Module3/CircularStrTest @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CircularStrTest : IDisposable + { + private const string Inp1 = @"z"; + + private const string Out1 = @"1"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + CircularStr.EnterVal(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From 62d7bc19566869ce97c8aab4ab73fd52f4ada49d Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:42:45 +0300 Subject: [PATCH 27/35] Create CyclingShiftTest --- CourseApp.Tests/Module3/CyclingShiftTest | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 CourseApp.Tests/Module3/CyclingShiftTest diff --git a/CourseApp.Tests/Module3/CyclingShiftTest b/CourseApp.Tests/Module3/CyclingShiftTest new file mode 100644 index 0000000..37958cc --- /dev/null +++ b/CourseApp.Tests/Module3/CyclingShiftTest @@ -0,0 +1,51 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CyclingShiftTest : IDisposable + { + private const string Inp1 = @"a + b"; + + private const string Out1 = @"-1"; + + private const string Inp2 = @"zabcd +abcdz"; + + private const string Out2 = @"4"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + CyclicShift.EnterValues(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From 4bc7f7df5079cda8b20d50c51c568af016ceccbf Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:43:33 +0300 Subject: [PATCH 28/35] Create FindSubstrTest --- CourseApp.Tests/Module3/FindSubstrTest | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 CourseApp.Tests/Module3/FindSubstrTest diff --git a/CourseApp.Tests/Module3/FindSubstrTest b/CourseApp.Tests/Module3/FindSubstrTest new file mode 100644 index 0000000..43e68c6 --- /dev/null +++ b/CourseApp.Tests/Module3/FindSubstrTest @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class FindSubstrTest : IDisposable + { + private const string Inp1 = @"ababbababa +aba"; + + private const string Out1 = @"0 5 7 "; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + FindSubstr.EnterValues(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From 6146160852e98d6e6016be4be68713e9e7d7d53b Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:44:36 +0300 Subject: [PATCH 29/35] Create PeriodStrTest --- CourseApp.Tests/Module3/PeriodStrTest | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 CourseApp.Tests/Module3/PeriodStrTest diff --git a/CourseApp.Tests/Module3/PeriodStrTest b/CourseApp.Tests/Module3/PeriodStrTest new file mode 100644 index 0000000..8501e00 --- /dev/null +++ b/CourseApp.Tests/Module3/PeriodStrTest @@ -0,0 +1,54 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class PeriodStrTest : IDisposable + { + private const string Inp1 = @"aaaaa"; + + private const string Out1 = @"5"; + + private const string Inp2 = @"abcabcabc"; + + private const string Out2 = @"3"; + + private const string Inp3 = @"abab"; + + private const string Out3 = @"2"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + [InlineData(Inp3, Out3)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + PeriodStr.EnterVal(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From cd1db19eb0cbfa0bfe604155ef6e3eb08b754641 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:45:41 +0300 Subject: [PATCH 30/35] Create MinSegTest --- CourseApp.Tests/Module4/MinSegTest | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 CourseApp.Tests/Module4/MinSegTest diff --git a/CourseApp.Tests/Module4/MinSegTest b/CourseApp.Tests/Module4/MinSegTest new file mode 100644 index 0000000..c2ac4e3 --- /dev/null +++ b/CourseApp.Tests/Module4/MinSegTest @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class MinSegTest : IDisposable + { + private const string Inp1 = @"7 3 +1 3 2 4 5 3 1"; + + private const string Out1 = @"1 +2 +2 +3 +1"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + MinSeg.GetMinSeg(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From e46348f45bf5e61e640e2773718edd0d12d0250d Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:46:21 +0300 Subject: [PATCH 31/35] Create NearSmalTest --- CourseApp.Tests/Module4/NearSmalTest | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 CourseApp.Tests/Module4/NearSmalTest diff --git a/CourseApp.Tests/Module4/NearSmalTest b/CourseApp.Tests/Module4/NearSmalTest new file mode 100644 index 0000000..14db073 --- /dev/null +++ b/CourseApp.Tests/Module4/NearSmalTest @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class NearSmalTest : IDisposable + { + private const string Inp1 = @"10 +1 2 3 2 1 4 2 5 3 1"; + + private const string Out1 = @"-1 4 3 4 -1 6 9 8 9 -1 "; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + NearSmal.GetNearestSmaller(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From a74eb03c6972b35abbd08d47517206ddda952e97 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:46:56 +0300 Subject: [PATCH 32/35] Create PspTest --- CourseApp.Tests/Module4/PspTest | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 CourseApp.Tests/Module4/PspTest diff --git a/CourseApp.Tests/Module4/PspTest b/CourseApp.Tests/Module4/PspTest new file mode 100644 index 0000000..f121402 --- /dev/null +++ b/CourseApp.Tests/Module4/PspTest @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class PspTest : IDisposable + { + private const string Inp1 = @"())(()"; + + private const string Out1 = @"2"; + + private const string Inp2 = @"))((("; + + private const string Out2 = @"5"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + Psp.GetPsp(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From 0c091235a592608da5b6bf0cb1a6f36c496acb11 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:47:44 +0300 Subject: [PATCH 33/35] Create SortTest --- CourseApp.Tests/Module4/SortTest | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 CourseApp.Tests/Module4/SortTest diff --git a/CourseApp.Tests/Module4/SortTest b/CourseApp.Tests/Module4/SortTest new file mode 100644 index 0000000..2178bff --- /dev/null +++ b/CourseApp.Tests/Module4/SortTest @@ -0,0 +1,58 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class SortTest : IDisposable + { + private const string Inp1 = @"3 +3 2 1"; + + private const string Out1 = @"YES"; + + private const string Inp2 = @"4 +4 1 3 2"; + + private const string Out2 = @"YES"; + + private const string Inp3 = @"3 +2 3 1"; + + private const string Out3 = @"NO"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + [InlineData(Inp3, Out3)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + SORT.GetSorting(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From e93c9f4cdad89f01a07d948328a9b9e9e9f15605 Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:48:48 +0300 Subject: [PATCH 34/35] Create BinarTreeBranchTest --- CourseApp.Tests/Module5/BinarTreeBranchTest | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 CourseApp.Tests/Module5/BinarTreeBranchTest diff --git a/CourseApp.Tests/Module5/BinarTreeBranchTest b/CourseApp.Tests/Module5/BinarTreeBranchTest new file mode 100644 index 0000000..7c3403b --- /dev/null +++ b/CourseApp.Tests/Module5/BinarTreeBranchTest @@ -0,0 +1,46 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using CourseApp.Module5.Task_1; + using Xunit; + + [Collection("Sequential")] + public class BinarTreeBranchTest : IDisposable + { + private const string Inp1 = @"7 3 2 1 9 5 4 6 8 0"; + + private const string Out1 = @"2 +9"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + BinarTreeBranch.BinaryTreeMethod(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} From 0dafcde4d4e2dd193c8c3ed1deec9b8817c8934a Mon Sep 17 00:00:00 2001 From: markkket <92202536+markkket@users.noreply.github.com> Date: Sat, 4 Jun 2022 18:49:18 +0300 Subject: [PATCH 35/35] Create IsTreeBalancedTest --- CourseApp.Tests/Module5/IsTreeBalancedTest | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 CourseApp.Tests/Module5/IsTreeBalancedTest diff --git a/CourseApp.Tests/Module5/IsTreeBalancedTest b/CourseApp.Tests/Module5/IsTreeBalancedTest new file mode 100644 index 0000000..eaa40f2 --- /dev/null +++ b/CourseApp.Tests/Module5/IsTreeBalancedTest @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using CourseApp.Module5.Task_1; + using Xunit; + + [Collection("Sequential")] + public class IsTreeBalancedTest : IDisposable + { + private const string Inp1 = @"7 3 2 1 9 5 4 6 8 0"; + + private const string Out1 = @"YES"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + IsTreeBalanced.IsTreeBalancedMethod(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +}