Skip to content

Commit 739efab

Browse files
committed
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
2 parents 50cc417 + bbd75f4 commit 739efab

File tree

5 files changed

+183
-2
lines changed

5 files changed

+183
-2
lines changed

day15/C++/profgrammer_pascal.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
*@author: profgrammer
3+
*@date: 09-01-2019
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
9+
int ncr(int n, int r){
10+
if(n < r || n < 0 || r < 0) return 0;
11+
if(n == r || r == 0) return 1;
12+
return ncr(n-1,r) + ncr(n-1, r-1);
13+
}
14+
15+
int main() {
16+
int n;
17+
cin>>n;
18+
for(int i = 0;i <= n;i++){
19+
for(int j = 0;j <= i;j++) cout<<ncr(i,j)<<" ";
20+
cout<<endl;
21+
}
22+
}

day15/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,33 @@ int main(){
8282
}
8383
```
8484
85+
## [C++ Solution by @profgrammer](./C++/profgrammer_pascal.cpp)
86+
87+
```cpp
88+
/*
89+
*@author: profgrammer
90+
*@date: 09-01-2019
91+
*/
92+
93+
#include <bits/stdc++.h>
94+
using namespace std;
95+
96+
int ncr(int n, int r){
97+
if(n < r || n < 0 || r < 0) return 0;
98+
if(n == r || r == 0) return 1;
99+
return ncr(n-1,r) + ncr(n-1, r-1);
100+
}
101+
102+
int main() {
103+
int n;
104+
cin>>n;
105+
for(int i = 0;i <= n;i++){
106+
for(int j = 0;j <= i;j++) cout<<ncr(i,j)<<" ";
107+
cout<<endl;
108+
}
109+
}
110+
```
111+
85112
## Python Implementation
86113

87114
### [Solution] (./Python/pascal.py)
@@ -189,4 +216,5 @@ def main():
189216
print(f'Pascal triangle cannot have {num} rows')
190217

191218
main()
219+
192220
```

day17/C++/Day17.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @author : dhruv-gupta14
3+
* @date : 12/01/2019
4+
*/
5+
6+
#include<iostream>
7+
using namespace std;
8+
#define N 8
9+
10+
void printBoard(int board[N][N]) {
11+
for (int i = 0; i < N; i++) {
12+
for (int j = 0; j < N; j++)
13+
cout << board[i][j] << " ";
14+
cout << endl;
15+
}
16+
}
17+
18+
bool isValid(int board[N][N], int row, int col) {
19+
for (int i = 0; i < col; i++) //check whether there is queen in the left or not
20+
if (board[row][i])
21+
return false;
22+
for (int i=row, j=col; i>=0 && j>=0; i--, j--)
23+
if (board[i][j]) //check whether there is queen in the left upper diagonal or not
24+
return false;
25+
for (int i=row, j=col; j>=0 && i<N; i++, j--)
26+
if (board[i][j]) //check whether there is queen in the left lower diagonal or not
27+
return false;
28+
return true;
29+
}
30+
31+
bool solveNQueen(int board[N][N], int col) {
32+
if (col >= N) //when N queens are placed successfully
33+
return true;
34+
for (int i = 0; i < N; i++) { //for each row, check placing of queen is possible or not
35+
if (isValid(board, i, col) ) {
36+
board[i][col] = 1; //if validate, place the queen at place (i, col)
37+
if ( solveNQueen(board, col + 1)) //Go for the other columns recursively
38+
return true;
39+
40+
board[i][col] = 0; //When no place is vacant remove that queen
41+
}
42+
}
43+
return false;
44+
}
45+
46+
bool checkSolution() {
47+
int board[N][N];
48+
for(int i = 0; i<N; i++)
49+
for(int j = 0; j<N; j++)
50+
board[i][j] = 0;
51+
52+
if ( solveNQueen(board, 0) == false ) {
53+
cout << "Solution does not exist";
54+
return false;
55+
}
56+
printBoard(board);
57+
return true;
58+
}
59+
60+
int main() {
61+
checkSolution();
62+
}

day17/README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,73 @@ function printBoard (chessBoard) {
139139
}
140140

141141
nqueens (8);
142-
```
142+
```
143+
144+
## C++ Implementation
145+
146+
### [Solution 1](./C++/Day17.cpp)
147+
148+
```cpp
149+
/*
150+
* @author : dhruv-gupta14
151+
* @date : 12/01/2019
152+
*/
153+
154+
#include<iostream>
155+
using namespace std;
156+
#define N 8
157+
158+
void printBoard(int board[N][N]) {
159+
for (int i = 0; i < N; i++) {
160+
for (int j = 0; j < N; j++)
161+
cout << board[i][j] << " ";
162+
cout << endl;
163+
}
164+
}
165+
166+
bool isValid(int board[N][N], int row, int col) {
167+
for (int i = 0; i < col; i++) //check whether there is queen in the left or not
168+
if (board[row][i])
169+
return false;
170+
for (int i=row, j=col; i>=0 && j>=0; i--, j--)
171+
if (board[i][j]) //check whether there is queen in the left upper diagonal or not
172+
return false;
173+
for (int i=row, j=col; j>=0 && i<N; i++, j--)
174+
if (board[i][j]) //check whether there is queen in the left lower diagonal or not
175+
return false;
176+
return true;
177+
}
178+
179+
bool solveNQueen(int board[N][N], int col) {
180+
if (col >= N) //when N queens are placed successfully
181+
return true;
182+
for (int i = 0; i < N; i++) { //for each row,check placing of queen is possible or not
183+
if (isValid(board, i, col) ) {
184+
board[i][col] = 1; //if validate, place the queen at place (i, col)
185+
if ( solveNQueen(board, col + 1)) //Go for the other columns recursively
186+
return true;
187+
188+
board[i][col] = 0; //When no place is vacant remove that queen
189+
}
190+
}
191+
return false;
192+
}
193+
194+
bool checkSolution() {
195+
int board[N][N];
196+
for(int i = 0; i<N; i++)
197+
for(int j = 0; j<N; j++)
198+
board[i][j] = 0;
199+
200+
if ( solveNQueen(board, 0) == false ) {
201+
cout << "Solution does not exist";
202+
return false;
203+
}
204+
printBoard(board);
205+
return true;
206+
}
207+
208+
int main() {
209+
checkSolution();
210+
}
211+
```

day8/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ int main() {
288288
getline(cin, first);
289289
getline(cin, second);
290290
cout << "The Levenshtein distance between \"" << first << "\" and \"" << second << "\" is: " << dist(first, second) << endl;
291-
}
291+
}
292292
```
293293

294294
### C Implementation

0 commit comments

Comments
 (0)