Skip to content
54 changes: 54 additions & 0 deletions day38/C++/profgrammer_stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* @author profgrammer
* @date 17-2-2019
*/

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

class MyStack{
private:
int top;
int stack[100];
int size;
public:
MyStack(int _size){
top = -1;
size = _size;
}

bool isEmpty(){
return top == -1;
}

bool isFull(){
return top == size - 1;
}

int peek(){
if(isEmpty()) {cout<<"Stack empty..\n"; return -1;}
return stack[top];
}

void push(int x){
if(isFull()) cout<<"Stack full..\n";
else stack[++top] = x;
}

void pop(){
if(isEmpty()) cout<<"Stack empty..\n";
else top--;
}
};

int main(){
MyStack stack(2);
stack.push(1);
stack.push(2);
stack.push(3); // err
cout<<stack.peek()<<endl;
stack.pop();
stack.pop();
stack.pop(); // err
cout<<stack.peek()<<endl;
}
63 changes: 62 additions & 1 deletion day38/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,65 @@ public class Stack {
while(c==1);
}
}
```
```

## C++ Implementation

### [Solution by @profgrammer](./C++/profgrammer_stack.cpp)

```cpp
/*
* @author profgrammer
* @date 17-2-2019
*/

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

class MyStack{
private:
int top;
int stack[100];
int size;
public:
MyStack(int _size){
top = -1;
size = _size;
}

bool isEmpty(){
return top == -1;
}

bool isFull(){
return top == size - 1;
}

int peek(){
if(isEmpty()) {cout<<"Stack empty..\n"; return -1;}
return stack[top];
}

void push(int x){
if(isFull()) cout<<"Stack full..\n";
else stack[++top] = x;
}

void pop(){
if(isEmpty()) cout<<"Stack empty..\n";
else top--;
}
};

int main(){
MyStack stack(2);
stack.push(1);
stack.push(2);
stack.push(3); // err
cout<<stack.peek()<<endl;
stack.pop();
stack.pop();
stack.pop(); // err
cout<<stack.peek()<<endl;
}
```
51 changes: 51 additions & 0 deletions day8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,55 @@ int main() {
}
```


=======
### [Solution 2 by @profgrammer](./Cpp/profgrammer_editdistance.cpp)
```cpp
/*
*@author: profgrammer
*@date: 31-12-2018
*/

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

string s1, s2;

int dp[1500][1500];

// function that returns minimum edits required to convert s1[0 .. i-1] into s2[0 .. j-1]
int minEdit(int i, int j){
// base cases. if s1 finishes we need j insertions, if s2 finishes we need i insertions
if(i == 0) return j;
if(j == 0) return i;
// housekeeping for dp with memoisation
if(dp[i][j] != inf) return dp[i][j];
// if the last characters are the same, no need to change anything and move both pointers by 1 unit
if(s1[i-1] == s2[j-1]) {
return dp[i][j] = minEdit(i-1, j-1);
}
// replace s1[i] to be s2[j]. the strings to be checked are still s1[0 .. i-1] and s2[0 .. j-1] but the cost is +1
int minReplace = 1 + minEdit(i-1, j-1);
// delete s1[i], cost is +1 and the strings are now s1[0 .. i-1] and s2[0 .. j] because we need to compare the i-1th character with the jth character
int minDelete = 1 + minEdit(i-1, j);
// insert a character into s1 at index i. cost is +1 and now the strings to be checked are s1[0 .. i] because the ith character isn't compared yet and s2[0 .. j-1]
int minInsert = 1 + minEdit(i, j-1);
// return min of these 3 values
return dp[i][j] = min(minReplace, min(minDelete, minInsert));
}

int main() {
for(int i = 0;i < 1500;i++){
for(int j = 0;j < 1500;j++) dp[i][j] = inf;
}
cin>>s1>>s2;
cout<<"The minimum edit distance is: ";
cout<<minEdit(s1.size(), s2.size())<<endl;
}
```


### [Solution 3 by @divyakhetan](./Cpp/EditDistanceday8.cpp)

```cpp
Expand Down Expand Up @@ -249,6 +298,7 @@ int main(){

### [Solution 4 by @aaditkamat] (./C++/levenshtein_distance.cpp)


```cpp
/**
* @author: aaditkamat
Expand Down Expand Up @@ -291,6 +341,7 @@ int main() {
}
```


### C Implementation

### [Solution 1](./C/levenshtein_distance.c)
Expand Down