From a3c316dbfeefd4b588ef35cf089fa5b57884ec51 Mon Sep 17 00:00:00 2001 From: HiradTorabi Date: Sat, 8 Mar 2025 17:23:33 +0330 Subject: [PATCH 1/2] salam testing --- .../src/main/java/Exercises.java | 96 +++++++++++++++++-- 1 file changed, 87 insertions(+), 9 deletions(-) diff --git a/AP1403 - Algorithms/src/main/java/Exercises.java b/AP1403 - Algorithms/src/main/java/Exercises.java index 15a2133..18fb943 100644 --- a/AP1403 - Algorithms/src/main/java/Exercises.java +++ b/AP1403 - Algorithms/src/main/java/Exercises.java @@ -8,8 +8,20 @@ public class Exercises { note: you should return the indices in ascending order and every array's solution is unique */ - public int[] productIndices(int[] values, int target) { - // todo + public int[] productIndices(int[] values, int target) + { + int n = values.length; + for (int i = 0; i < n; ++i) + { + for (int j = i + 1; j < n; ++j) + { + if (values[i] * values[j] == target) + { + return new int[] {i, j}; + } + } + + } return null; } @@ -24,11 +36,49 @@ public int[] productIndices(int[] values, int target) { so you should walk in that matrix in a curl and then add the numbers in order you've seen them in a 1D array */ - public int[] spiralTraversal(int[][] values, int rows, int cols) { - // todo - return null; + public int[] spiralTraversal(int[][] values, int rows, int cols) + { + if (values == null || rows == 0 || cols == 0) + { + return new int[0]; + } + int[] result = new int[rows * cols]; + int index = 0; + int top = 0, bottom = rows - 1; + int left = 0, right = cols - 1; + while (top <= bottom && left <= right) + { + for (int i = left; i <= right; i++) + { + result[index++] = values[top][i]; + } + top++; + for (int i = top; i <= bottom; i++) + { + result[index++] = values[i][right]; + } + right--; + if (top <= bottom) + { + for (int i = right; i >= left; i--) + { + result[index++] = values[bottom][i]; + } + bottom--; + } + if (left <= right) + { + for (int i = bottom; i >= top; i--) + { + result[index++] = values[i][left]; + } + left++; + } + } + return result; } + /* integer partitioning is a combinatorics problem in discreet maths the problem is to generate sum numbers which their summation is the input number @@ -53,12 +103,40 @@ public int[] spiralTraversal(int[][] values, int rows, int cols) { if you're familiar with lists and arraylists, you can also edit method's body to use them instead of array */ - public int[][] intPartitions(int n) { - // todo - return null; + public class IntegerPartition + { + public static void main(String[] args) + { + int n = 4; + int[] partition = new int[n]; + partition(n, n, partition, 0); + } + public static void partition(int n, int max, int[] partition, int index) + { + if (n == 0) + { + printPartition(partition,index); + return; + } + + for (int i = Math.min(max,n); i >= 1; i--) + { + partition[index] = i; + partition(n - i, i, partition, index + 1); + } + } + public static void printPartition(int[] partition, int length) + { + for (int i = 0; length > i; i++) + { + System.out.print(partition[i] + " "); + } + System.out.println(); + } } - public static void main(String[] args) { + public static void main(String[] args) + { // you can test your code here } } From 952c68af32458307566d902ee84477654626a62d Mon Sep 17 00:00:00 2001 From: HiradTorabi Date: Fri, 14 Mar 2025 19:14:40 +0330 Subject: [PATCH 2/2] Add files via upload --- .../AP1403 - Algorithms/pom.xml | 38 +++++ .../src/main/java/Exercises.java | 160 ++++++++++++++++++ .../src/test/java/TestPartitions.java | 65 +++++++ .../src/test/java/TestProduct.java | 37 ++++ .../src/test/java/TestTraversal.java | 66 ++++++++ .../classes/Exercises$IntegerPartition.class | Bin 0 -> 1668 bytes .../target/classes/Exercises.class | Bin 0 -> 1449 bytes First-Assignment-Algorithms/README.md | 42 +++++ 8 files changed, 408 insertions(+) create mode 100644 First-Assignment-Algorithms/AP1403 - Algorithms/pom.xml create mode 100644 First-Assignment-Algorithms/AP1403 - Algorithms/src/main/java/Exercises.java create mode 100644 First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestPartitions.java create mode 100644 First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestProduct.java create mode 100644 First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestTraversal.java create mode 100644 First-Assignment-Algorithms/AP1403 - Algorithms/target/classes/Exercises$IntegerPartition.class create mode 100644 First-Assignment-Algorithms/AP1403 - Algorithms/target/classes/Exercises.class create mode 100644 First-Assignment-Algorithms/README.md diff --git a/First-Assignment-Algorithms/AP1403 - Algorithms/pom.xml b/First-Assignment-Algorithms/AP1403 - Algorithms/pom.xml new file mode 100644 index 0000000..f46c801 --- /dev/null +++ b/First-Assignment-Algorithms/AP1403 - Algorithms/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + org.project + Algorithms + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + + org.junit.jupiter + junit-jupiter + 5.11.4 + test + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.2 + + + + + + \ No newline at end of file diff --git a/First-Assignment-Algorithms/AP1403 - Algorithms/src/main/java/Exercises.java b/First-Assignment-Algorithms/AP1403 - Algorithms/src/main/java/Exercises.java new file mode 100644 index 0000000..d4788a9 --- /dev/null +++ b/First-Assignment-Algorithms/AP1403 - Algorithms/src/main/java/Exercises.java @@ -0,0 +1,160 @@ +import java.util.Arrays; + +public class Exercises { + + /* + there is an array of positive integers as input of function and another integer for the target value + all the algorithm should do is to find those two integers in array which their multiplication is the target + then it should return an array of their indices + e.g. {1, 2, 3, 4} with target of 8 -> {1, 3} + + note: you should return the indices in ascending order and every array's solution is unique + */ + public int[] productIndices(int[] values, int target) + { + int n = values.length; + for (int i = 0; i < n; ++i) + { + for (int j = i + 1; j < n; ++j) + { + if (values[i] * values[j] == target) + { + return new int[] {i, j}; + } + } + + } + return null; + + /* public static void main(String[] args) + { + int[] values = {2, 5, 1, 4, 10, 69, 420}; + int target = 420; + int[] result = productIndices(values, target); + + if (result.length > 0) + { + System.out.println("Indices: " + Arrays.toString(result)); + } + else + { + System.out.println("No pair found."); + } + }*/ + } + + /* + given a matrix of random integers, you should do spiral traversal in it + e.g. if the matrix is as shown below: + 1 2 3 + 4 5 6 + 7 8 9 + then the spiral traversal of that is: + {1, 2, 3, 6, 9, 8, 7, 4, 5} + + so you should walk in that matrix in a curl and then add the numbers in order you've seen them in a 1D array + */ + public int[] spiralTraversal(int[][] values, int rows, int cols) + { + if (values == null || rows == 0 || cols == 0) + { + return new int[0]; + } + int[] result = new int[rows * cols]; + int index = 0; + int top = 0, bottom = rows - 1; + int left = 0, right = cols - 1; + while (top <= bottom && left <= right) + { + for (int i = left; i <= right; i++) + { + result[index++] = values[top][i]; + } + top++; + for (int i = top; i <= bottom; i++) + { + result[index++] = values[i][right]; + } + right--; + if (top <= bottom) + { + for (int i = right; i >= left; i--) + { + result[index++] = values[bottom][i]; + } + bottom--; + } + if (left <= right) + { + for (int i = bottom; i >= top; i--) + { + result[index++] = values[i][left]; + } + left++; + } + } + return result; + } + + + /* + integer partitioning is a combinatorics problem in discreet maths + the problem is to generate sum numbers which their summation is the input number + + e.g. 1 -> all partitions of integer 3 are: + 3 + 2, 1 + 1, 1, 1 + + e.g. 2 -> for number 4 goes as: + 4 + 3, 1 + 2, 2 + 2, 1, 1 + 1, 1, 1, 1 + + note: as you can see in examples, we want to generate distinct summations, which means 1, 2 and 2, 1 are no different + you should generate all partitions of the input number and + + hint: you can measure the size and order of arrays by finding the pattern of partitions and their number + trust me, that one's fun and easy :) + + if you're familiar with lists and arraylists, you can also edit method's body to use them instead of array + */ + public class IntegerPartition + { + public static void main(String[] args) + { + int n = 4; + System.out.println("Integer partitions of " + n + " is:"); + int[] partition = new int[n]; + partition(n, n, partition, 0); + } + public static void partition(int n, int max, int[] partition, int index) + { + if (n == 0) + { + printPartition(partition,index); + return; + } + for (int i = Math.min(max,n); i >= 1; i--) + { + partition[index] = i; + partition(n - i, i, partition, index + 1); + } + } + public static void printPartition(int[] partition, int length) + { + for (int i = 0; length > i; i++) + { + System.out.print(partition[i] + " "); + } + System.out.println(); + } + } + + public static void main(String[] args) + { + // you can test your code here + } +} diff --git a/First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestPartitions.java b/First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestPartitions.java new file mode 100644 index 0000000..c9e62db --- /dev/null +++ b/First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestPartitions.java @@ -0,0 +1,65 @@ +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class TestPartitions { + static Exercises ex; + + @BeforeAll + static void setUp() { + ex = new Exercises(); + } + + @Test + void testPartition0() { + int[][] expected = { + {1} + }; + assertArrayEquals(expected, ex.intPartitions(1)); + } + + @Test + void testPartition1() { + int[][] expected = { + {4}, + {3, 1}, + {2, 2}, + {2, 1, 1}, + {1, 1, 1, 1} + }; + assertArrayEquals(expected, ex.intPartitions(4)); + } + + @Test + void testPartition2() { + int[][] expected = { + {5}, + {4, 1}, + {3, 2}, + {3, 1, 1}, + {2, 2, 1}, + {2, 1, 1, 1}, + {1, 1, 1, 1, 1} + }; + assertArrayEquals(expected, ex.intPartitions(5)); + } + + @Test + void testPartition3() { + int[][] expected = { + {6}, + {5, 1}, + {4, 2}, + {4, 1, 1}, + {3, 3}, + {3, 2, 1}, + {3, 1, 1, 1}, + {2, 2, 2}, + {2, 2, 1, 1}, + {2, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1} + }; + assertArrayEquals(expected, ex.intPartitions(6)); + } +} diff --git a/First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestProduct.java b/First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestProduct.java new file mode 100644 index 0000000..2bae3e7 --- /dev/null +++ b/First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestProduct.java @@ -0,0 +1,37 @@ +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class TestProduct { + static Exercises ex; + + @BeforeAll + static void setUp() { + ex = new Exercises(); + } + + @Test + void testProduct0() { + int[] nums = {5, 10, 2, 20}; + int target = 20; + int[] expected = {1, 2}; + assertArrayEquals(expected, ex.productIndices(nums, target)); + } + + @Test + void testProduct1() { + int[] nums = {3, 7, 1, 14}; + int target = 21; + int[] expected = {0, 1}; + assertArrayEquals(expected, ex.productIndices(nums, target)); + } + + @Test + void testProduct2() { + int[] nums = {69, 15, 17, 23, 5, 11, 4, 6, 85, 39, 81, 34, 76, 21, 3, 36, 98, 77, 9, 42}; + int target = 2898; + int[] expected = {0, 19}; + assertArrayEquals(expected, ex.productIndices(nums, target)); + } +} diff --git a/First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestTraversal.java b/First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestTraversal.java new file mode 100644 index 0000000..969c273 --- /dev/null +++ b/First-Assignment-Algorithms/AP1403 - Algorithms/src/test/java/TestTraversal.java @@ -0,0 +1,66 @@ +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class TestTraversal { + static Exercises ex; + + @BeforeAll + static void setUp() { + ex = new Exercises(); + } + + @Test + void testTraverse0() { + int[][] nums = { + {1, 2, 3, 4, 5} + }; + int rows = 1; + int cols = 5; + int[] expected = {1, 2, 3, 4, 5}; + assertArrayEquals(expected, ex.spiralTraversal(nums, rows, cols)); + } + + @Test + void testTraverse1() { + int[][] nums = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + {10, 11, 12} + }; + int rows = 4; + int cols = 3; + int[] expected = {1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8}; + assertArrayEquals(expected, ex.spiralTraversal(nums, rows, cols)); + } + + @Test + void testTraverse2() { + int[][] nums = { + {1, 2, 3, 4}, + {5, 6, 7, 8} + }; + int rows = 2; + int cols = 4; + int[] expected = {1, 2, 3, 4, 8, 7, 6, 5}; + assertArrayEquals(expected, ex.spiralTraversal(nums, rows, cols)); + } + + @Test + void testTraverse3() { + int[][] nums = { + {1, 2, 3, 4, 5, 6}, + {7, 8, 9, 10, 11, 12}, + {13, 14, 15, 16, 17, 18}, + {19, 20, 21, 22, 23, 24}, + {25, 26, 27, 28, 29, 30} + }; + int rows = 5; + int cols = 6; + int[] expected = {1, 2, 3, 4, 5, 6, 12, 18, 24, 30, 29, 28, 27, 26, 25, 19, 13, 7, 8, 9, 10, 11, 17, 23, 22, 21, + 20, 14, 15, 16}; + assertArrayEquals(expected, ex.spiralTraversal(nums, rows, cols)); + } +} diff --git a/First-Assignment-Algorithms/AP1403 - Algorithms/target/classes/Exercises$IntegerPartition.class b/First-Assignment-Algorithms/AP1403 - Algorithms/target/classes/Exercises$IntegerPartition.class new file mode 100644 index 0000000000000000000000000000000000000000..8624799f414fa31fa07727b8370dfc5275b9f431 GIT binary patch literal 1668 zcmaJ?TW=dh6#mBEcx`8MX@V0HJ0T4zIKdG_Ypo|$ zBO!6C5J(jWsj3QZNJxD{Di0;95I=zb!3#g3stDigT5(Du9=vnrT)y+2b7sbW{`24u z0B>O}jTquKEC(G(2=rc+*W^r1dfPKAn^#p;3nb=T&()U%;`zz-G?GZ!NIU3+BQSjB zx(cdpQ#B__o>to`Se1cxwd;EVsYY8)=1ZlGQYixHcF==ff$m1&diq#IBH!}qL%)qD z9Aq#+w>GCGsdq@E?h?L(kdvhpp2U!iVFy{{9^+rxZ)#Pix4*|KGUX7(^=DR1(uxk0 ztQY&(gi!}iVN77KE_cYFN9lbbba z`pSM)*4Aaob{TBbt^X9WfMBF33B)$|5w4k?yw0|`-j?FpRnfA#yCfSe?unZ6w#|ss6@M?N zss-1S*?rWGY13qZ=$Czecdt=A*`8yX#{$>( z8|oDI;~k7{U-9_4YeCG*TCL)0#c4Su&r`vCO*VN0S^R(Rpp8|5b0^9?jlGQz1V;bg zm8T{u6=(Sj#CUfA3!EmVxO-9GC@3NPV2RR-}RW{7j`w&$|Hrbz*Q(kt{+=AVdWjxiOFu8d->L?5(rG*^Sog zPzkB0aNvTt!li`@l~W0-1X772aY3RN!~rP>?)(o`zE2M@ z&}*zl7RD@`pmE*tc>Bp*wR~8%#VGXr#>~VL{S1-Z$N$GIU@r#ku;#wzsbU@L)uW)8 zSfK^C6SqoWMDp#PigrX?q!`P<}S@})JS&&RoZc$R*B3((#tu=EVeqL0Si79Ytn!;%hs%q!lC0qeyZl6=#f0^73n=`eD`AFvdJ* zSh<(t8lS=|w9MfwUd1Hl@d7S$eI0L8UPB30yn!1iqfPk{7H|s{e2lm785VJy@@u?< zZ}2X@#WEfe{R`!~xTAZV1JzOG z%^Y;}CuG@gorqmLL7Tz-k9d-r$xOb-8EO`%=>cCunsYXZ`?RMy%4!;>G0?Ad*nHc9 zW#MTdEQ|~F0sBac$2h&uX^F*v5{Z4-N*W|Mhx6T`L7r4< +Good luck, and happy coding!