Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions day19/C++/CartesianProductDay19.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @author divyakhetan
* @date 16/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
cout << "Enter length of 1st and 2nd array";
int n1, n2;
cin >> n1 >> n2;
int a[n1];
int b[n2];
for(int i = 0; i < n1; i++){
cin >> a[i];

}

for(int i = 0; i < n2; i++){
cin >> b[i];
}

for(int i = 0; i < n1; i++){
for(int j = 0; j < n2; j++){
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
}
}
return 0;
}
32 changes: 32 additions & 0 deletions day19/C++/FisherYateShuffleDay19.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @author divyakhetan
* @date 16/1/2019
*/

#include<bits/stdc++.h>
using namespace std;


void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}

int main(){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
srand(time(NULL));
for(int i = n - 1; i >= 0; i--){
int num = rand() % (i + 1); //need to get an index from 0..i-1
swap(a[i], a[num]);
}
cout << "The shuffled array is: ";
for(int i = 0; i < n; i++){
cout << a[i] << " ";
}
}
77 changes: 76 additions & 1 deletion day19/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![cover](./cover.png)
![cover](./cover.png)

# Day 19 - Array Series Part 2

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

### C++ Implementation

#### [Solution by @divyakhetan](./C++/CartesianProductDay19.cpp)

```cpp
/**
* @author divyakhetan
* @date 16/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
cout << "Enter length of 1st and 2nd array";
int n1, n2;
cin >> n1 >> n2;
int a[n1];
int b[n2];
for(int i = 0; i < n1; i++){
cin >> a[i];

}

for(int i = 0; i < n2; i++){
cin >> b[i];
}

for(int i = 0; i < n1; i++){
for(int j = 0; j < n2; j++){
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
}
}
return 0;
}
```

### Python Implementation

### [Solution](./Python/cartesian_product.py)
Expand Down Expand Up @@ -125,6 +162,7 @@ def main():
set_B = get_input('B')
calculate_cartesian_product(set_A, set_B)
```

***

## Ques B
Expand Down Expand Up @@ -172,6 +210,43 @@ function fisherYates (arr) {
fisherYates ([1, 2, 3, 4, 5, 6]);
```

### C++ Implementation

#### [Solution ](./C++/FisherYateShuffleDay19.cpp)

```cpp
/**
* @author divyakhetan
* @date 16/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
cout << "Enter length of 1st and 2nd array";
int n1, n2;
cin >> n1 >> n2;
int a[n1];
int b[n2];
for(int i = 0; i < n1; i++){
cin >> a[i];

}

for(int i = 0; i < n2; i++){
cin >> b[i];
}

for(int i = 0; i < n1; i++){
for(int j = 0; j < n2; j++){
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
}
}
return 0;
}
```

### Python implementation

#### [Solution](./Python/fisher_yates.py)
Expand Down
51 changes: 51 additions & 0 deletions day20/C++/PartitionDay20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @author divyakhetan
* @date 16/1/2019
*/

#include<bits/stdc++.h>
using namespace std;




int main(){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}

int num;
cin >> num;

int count = 0;
vector<int> part;
vector<vector <int> > ans;
int last_added = 0;
for(int i = 0; i < n; i++){
if(count == num ){

ans.push_back(part);
part.clear();
part.push_back(a[i]);
count = 1;
}
else{

count++;
part.push_back(a[i]);
}

}

ans.push_back(part); //for the remaining array
//cout << ans.size();
for(int i = 0; i < ans.size(); i++){
for(int j = 0; j < ans[i].size(); j++){
cout << ans[i][j] << " ";
}
cout << endl;
}
}
38 changes: 38 additions & 0 deletions day20/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,41 @@ function partition (array, size) {
return partitionedArray;
}
```
## C++ Solution

### [Solution @divyakhetan](./C++/PartitionDay20.cpp)



```cpp
/**
* @author divyakhetan
* @date 16/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
cout << "Enter length of 1st and 2nd array";
int n1, n2;
cin >> n1 >> n2;
int a[n1];
int b[n2];
for(int i = 0; i < n1; i++){
cin >> a[i];

}

for(int i = 0; i < n2; i++){
cin >> b[i];
}

for(int i = 0; i < n1; i++){
for(int j = 0; j < n2; j++){
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
}
}
return 0;
}
```