Skip to content

Commit dcac8e1

Browse files
divyakhetanMadhavBahl
authored andcommitted
day 19 and 20 (#185)
1 parent 47df62a commit dcac8e1

File tree

5 files changed

+227
-1
lines changed

5 files changed

+227
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @author divyakhetan
3+
* @date 16/1/2019
4+
*/
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
int main(){
10+
cout << "Enter length of 1st and 2nd array";
11+
int n1, n2;
12+
cin >> n1 >> n2;
13+
int a[n1];
14+
int b[n2];
15+
for(int i = 0; i < n1; i++){
16+
cin >> a[i];
17+
18+
}
19+
20+
for(int i = 0; i < n2; i++){
21+
cin >> b[i];
22+
}
23+
24+
for(int i = 0; i < n1; i++){
25+
for(int j = 0; j < n2; j++){
26+
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
27+
}
28+
}
29+
return 0;
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @author divyakhetan
3+
* @date 16/1/2019
4+
*/
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
10+
void swap(int *a, int *b){
11+
int temp = *a;
12+
*a = *b;
13+
*b = temp;
14+
}
15+
16+
int main(){
17+
int n;
18+
cin >> n;
19+
int a[n];
20+
for(int i = 0; i < n; i++){
21+
cin >> a[i];
22+
}
23+
srand(time(NULL));
24+
for(int i = n - 1; i >= 0; i--){
25+
int num = rand() % (i + 1); //need to get an index from 0..i-1
26+
swap(a[i], a[num]);
27+
}
28+
cout << "The shuffled array is: ";
29+
for(int i = 0; i < n; i++){
30+
cout << a[i] << " ";
31+
}
32+
}

day19/README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![cover](./cover.png)
1+
![cover](./cover.png)
22

33
# Day 19 - Array Series Part 2
44

@@ -94,6 +94,43 @@ console.log (cartesian ([1, 2], []));
9494
console.log (cartesian ([1, 2, 3, 4], ['a', 'b', 'c']));
9595
```
9696

97+
### C++ Implementation
98+
99+
#### [Solution by @divyakhetan](./C++/CartesianProductDay19.cpp)
100+
101+
```cpp
102+
/**
103+
* @author divyakhetan
104+
* @date 16/1/2019
105+
*/
106+
107+
#include<bits/stdc++.h>
108+
using namespace std;
109+
110+
int main(){
111+
cout << "Enter length of 1st and 2nd array";
112+
int n1, n2;
113+
cin >> n1 >> n2;
114+
int a[n1];
115+
int b[n2];
116+
for(int i = 0; i < n1; i++){
117+
cin >> a[i];
118+
119+
}
120+
121+
for(int i = 0; i < n2; i++){
122+
cin >> b[i];
123+
}
124+
125+
for(int i = 0; i < n1; i++){
126+
for(int j = 0; j < n2; j++){
127+
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
128+
}
129+
}
130+
return 0;
131+
}
132+
```
133+
97134
### Python Implementation
98135

99136
### [Solution](./Python/cartesian_product.py)
@@ -125,6 +162,7 @@ def main():
125162
set_B = get_input('B')
126163
calculate_cartesian_product(set_A, set_B)
127164
```
165+
128166
***
129167

130168
## Ques B
@@ -172,6 +210,43 @@ function fisherYates (arr) {
172210
fisherYates ([1, 2, 3, 4, 5, 6]);
173211
```
174212

213+
### C++ Implementation
214+
215+
#### [Solution ](./C++/FisherYateShuffleDay19.cpp)
216+
217+
```cpp
218+
/**
219+
* @author divyakhetan
220+
* @date 16/1/2019
221+
*/
222+
223+
#include<bits/stdc++.h>
224+
using namespace std;
225+
226+
int main(){
227+
cout << "Enter length of 1st and 2nd array";
228+
int n1, n2;
229+
cin >> n1 >> n2;
230+
int a[n1];
231+
int b[n2];
232+
for(int i = 0; i < n1; i++){
233+
cin >> a[i];
234+
235+
}
236+
237+
for(int i = 0; i < n2; i++){
238+
cin >> b[i];
239+
}
240+
241+
for(int i = 0; i < n1; i++){
242+
for(int j = 0; j < n2; j++){
243+
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
244+
}
245+
}
246+
return 0;
247+
}
248+
```
249+
175250
### Python implementation
176251

177252
#### [Solution](./Python/fisher_yates.py)

day20/C++/PartitionDay20.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @author divyakhetan
3+
* @date 16/1/2019
4+
*/
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
10+
11+
12+
int main(){
13+
int n;
14+
cin >> n;
15+
int a[n];
16+
for(int i = 0; i < n; i++){
17+
cin >> a[i];
18+
}
19+
20+
int num;
21+
cin >> num;
22+
23+
int count = 0;
24+
vector<int> part;
25+
vector<vector <int> > ans;
26+
int last_added = 0;
27+
for(int i = 0; i < n; i++){
28+
if(count == num ){
29+
30+
ans.push_back(part);
31+
part.clear();
32+
part.push_back(a[i]);
33+
count = 1;
34+
}
35+
else{
36+
37+
count++;
38+
part.push_back(a[i]);
39+
}
40+
41+
}
42+
43+
ans.push_back(part); //for the remaining array
44+
//cout << ans.size();
45+
for(int i = 0; i < ans.size(); i++){
46+
for(int j = 0; j < ans[i].size(); j++){
47+
cout << ans[i][j] << " ";
48+
}
49+
cout << endl;
50+
}
51+
}

day20/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,41 @@ function partition (array, size) {
8686
return partitionedArray;
8787
}
8888
```
89+
## C++ Solution
90+
91+
### [Solution @divyakhetan](./C++/PartitionDay20.cpp)
92+
93+
94+
95+
```cpp
96+
/**
97+
* @author divyakhetan
98+
* @date 16/1/2019
99+
*/
100+
101+
#include<bits/stdc++.h>
102+
using namespace std;
103+
104+
int main(){
105+
cout << "Enter length of 1st and 2nd array";
106+
int n1, n2;
107+
cin >> n1 >> n2;
108+
int a[n1];
109+
int b[n2];
110+
for(int i = 0; i < n1; i++){
111+
cin >> a[i];
112+
113+
}
114+
115+
for(int i = 0; i < n2; i++){
116+
cin >> b[i];
117+
}
118+
119+
for(int i = 0; i < n1; i++){
120+
for(int j = 0; j < n2; j++){
121+
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
122+
}
123+
}
124+
return 0;
125+
}
126+
```

0 commit comments

Comments
 (0)