Implement and pass tests for triangle, spiral, and partitions #6
@@ -1,5 +1,7 @@
|
|||||||
public class MainExercises
|
import java.util.ArrayList;
|
||||||
{
|
import java.util.List;
|
||||||
|
public class MainExercises {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
you should create a triangle with "*" and return a two-dimensional array of characters based on that
|
you should create a triangle with "*" and return a two-dimensional array of characters based on that
|
||||||
the triangle's area is empty, which means some characters should be " "
|
the triangle's area is empty, which means some characters should be " "
|
||||||
@@ -15,18 +17,28 @@ public class MainExercises
|
|||||||
* *
|
* *
|
||||||
* *
|
* *
|
||||||
*****
|
*****
|
||||||
|
|
||||||
the output has to be a two-dimensional array of characters, so don't just print the triangle!
|
the output has to be a two-dimensional array of characters, so don't just print the triangle!
|
||||||
*/
|
*/
|
||||||
public char[][] generateTriangle(int n) {
|
public char[][] generateTriangle(int n) {
|
||||||
|
char[][] triangle = new char[n][]; //تعداد سطر ها برابر با n است و تعداد ستون ها بعدا مشخص میشود
|
||||||
// todo
|
for (int i = 0; i < n; i++) { //لوپ برای حرکت از سطر صفرم تا سطر اخر
|
||||||
return null;
|
triangle[i] = new char[i + 1]; // طول هر سطر برابر با i + 1 است تا شکل مثلث را داشته باشیم
|
||||||
|
for (int j = 0; j <= i; j++) // لوپ داخل لوپ قبلی برای حرکت روی ستون هایی که ساخته شده اند
|
||||||
|
if (j == 0 || j == i || i == n - 1) { // سه شرط لازم برای گذاشتن * برای ساخت مثلث تو خالی طبق الگو
|
||||||
|
triangle[i][j] = '*';
|
||||||
|
} else {
|
||||||
|
triangle[i][j] = ' '; // گذاشتن ' ' برای فضا های خالی
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return triangle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
|
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
|
||||||
|
|
||||||
@@ -58,8 +70,50 @@ public class MainExercises
|
|||||||
- Number of columns: matrix[0].length (if rectangular)
|
- Number of columns: matrix[0].length (if rectangular)
|
||||||
*/
|
*/
|
||||||
public int[] spiralTraversal(int[][] matrix) {
|
public int[] spiralTraversal(int[][] matrix) {
|
||||||
// todo
|
int rows= matrix.length; // تعداد کل سطرهای ماتریس
|
||||||
return null;
|
int cols =matrix[0].length; // تعداد کل ستونهای ماتریس
|
||||||
|
|
||||||
|
int [] result = new int[rows* cols] ;// ساخت آرایه یک بعدی خروجی به اندازه کل المانهای ماتریس
|
||||||
|
int index = 0; // اندیس برای پر کردن آرایه خروجی
|
||||||
|
|
||||||
|
// تعریف ۴ مرز اصلی برای هدایت حرکت حلزونی
|
||||||
|
int top =0;
|
||||||
|
int bottom = rows- 1;
|
||||||
|
int left = 0;
|
||||||
|
int right= cols -1;
|
||||||
|
|
||||||
|
while (top<= bottom && left <=right) { // حلقه اصلی: تا زمانی که مرزها به هم نرسیدهاند یا از هم رد نشدهاند ادامه می دهد
|
||||||
|
//حرکت از چپ به راست روی سطر بالایی
|
||||||
|
for (int i = left; i <= right; i++) {
|
||||||
|
result[index++] = matrix[top][i];
|
||||||
|
}
|
||||||
|
top++; // چون سطر بالایی کاملاً پیمایش شد، مرز بالا را یک واحد می اوریم پایین
|
||||||
|
|
||||||
|
// ۲. حرکت از بالا به پایین روی ستون راستی
|
||||||
|
for (int i =top; i <=bottom;i++) {
|
||||||
|
result[ index++] = matrix[i][right];
|
||||||
|
}
|
||||||
|
right--; // چون ستون راستی کاملاً پیمایش شد، مرز راست را یک واحد میاوریم چپ
|
||||||
|
|
||||||
|
// ۳. حرکت از راست به چپ روی سطر پایینی
|
||||||
|
// شرطِ (top <= bottom) برای این است که مطمئن شویم در گامهای قبلی سطرها تمام نشده باشند
|
||||||
|
if (top <= bottom) {
|
||||||
|
for (int i = right; i >= left;i--){
|
||||||
|
result[index++ ] =matrix[bottom][i];
|
||||||
|
}
|
||||||
|
bottom--; // چون سطر پایینی پیمایش شد، مرز پایین را یک واحد میاوریم بالا
|
||||||
|
}
|
||||||
|
|
||||||
|
// ۴. حرکت از پایین به بالا روی ستون چپی
|
||||||
|
// شرطِ (left <= right) برای این است که مطمئن شویم ستونها در گامهای قبلی تمام نشده باشند
|
||||||
|
if (left <=right){
|
||||||
|
for (int i = bottom; i >= top; i--) {
|
||||||
|
result[index++ ] = matrix[i][ left];
|
||||||
|
}
|
||||||
|
left++ ; // چون ستون چپی پیمایش شد، مرز چپ را یک واحد میاوریم راست
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -90,14 +144,49 @@ public class MainExercises
|
|||||||
body to use them instead of arrays.
|
body to use them instead of arrays.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
public int[][] intPartitions(int n) {
|
public int[][] intPartitions(int n) {
|
||||||
// todo
|
// ایجاد یک (ArrayList) برای ذخیره موقت تمام افرازها
|
||||||
return null;
|
// چون از قبل نمیدانیم تعداد کل افرازها چقدر است، (ArrayList) بهترین گزینه است
|
||||||
|
List<List<Integer>>resultList = new ArrayList<>();
|
||||||
|
|
||||||
|
// صدا زدن تابع بازگشتی کمکی برای شروع فرآیند پیدا کردن افرازها
|
||||||
|
findPartitions(n, n, new ArrayList<>(), resultList);
|
||||||
|
|
||||||
|
// تبدیل لیست دو بعدی پویا (List<List<Integer>>) به آرایه دو بعدی ثابت جاوا (int[][])
|
||||||
|
int[][] partitions=new int[resultList.size()][];
|
||||||
|
for (int i = 0; i < resultList.size(); i++) {
|
||||||
|
List<Integer> current = resultList.get(i); // گرفتن یکی از افرازها
|
||||||
|
partitions[i] = new int[current.size()]; // ساخت یک سطر ارایه به اندازه طول آن افراز
|
||||||
|
for (int j = 0; j < current.size(); j++) {
|
||||||
|
partitions[i][j] = current.get(j); // کپی کردن تک تک اعداد درون آرایه
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// برگرداندن آرایه دو بعدی نهایی که شامل تمام افرازهاست
|
||||||
|
return partitions;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void findPartitions(int remaining , int maxVal,List<Integer> current, List<List<Integer>> result){
|
||||||
|
// شرط پایان بازگشت: اگر باقیمانده عدد به صفر رسید، یعنی یک افراز کامل پیدا شده است
|
||||||
|
if(remaining==0){
|
||||||
|
// یک کپی از افراز فعلی (current) میسازیم و به لیست نتایج اصلی اضافه میکنیم
|
||||||
|
result.add(new ArrayList<>(current));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// شروع از کوچکترین مقدار بین باقیمانده و حداکثر عدد مجاز قبلی به صورت نزولی تا ۱
|
||||||
|
for(int i = Math.min(remaining, maxVal); i >= 1 ; i--){
|
||||||
|
current.add(i); // عدد i را به افراز فعلی اضافه کن
|
||||||
|
// فرآیند را برای باقیمانده عدد (remaining - i) تکرار کن
|
||||||
|
// پارامتر دوم را i میگذاریم تا در گام بعدی، عددی بزرگتر از i انتخاب نشود
|
||||||
|
findPartitions(remaining - i, i, current,result);
|
||||||
|
current.remove(current.size() - 1); // ۳. عقبگرد (Backtrack): عدد را حذف کن تا در چرخه بعدی حلقه، حالتهای دیگر بررسی شوند
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main()
|
public static void main(String[] args){
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user