From 1ff0fcb62cfe9e541cbcfd5baaf93d5ab07d66e5 Mon Sep 17 00:00:00 2001 From: Abubakar Ahmed Date: Thu, 17 Apr 2025 19:47:47 +0530 Subject: [PATCH 1/2] Solved array methods. --- Src/BootCamp.Chapter1/ArrayOperations.cs | 93 ++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 6 deletions(-) diff --git a/Src/BootCamp.Chapter1/ArrayOperations.cs b/Src/BootCamp.Chapter1/ArrayOperations.cs index c11f7c3ea..2a6744b23 100644 --- a/Src/BootCamp.Chapter1/ArrayOperations.cs +++ b/Src/BootCamp.Chapter1/ArrayOperations.cs @@ -1,4 +1,6 @@ -namespace BootCamp.Chapter1 +using System; + +namespace BootCamp.Chapter1 { public static class ArrayOperations { @@ -10,6 +12,35 @@ public static class ArrayOperations public static void Sort(int[] array) { // ToDo: implement. + if (array is null || array.Length == 0) + return; + + var isArraySorted = IsSorted(array); + if (isArraySorted is false) + SortArray(array); + } + + private static bool IsSorted(int[] array) + { + for (int i = 0; i < array.Length - 1; i++) + { + if (array[i] > array[i + 1]) + return true; + } + return false; + } + + private static void SortArray(int[] array) + { + var length = array.Length; + for (int i = 0; i < length - 1; i++) + { + for (int j = 0; j < length - i - 1; j++) + { + if (array[j] > array[j + 1]) + (array[j + 1], array[j]) = (array[j], array[j + 1]); + } + } } /// @@ -20,6 +51,12 @@ public static void Sort(int[] array) public static void Reverse(int[] array) { // ToDo: implement. + if (array is null || array.Length == 0) + return; + for (int i = 0; i < array.Length / 2; i++) + { + (array[i], array[^(i + 1)]) = (array[^(i + 1)], array[i]); + } } /// @@ -30,7 +67,11 @@ public static void Reverse(int[] array) public static int[] RemoveLast(int[] array) { // ToDo: implement. - return array; + if (array is null || array.Length == 0) + return array; + var newArray = new int[array.Length - 1]; + Array.Copy(array, newArray, newArray.Length); + return newArray; } /// @@ -40,7 +81,11 @@ public static int[] RemoveLast(int[] array) public static int[] RemoveFirst(int[] array) { // ToDo: implement. - return array; + if (array is null || array.Length == 0) + return array; + var newArray = new int[array.Length - 1]; + array.CopyTo(newArray, 1); + return newArray; } /// @@ -52,7 +97,17 @@ public static int[] RemoveFirst(int[] array) public static int[] RemoveAt(int[] array, int index) { // ToDo: implement. - return array; + if (array is null || array.Length == 0) + return array; + if (index > array.Length - 1 || index < 0) + return array; + var newArray = new int[array.Length - 1]; + for (int i = 0, j = 0; i < array.Length; i++) + { + if (i == index) continue; + newArray[j++] = array[i]; + } + return newArray; } /// @@ -64,6 +119,12 @@ public static int[] RemoveAt(int[] array, int index) public static int[] InsertFirst(int[] array, int number) { // ToDo: implement. + if (array is null || array.Length == 0) + return new int[] { 0 }; + + var newArray = new int[array.Length]; + newArray[0] = number; + Array.Copy(array, 0, newArray, 1, array.Length); return array; } @@ -76,7 +137,12 @@ public static int[] InsertFirst(int[] array, int number) public static int[] InsertLast(int[] array, int number) { // ToDo: implement. - return array; + if (array is null || array.Length == 0) + return new int[] { 0 }; + var newArray = new int[array.Length]; + array.CopyTo(newArray, 0); + newArray[^1] = number; + return newArray; } /// @@ -89,7 +155,22 @@ public static int[] InsertLast(int[] array, int number) public static int[] InsertAt(int[] array, int number, int index) { // ToDo: implement. - return array; + if (array is null || array.Length == 0) + return new int[] { 0 }; + + var newArray = new int[array.Length]; + + if (index > newArray.Length - 1 || index < 0) + return array; + + newArray[index] = number; + for (int i = 0, j = 0; i < newArray.Length; i++) + { + if (i == index) + continue; + newArray[i] = array[j++]; + } + return newArray; } } } From 780c4bb3183f4eb0e5879e16344caa40e4ca32bb Mon Sep 17 00:00:00 2001 From: Abubakar Ahmed Date: Thu, 17 Apr 2025 21:36:38 +0530 Subject: [PATCH 2/2] used Array extention methods to solve problems --- Src/BootCamp.Chapter1/ArrayOperations.cs | 32 ++++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Src/BootCamp.Chapter1/ArrayOperations.cs b/Src/BootCamp.Chapter1/ArrayOperations.cs index 2a6744b23..457858376 100644 --- a/Src/BootCamp.Chapter1/ArrayOperations.cs +++ b/Src/BootCamp.Chapter1/ArrayOperations.cs @@ -24,10 +24,10 @@ private static bool IsSorted(int[] array) { for (int i = 0; i < array.Length - 1; i++) { - if (array[i] > array[i + 1]) - return true; + if ((array[i] > array[i + 1])) + return false; } - return false; + return true; } private static void SortArray(int[] array) @@ -84,7 +84,7 @@ public static int[] RemoveFirst(int[] array) if (array is null || array.Length == 0) return array; var newArray = new int[array.Length - 1]; - array.CopyTo(newArray, 1); + Array.Copy(array, 1, newArray, 0, newArray.Length); return newArray; } @@ -120,12 +120,14 @@ public static int[] InsertFirst(int[] array, int number) { // ToDo: implement. if (array is null || array.Length == 0) - return new int[] { 0 }; + return new int[] { number }; - var newArray = new int[array.Length]; + var newArray = new int[array.Length + 1]; newArray[0] = number; - Array.Copy(array, 0, newArray, 1, array.Length); - return array; + for (int i = 1, j = 0; i < newArray.Length; i++) + newArray[i] = array[j++]; + + return newArray; } /// @@ -138,8 +140,8 @@ public static int[] InsertLast(int[] array, int number) { // ToDo: implement. if (array is null || array.Length == 0) - return new int[] { 0 }; - var newArray = new int[array.Length]; + return new int[] { number }; + var newArray = new int[array.Length + 1]; array.CopyTo(newArray, 0); newArray[^1] = number; return newArray; @@ -155,14 +157,12 @@ public static int[] InsertLast(int[] array, int number) public static int[] InsertAt(int[] array, int number, int index) { // ToDo: implement. - if (array is null || array.Length == 0) - return new int[] { 0 }; - - var newArray = new int[array.Length]; - - if (index > newArray.Length - 1 || index < 0) + if (index < 0 || index > (array is null ? 0 : array.Length)) return array; + if (array is null || array.Length == 0) + return new int[] { number }; + var newArray = new int[array.Length + 1]; newArray[index] = number; for (int i = 0, j = 0; i < newArray.Length; i++) {