-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatterns.java
786 lines (600 loc) · 20.9 KB
/
Patterns.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
package BasicPrograms;
/**
* @author Srinvas Vadige, srinivas.vadige@gmail.com
* @since 21 Sept 2024
*/
public class Patterns {
public static void main(String[] args) {
System.out.println("PATTERNS");
System.out.println();
/*
METHOD INVOCATIONS --------------------------------------------------
*/
int n = 5;
/*
1) Rectangular Star Pattern
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
*/
System.out.println("1) Rectangular Star Pattern");
rectangularStarPattern(n); System.out.println(); // new line for next method
/*
2) Right-Angled Triangle Pattern
*
* *
* * *
* * * *
* * * * *
*/
System.out.println("2) Right-Angled Triangle Pattern");
rightAngledTrianglePattern(n); System.out.println();
/*
3) Right-Angled Triangle Number Pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*/
System.out.println("3) Right-Angled Triangle Number Pattern");
rightAngledTriangleNumberPattern(n); System.out.println();
/*
4) Right-Angled Triangle Number Pattern - 2
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
*/
System.out.println("4) Right-Angled Triangle Number Pattern - 2");
rightAngledTriangleNumberPattern2(n); System.out.println();
/*
5) Inverted Right-Angled Triangle Pattern
* * * * *
* * * *
* * *
* *
*
*/
System.out.println("5) Inverted Right-Angled Triangle Pattern");
invertedRightAngledTrianglePattern(n); System.out.println();
/*
6) Inverted Right-Angled Triangle Pattern
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
*/
System.out.println("6) Inverted Right-Angled Triangle Pattern");
invertedRightAngledTriangleNumberPattern(n); System.out.println();
/*
7) Star Pyramid
*
***
*****
*******
*********
*/
System.out.println("7) Star Pyramid");
starPyramid(n); System.out.println();
/*
8) Inverted Star Pyramid
*********
_*******_
__*****__
___***___
____*____
*/
System.out.println("8) Inverted Star Pyramid");
invertedStarPyramid(n); System.out.println();
/*
9) Diamond Star Pyramid
....*....
...***...
..*****..
.*******.
*********
*********
_*******_
__*****__
___***___
____*____
*/
System.out.println("9) Diamond Star Pyramid");
diamondStarPyramid(n); System.out.println();
/*
10) Half Diamond Star Pattern
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
*/
System.out.println("10) Half Diamond Star Pattern");
halfDiamondStarPyramid(n); System.out.println();
/*
11) Binary Number Triangle Pattern
1
01
101
0101
10101
010101
*/
System.out.println("11) Binary Number Triangle Pattern");
binaryNumberTrianglePattern(n); System.out.println();
/*
12) Number Crown Pattern"
1 1
12 21
123 321
1234 4321
1234554321
*/
System.out.println("12) Number Crown Pattern");
numberCrownPattern(n); System.out.println();
/*
13) Increasing Number Triangle Pattern
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 //note 10 and 14 in same col
*/
System.out.println("13) Increasing Number Triangle Pattern");
increasingNumberTrianglePattern(n); System.out.println();
/*
14) Increasing Letter Triangle Pattern
A
A B
A B C
A B C D
A B C D E
A B C D E F
*/
System.out.println("14) Increasing Letter Triangle Pattern");
increasingLetterTrianglePattern(n); System.out.println();
/*
15) Reverse Letter Triangle Pattern
A B C D E
A B C D
A B C
A B
A
*/
System.out.println("15) Reverse Letter Triangle Pattern");
reverseLetterTrianglePattern(n); System.out.println();
/*
16) Alpha-Ramp Pattern
A
B B
C C C
D D D D
E E E E E
F F F F F F
*/
System.out.println("16) Alpha-Ramp Pattern");
alphaRampPattern(n); System.out.println();
/*
17) Alpha-Hill Pattern
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
*/
System.out.println("17) Alpha-Hill Pattern");
alphaHillPattern(n); System.out.println();
/*
18) Alpha-Triangle Pattern
E
D E
C D E
B C D E
A B C D E
*/
System.out.println("18) Alpha-Triangle Pattern");
alphaTrianglePattern(n); System.out.println();
/*
19) Symmetric-Void Pattern
**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********
*/
System.out.println("19) Symmetric-Void Pattern");
symmetricVoidPattern(n); System.out.println();
/*
20) Symmetric-Butterfly Pattern
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
*/
System.out.println("20) Symmetric-Butterfly Pattern");
symmetricButterflyPattern(n); System.out.println();
/*
21) Hollow Rectangle Pattern
*****
* *
* *
* *
*****
*/
System.out.println("21) Hollow Rectangle Pattern");
hollowRectangularPattern(n); System.out.println();
/*
22) The Number Pattern
555555555
544444445
543333345
543222345
543212345
543222345
543333345
544444445
555555555
*/
System.out.println("22) The Number Pattern");
theNumberPattern(n); System.out.println();
}
/*
METHOD DECLARATIONS --------------------------------------------------
*/
static void rectangularStarPattern(int n) {
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.print("* ");
}
System.out.println(); // exit from current row print in next line
}
}
static void rightAngledTrianglePattern(int n) {
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
System.out.print("* ");
}
System.out.println();
}
}
static void rightAngledTriangleNumberPattern(int n) {
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
System.out.print(j+" ");
}
System.out.println();
}
}
static void rightAngledTriangleNumberPattern2(int n) {
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
System.out.print(i+" ");
}
System.out.println();
}
}
static void invertedRightAngledTrianglePattern(int n) {
for(int i=n; i>=0; i--){ // or for (int i = 0; i < N; i++) to loop n times
for(int j=1; j<=i; j++){ // or for (int j = n; j > i; j--)
System.out.print("* ");
}
System.out.println();
}
}
static void invertedRightAngledTriangleNumberPattern(int n) {
for(int i=n; i>=0; i--){ // or for (int i = 0; i < N; i++) to loop n times
for(int j=1; j<=i; j++){ // or for (int j = n; j > i; j--)
System.out.print(j+" "); // or System.out.print(N-j+1+" ")
}
System.out.println();
}
}
/**
* i == rows and j == columns
* @param n
*/
static void starPyramid(int n) {
int cols= 2*n-1; // if n = 5 => ----*---- and n is number of rows
for (int i = 1; i <= n; i++) {
int noOfStarsInThisRow = 2*i-1; // or +2 in each i loop
int fillStart = n-i+1; // or (int)Math.ceil(cols/2) - i+1; i.e i1 sc5, i2 sc4, i3,sc3, i4 sc2, i5 sc1
int fillEnd = fillStart + noOfStarsInThisRow - 1;
for (int j = 1; j <= cols; j++) {
if( j>=fillStart && j<=fillEnd){
System.out.print("*");
} else System.out.print(".");
}
// // or
// for (int j=1, noOfStars=0; j <= cols; j++) {
// if(areaStartCol== j-areaStartCol && noOfStars < noOfStarsInThisRow){
// noOfStars++;
// System.out.print("*");
// } else System.out.print(".");
// }
System.out.println();
}
// // or
// for (int i = 0; i < n; i++) {
// // print spaces
// for (int j = 0; j < n - i - 1; j++) { // below midIndex -i, where midIndex always be n-1
// System.out.print(".");
// }
// // print stars
// for (int k = 0; k < 2 * i + 1; k++) {
// System.out.print("*");
// }
// // pritn spaces again
// System.out.println();
// }
// or ---------- IMPORTANT ---------
// int mid = cols/2; // or n-1 => mid will be always the same, only the pyramid width increases in each row by 1 on each side
// for(int i=0; i<n; i++){
// int fillStart = mid - i;
// int fillEnd = mid + i;
// //iterating through all the columns
// for(int j=0; j<cols; j++){
// // filling the stars in current row if j is between stars
// if(j >= fillStart && j<= fillEnd)
// System.out.print("*");
// else System.out.print("-");
// }
// System.out.println();
// }
}
static void invertedStarPyramid(int rows) {
int midIndex = rows-1;
int cols = 2*rows-1;
for (int i = rows-1; i >= 0 ; i--) {
int widthStart = midIndex-i;
int widthEnd = midIndex+i;
for (int j = 0; j < widthStart; j++) System.out.print("_");
for (int j = widthStart; j <= widthEnd; j++) System.out.print("*");
for (int j = widthEnd+1; j < cols; j++) System.out.print("_");
System.out.println();
}
}
static void diamondStarPyramid(int rows) {
starPyramid(rows);
invertedStarPyramid(rows);
}
static void halfDiamondStarPyramid(int rows) {
rightAngledTrianglePattern(rows-1);
invertedRightAngledTrianglePattern(rows);
}
static void binaryNumberTrianglePattern(int n) {
for(int i=0; i<n; i++){
int ibool = i%2==0 ? 1:0;
for(int j=0; j<=i; j++){
System.out.print(ibool + " ");
ibool = 1 - ibool;
}
System.out.println();
}
}
static void numberCrownPattern(int rows){
int cols = 2*rows;
for (int i = rows-1; i >= 0 ; i--) {
int widthStart = rows-i;
int widthEnd = rows+i;
for (int j = 1; j <= widthStart; j++) System.out.print(j);
for (int j = widthStart; j < widthEnd; j++) System.out.print(" ");
for (int j = widthEnd+1; j <= cols; j++) System.out.print(cols-j+1);
System.out.println();
}
// // or
// int spaces = 2*(rows-1);
// for(int i=1;i<=rows;i++){
// for(int j=1;j<=i;j++) System.out.print(j);
// for(int j=1;j<=spaces;j++) System.out.print(" ");
// for(int j=i;j>=1;j--) System.out.print(j);
// spaces-=2; // After each iteration nos. increase by 2, thus, spaces will decrement by 2
// System.out.println();
// }
}
static void increasingNumberTrianglePattern(int n) {
int num = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
static void increasingLetterTrianglePattern(int n) {
for (int i = 0; i < n; i++) {
// for (int j = 0; j <= i; j++) System.out.print( (char) (j + 65) + " ");
for (char ch = 'A'; ch<='A'+i; ch++) System.out.print(ch + " ");
System.out.println();
}
}
static void reverseLetterTrianglePattern(int n) {
for (int i = n; i > 0; i--) {
// for (int j = 0; j < i; j++) System.out.print( (char) (j + 65) + " ");
for (char ch = 'A'; ch<'A'+i; ch++) System.out.print(ch + " ");
System.out.println();
}
}
static void alphaRampPattern(int n) {
for (char ch = 'A'; ch<'A'+n; ch++) {
for (int j = 'A'; j <= ch; j++) System.out.print(ch + " "); // or (char) 'A'+i
System.out.println();
}
}
static void alphaHillPattern(int rows) {
int midIndex = rows -1;
for (int i = 0; i < rows; i++) {
int startWidth = midIndex-i;
int endWidth = midIndex+i;
for (int j = 0; j < startWidth; j++) System.out.print(" ");
for (int j = startWidth; j <= midIndex; j++) System.out.print( (char) (j - startWidth + 'A'));
for (int j = midIndex +1 ; j <= endWidth; j++) System.out.print( (char) (endWidth - j + 'A'));
System.out.println();
// or instead of second & third loop use this instead
/*
char ch = 'A';
int breakpoint = (2*i+1)/2;
for(int j=1;j<=2*i+1;j++){
System.out.print(ch);
if(j <= breakpoint) ch++;
else ch--;
}
*/
}
}
static void alphaTrianglePattern(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = i; j >= 1; j--)
System.out.print((char) (rows - j + 'A') + " ");
System.out.println();
}
}
static void symmetricVoidPattern(int n) {
int rows = 2*n;
int cols = 2*n;
int midIndex = n; // 5th index / 6th ele is going to be the middle if we 11 eles but here we have 10 eles
for (int row = 0, i = 0; row < rows; row++) { // i is the fill pattern
int startWidth = midIndex - i; // when i=1; 4th index / 5th ele => INCLUSIVE
int endWidthEx = midIndex + i; // when i=1; 6th index / 7th ele => EXCULSIVE as even cols
for (int col = 0; col < cols; col++) {
if (col>=startWidth && col<endWidthEx) // col<endWidthEx EXCLUSIVE => because we are considering 6th ele / 5th index as middle item
System.out.print(" ");
else System.out.print("*");
}
// increase the width or i-pattern upto n-1 skip n-1 and then decrease from n
// skip i=5 step cause maintain two similar i => 0, 1, 2, 3, 4, 4, 3, 2, 1, 0
// * *
// * *
// in the middle rows
if (row < n-1) // considering i as fill pattern as per n cols not 2n cols. Here row is row < 4 i.e upto row == 3 or 4th ele i.e upto i==3 val is 4th ele
i++;
else if( row >= n) // from row == 5 i.e 6th ele there i = 4
i--;
System.out.println();
}
// or use two parts of n rows instead of direct 2n rows
}
static void symmetricButterflyPattern(int n) { // if you don't wan to use i then change values of startWidth and endWidth in if (row <= n) condition instead of i
int rows = 2*n;
int cols = 2*n;
int midIndex = n;
for (int row = 1, i = n-1; row <= rows; row++) {
int startWidth = midIndex - i; // INCLUSIVE as row starts from 1
int endWidth = midIndex + i; // INCLUSIVE as row starts from 1
for (int col = 1; col < cols; col++) {
if (col >= startWidth && col<=endWidth)
System.out.print(" ");
else System.out.print("*");
}
// remember that current row is less than next loop
// i & row will increase/decrease at once after the child for loop
// row starts from 1;
// let n =5; i decrease upto 0 i.e when row = 5 and i should increase when row becomes 6
// i.e here increase i at row = 4 (when i = 0) => i should not be less than 0
if (row <= n)
i--;
else
i++;
System.out.println();
}
// or use two halfs of n rows instead of direct 2n rows
}
static void hollowRectangularPattern(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n-1 || j == 0 || j == n-1) {
System.out.print("*");
} else System.out.print(" ");
}
System.out.println();
}
}
static void theNumberPattern(int n) {
// here i saw symmetricButterflyPattern() pattern but here decrease and increase
int rows = 2*n-1;
int cols = 2*n-1;
int midNum = n; // odd cols
for (int row = 1, i=n; row <= rows; row++) {
int startWidth = midNum - i; // INCLUSIVE --> min val is 0th index
int endWidth = midNum + i -2; // -2 to make it INCLUSIVE --> so, max val be 8 i.e 8th index
for (int j = 0; j < cols; j++) {
if (j < startWidth)
System.out.print(n-j);
else if (j >= startWidth && j <= endWidth )
System.out.print(i);
else // j > endWidth
System.out.print(j-n+2); // here min of j-n is 0 || (i + distance++ to right)
System.out.print(" ");
}
// next loop row = current row + 1
int nextRow = row + 1;
if(nextRow <= n) // or row < n
i--;
else i++;
System.out.println();
}
System.out.println();
// //or use DISTANCE pattern Algorithm
/*
for example take n = 4
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
if we substract n from above matrix then it'll become as below matrix
we know that i is for rows and j is for cols but for distance, it'll be in reverse
j distance
◄-----------►
left right
0 0 0 0 0 0 0 ▲ top distance
0 1 1 1 1 1 0 |
0 1 2 2 2 1 0 |
0 1 2 3 2 1 0 | i distance
0 1 2 2 2 1 0 |
0 1 1 1 1 1 0 |
0 0 0 0 0 0 0 ▼ bottom distance
let's take "3" in second matrix then it's left-distance is 3 as j=3 and top-distance is 3 as i=3
"Top" distance of top-left 0 is: 0 || and for other eles that is: i
"Left" distance same 0 will be is: 0 || and for other eles that is: j
"Right" distance same top-left 0 is: 2n-2 || and for other eles that is: 2n-2-j as j is 0
"Bottom" distance of same 0 is: 2n-2 || and for other eles that is: 2n-2-i as i is 0
and calculate the min distance of any element here, then that distance will the element's value
and finally subtract current 0 1 2 3... matrix with n
then we'll get the original matrix
*/
// for(int i=0;i<2*n-1;i++){ // rows, start from 0 as index means distance - iDistance
// for(int j=0;j<2*n-1;j++){ // cols, start from 0 as index means distance - jDistance
// int top = i;
// int left = j;
// int bottom = (2*n - 2) - i;
// int right = (2*n - 2) - j;
// // Min of 4 directions and then we subtract from n
// // This pattern is little similarly to above hollowRectangularPattern() logic
// System.out.print(n- Math.min(Math.min(top,bottom), Math.min(left,right)) + " ");
// }
// System.out.println();
// }
}
}