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 } } 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 0000000..8624799 Binary files /dev/null and b/First-Assignment-Algorithms/AP1403 - Algorithms/target/classes/Exercises$IntegerPartition.class differ diff --git a/First-Assignment-Algorithms/AP1403 - Algorithms/target/classes/Exercises.class b/First-Assignment-Algorithms/AP1403 - Algorithms/target/classes/Exercises.class new file mode 100644 index 0000000..0e02a5c Binary files /dev/null and b/First-Assignment-Algorithms/AP1403 - Algorithms/target/classes/Exercises.class differ diff --git a/First-Assignment-Algorithms/README.md b/First-Assignment-Algorithms/README.md new file mode 100644 index 0000000..bec7e9d --- /dev/null +++ b/First-Assignment-Algorithms/README.md @@ -0,0 +1,42 @@ +# First Assignment - Algorithms + +Are you ready to challenge yourself with some algorithmic problems? +This section will help strengthen your *Java* skills and reinforce the **Way of BP**. + + +## Getting Started + +This is the second part of your first assignment in the *Advanced Programming* course. +We strongly recommend that you complete the first part – [Warm-Up](https://github.com/Advanced-Programming-1403/First-Assignment-WarmUp) – before starting. + +Just like in the previous section, you will need to solve three problems using the same approach. + + +## Evaluation + +To successfully complete this assignment, you must: + +- Implement the functions as described in the comments, ensuring your code compiles and runs without errors. +- Pass all unit tests in the `src/test/java` directory. +- Write **clean and readable code** for your mentor. It is recommended to: + - Use meaningful comments. + - Follow **clean-code principles** for better readability and maintainability. + + +## Submission Instructions + +To submit your assignment for review, follow the same steps mentioned in the *Warm-Up* section, using Git for version control. + +**Tip:** It's recommended to use *branches* in Git: +- Commit your changes in `origin/develop`. +- Create a pull request for your mentor to review. + + +## Important Notes + +**Strictly prohibited:** Using ChatGPT or any other generative AI tool to solve any part of this assignment. +**Allowed:** Using AI tools to clarify concepts and learning, but not for direct answers. +**Best Practice:** If you're struggling, try searching, asking questions, or consulting official documentation. + +
+Good luck, and happy coding!