From d9b8602b73080c36a30e7999866fae816ed80fd9 Mon Sep 17 00:00:00 2001 From: mostafa mehrabi Date: Fri, 7 Mar 2025 01:06:04 +0330 Subject: [PATCH 1/5] comit.devops --- .../src/main/java/Exercises.java | 147 ++++++++++++++++-- 1 file changed, 137 insertions(+), 10 deletions(-) diff --git a/AP1403 - Algorithms/src/main/java/Exercises.java b/AP1403 - Algorithms/src/main/java/Exercises.java index 15a2133..955c707 100644 --- a/AP1403 - Algorithms/src/main/java/Exercises.java +++ b/AP1403 - Algorithms/src/main/java/Exercises.java @@ -1,6 +1,10 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class Exercises { - /* + /*;p 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 @@ -8,8 +12,18 @@ 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 static int[] productIndices(int[] values, int target) { + + for (int i = 0 ; i < values.length ; i++){ + for(int j = i + 1 ; j < values.length ; j++){ + if(values[i] * values[j] == target){ + int[] index = {i , j}; + Arrays.sort(index); + return index; + } + } + } + return null; } @@ -24,9 +38,52 @@ 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 + public static int[] spiralTraversal(int[][] values, int rows, int cols) { + if (values == null || rows == 0 || cols == 0) { + return new int[0]; // .اگر روردی نا معتبر بود یک ارایه ی خالی چاپ میکنیم + } + List resultList = new ArrayList<>(); + int startrow = 0 ; + int endrow = rows - 1 ; + int startcol = 0 ; + int endcol = cols -1 ; + + while (startrow <= endrow && startcol <= endcol) { + + for(int i = startcol ; i < endcol ; i++){ + resultList.add(values [startrow][i]); + } + startrow++ ; + + for(int i = startrow ; i <= endrow ; i++){ + resultList.add(values[i][endcol]); + } + endcol-- ; + + if (startrow <= endrow) { + for(int i = endcol ; i >= startcol ; i--){ + resultList.add(values[endrow][i]); + } + } + endrow-- ; + + if (startcol <= endcol) { + for(int i = endrow ; i >= startrow ; i--){ + resultList.add(values[i][startcol]);; + } + } + startcol++ ; + + // .هر کد های بالا به ترتیب کد هایی برای حرکت به راست پایین چپ و بالا است + + int[] result = new int[resultList.size()]; + for(int i = 0 ; i < resultList.size() ; i++){ + result[i] = resultList.get(i); + } + return result; + } return null; + } /* @@ -53,12 +110,82 @@ 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 static int[][] intPartitions(int n) { + + List> partitionsList = new ArrayList<>(); + partitionHelper(n, n, new ArrayList<>(), partitionsList); + + int[][] partitionsArray = new int[partitionsList.size()][]; + for (int i = 0; i < partitionsList.size(); i++) { + List partition = partitionsList.get(i); + partitionsArray[i] = new int[partition.size()]; + for (int j = 0; j < partition.size(); j++) { + partitionsArray[i][j] = partition.get(j); + } + } + return partitionsArray; + } + + private static void partitionHelper(int n, int max, List currentPartition, List> partitions) { + if (n == 0) { + partitions.add(new ArrayList<>(currentPartition)); + return; + } + + for (int i = Math.min(max, n); i >= 1; i--) { + currentPartition.add(i); + partitionHelper(n - i, i, currentPartition, partitions); + currentPartition.remove(currentPartition.size() - 1); + } } public static void main(String[] args) { - // you can test your code here + + int[] array = {1,2,3,4}; + int targetvalue = 8 ; + int[] result = productIndices(array , targetvalue); + + if (result != null) { + System.out.println("index : " + Arrays.toString(result)); + + } + else + { + System.out.println("we do not find it ."); + } + } + + + // main چک کردن کد دوم در + + int[][] matrix = { {1,2,3},{4,5,6},{7,8,9} }; + + int rows = matrix.length ; + int cols = matrix[0].length ; + + int[] testing = spiralTraversal(matrix , rows , cols); + + for(int i = 0 ; i < testing.length ; i++){ + System.out.print(testing[i] + " "); + } + + // main چک کردن کد سوم در + + int number = 4; + int[][] result = intPartitions(number); + + System.out.println("Partitions of " + number + ":"); + for (int[] partition : result) { + System.out.print("["); + for (int i = 0; i < partition.length; i++) { + System.out.print(partition[i]); + if (i < partition.length - 1) + { + System.out.print(", "); + } + } + System.out.println("]"); + } + + } -} From 30b3c714587748a0b790f2afe82d5dae72ca941c Mon Sep 17 00:00:00 2001 From: mostafa mehrabi Date: Fri, 7 Mar 2025 01:13:05 +0330 Subject: [PATCH 2/5] new comit --- .../src/main/java/Exercises.java | 30 +++---------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/AP1403 - Algorithms/src/main/java/Exercises.java b/AP1403 - Algorithms/src/main/java/Exercises.java index 955c707..9064529 100644 --- a/AP1403 - Algorithms/src/main/java/Exercises.java +++ b/AP1403 - Algorithms/src/main/java/Exercises.java @@ -13,30 +13,6 @@ public class Exercises { note: you should return the indices in ascending order and every array's solution is unique */ public static int[] productIndices(int[] values, int target) { - - for (int i = 0 ; i < values.length ; i++){ - for(int j = i + 1 ; j < values.length ; j++){ - if(values[i] * values[j] == target){ - int[] index = {i , j}; - Arrays.sort(index); - return index; - } - } - } - - return null; - } - - /* - 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 static int[] spiralTraversal(int[][] values, int rows, int cols) { if (values == null || rows == 0 || cols == 0) { @@ -139,6 +115,8 @@ private static void partitionHelper(int n, int max, List currentPartiti } } +} + public static void main(String[] args) { int[] array = {1,2,3,4}; @@ -186,6 +164,6 @@ public static void main(String[] args) { } System.out.println("]"); } - - } + + From 313c989f66cc5715643bb75646e82e30d543c333 Mon Sep 17 00:00:00 2001 From: mostafa mehrabi Date: Fri, 7 Mar 2025 01:20:36 +0330 Subject: [PATCH 3/5] new.commit --- AP1403 - Algorithms/src/main/java/Exercises.java | 1 + 1 file changed, 1 insertion(+) diff --git a/AP1403 - Algorithms/src/main/java/Exercises.java b/AP1403 - Algorithms/src/main/java/Exercises.java index 9064529..2049f32 100644 --- a/AP1403 - Algorithms/src/main/java/Exercises.java +++ b/AP1403 - Algorithms/src/main/java/Exercises.java @@ -167,3 +167,4 @@ public static void main(String[] args) { } + From 9634bb79d0c1d7a4cdca34e149d152851608360c Mon Sep 17 00:00:00 2001 From: mostafa mehrabi Date: Fri, 7 Mar 2025 01:22:04 +0330 Subject: [PATCH 4/5] new.commit --- AP1403 - Algorithms/src/main/java/Exercises.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/AP1403 - Algorithms/src/main/java/Exercises.java b/AP1403 - Algorithms/src/main/java/Exercises.java index 2049f32..5e8ac72 100644 --- a/AP1403 - Algorithms/src/main/java/Exercises.java +++ b/AP1403 - Algorithms/src/main/java/Exercises.java @@ -164,7 +164,4 @@ public static void main(String[] args) { } System.out.println("]"); } - } - - - + } From 8172b1e76a1192e453c8c750dcca77ea56a77dbd Mon Sep 17 00:00:00 2001 From: mostafa mehrabi Date: Fri, 7 Mar 2025 01:44:19 +0330 Subject: [PATCH 5/5] mosi.comit --- AP1403 - Algorithms/src/main/java/Exercises.java | 1 - 1 file changed, 1 deletion(-) diff --git a/AP1403 - Algorithms/src/main/java/Exercises.java b/AP1403 - Algorithms/src/main/java/Exercises.java index 5e8ac72..ee19da1 100644 --- a/AP1403 - Algorithms/src/main/java/Exercises.java +++ b/AP1403 - Algorithms/src/main/java/Exercises.java @@ -133,7 +133,6 @@ public static void main(String[] args) { } } - // main چک کردن کد دوم در int[][] matrix = { {1,2,3},{4,5,6},{7,8,9} };