Skip to content

Commit 2ddc541

Browse files
committed
solve 11_PatternChallenge
1 parent e868089 commit 2ddc541

File tree

3 files changed

+152
-20
lines changed

3 files changed

+152
-20
lines changed

Pattern Algorithms/10_PatternChallenge.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ void printPattern_10(int input){
6363
// Newline to print when inner loops are done executing
6464
cout<<endl;
6565
}
66-
// Jump to newline when the inner loops have finished executing.
6766

6867
}
6968
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
/*******************************************************************************************************
3+
/* ___ _ _ _ _
4+
/* / _ \ | | (_)| | | |
5+
/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___
6+
/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __|
7+
/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \
8+
/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/
9+
/* __/ |
10+
/* |___/
11+
/******************************************************************************************************/
12+
13+
/***********************************
14+
/* 11_PatternChallenge
15+
/***********************************/
16+
17+
// Library imports
18+
#include <iostream>
19+
using namespace std;
20+
21+
//Prototypes for the algorithms
22+
void printPattern_11(int);
23+
24+
int main(){
25+
// user input
26+
int userInput;
27+
28+
cout<<"Enter number of lines."<<endl;
29+
cin>>userInput;
30+
31+
printPattern_11(userInput);
32+
return 0;
33+
}
34+
35+
/**********************************************************
36+
/* <printPattern_11>
37+
/*
38+
/* @description : Prints a pyramid pattern in increasing and then in a decreasing way after reaching the mid point.
39+
/* @param : Integer<input>
40+
/* @return : None, void
41+
/* @complexity : O(n)
42+
/* @explanation : Algorithm that iterates the first half of the pyramid with whitespaces upto 'mid' times, and the
43+
/* lower half of the pyramid is decremented again after middle(mid). First, we keep a track of the
44+
/* middle of the pyramid. we increment the numbers with every iteration and decrease the whitespaces.
45+
/* once the outer loop reaches mid, we use an if statement to check if 'i' is greater than mid. if yes,
46+
/* we increment the whitespaces and decrement the digits with every iteration, same as above but opposite.
47+
/* @author : Pranay Kothapalli @kotAPI
48+
/**********************************************************/
49+
void printPattern_11(int input){
50+
// Outer loop loops input number of times
51+
int mid;
52+
// Check if the user input is even or odd, choose a valid mid point depending on the input
53+
if(input%2==0){
54+
// Mid/2 if the input is even. (if input is 8, mid is 4, 8/2 = 4).
55+
mid = input/2;
56+
}
57+
else{
58+
// Mid/2+1 if the input is odd. (if input is 9, mid is 5, 9/2+1 = 5).
59+
mid = input/2 +1;
60+
}
61+
62+
for(int i=1;i<input;i++){
63+
// if i hasn't reached the mid point of the pyramid
64+
// execute this block
65+
if(i<=mid){
66+
// decrementing white spaces on iteration depending on mid-i
67+
for(int j=mid-i;j>0;j--){
68+
cout<<" ";
69+
}
70+
// incrementing digits with every iteration depending on i
71+
for(int k=1;k<=i;k++){
72+
cout<<k;
73+
}
74+
}
75+
// if i is greater than mid.
76+
// execute this code block
77+
else{
78+
// Incrementing white spaces with every iteration depending on i-mid
79+
for(int j=1;j<=i-mid;j++){
80+
cout<<" ";
81+
}
82+
// decrementing the digits with every iteration depending on input-i. forming a reverse pyramid.
83+
for(int k=1;k<=input-i;k++){
84+
cout<<k;
85+
}
86+
}
87+
// Add a newline once the inner loops have finished executing.
88+
cout<<endl;
89+
}
90+
}

Pattern Algorithms/README.md

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,8 @@
128128
1 2
129129
1
130130

131-
### 12_PatternChallenge
132-
133-
134-
1
135-
1 2 1
136-
1 2 3 2 1
137-
1 2 3 4 3 2 1
138-
1 2 3 4 5 4 3 2 1
139-
1 2 3 4 5 6 5 4 3 2 1
140-
1 2 3 4 5 6 7 6 5 4 3 2 1
141-
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
142-
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
143131

144-
### 13_PatternChallenge
132+
### 12_PatternChallenge
145133

146134

147135
1
@@ -163,7 +151,7 @@
163151
1 2
164152
1
165153

166-
### 14_PatternChallenge
154+
### 13_PatternChallenge
167155

168156

169157

@@ -186,7 +174,7 @@
186174
1 2 1
187175
1
188176

189-
### 15_PatternChallenge
177+
### 14_PatternChallenge
190178

191179
1 2 3 4 5 5 4 3 2 1
192180
1 2 3 4 4 3 2 1
@@ -200,7 +188,7 @@
200188
1 2 3 4 5 5 4 3 2 1
201189

202190

203-
### 16_PatternChallenge
191+
### 15_PatternChallenge
204192

205193
p
206194
pr
@@ -216,7 +204,7 @@
216204
pr
217205
p
218206

219-
### 17_PatternChallenge
207+
### 16_PatternChallenge
220208
(Chessboard with varying width and height)
221209

222210
# # # #
@@ -229,7 +217,7 @@
229217
# # # #
230218

231219

232-
### 18_PatternChallenge
220+
### 17_PatternChallenge
233221

234222
* * * * * * * * * *
235223
*
@@ -253,4 +241,59 @@
253241
*
254242
* * * * * * * * * *
255243

256-
244+
TODO :
245+
246+
11111
247+
12221
248+
12321
249+
12221
250+
11111
251+
252+
1
253+
A B
254+
2 3 4
255+
C D E F
256+
5 6 7 8 9
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+

0 commit comments

Comments
 (0)