Skip to content

Commit 5b36f05

Browse files
Add cpp code for day 38 stack (#256)
* cpp code for day 7 and 8 * cpp code for day 7 and 8 * fixed readme merge conflict * added cpp code for day 38 Co-authored-by: MADHAV BAHL <madhavbahl10@gmail.com>
1 parent 6898670 commit 5b36f05

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

day38/C++/profgrammer_stack.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @author profgrammer
3+
* @date 17-2-2019
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
9+
class MyStack{
10+
private:
11+
int top;
12+
int stack[100];
13+
int size;
14+
public:
15+
MyStack(int _size){
16+
top = -1;
17+
size = _size;
18+
}
19+
20+
bool isEmpty(){
21+
return top == -1;
22+
}
23+
24+
bool isFull(){
25+
return top == size - 1;
26+
}
27+
28+
int peek(){
29+
if(isEmpty()) {cout<<"Stack empty..\n"; return -1;}
30+
return stack[top];
31+
}
32+
33+
void push(int x){
34+
if(isFull()) cout<<"Stack full..\n";
35+
else stack[++top] = x;
36+
}
37+
38+
void pop(){
39+
if(isEmpty()) cout<<"Stack empty..\n";
40+
else top--;
41+
}
42+
};
43+
44+
int main(){
45+
MyStack stack(2);
46+
stack.push(1);
47+
stack.push(2);
48+
stack.push(3); // err
49+
cout<<stack.peek()<<endl;
50+
stack.pop();
51+
stack.pop();
52+
stack.pop(); // err
53+
cout<<stack.peek()<<endl;
54+
}

day38/README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,65 @@ public class Stack {
278278
while(c==1);
279279
}
280280
}
281-
```
281+
```
282+
283+
## C++ Implementation
284+
285+
### [Solution by @profgrammer](./C++/profgrammer_stack.cpp)
286+
287+
```cpp
288+
/*
289+
* @author profgrammer
290+
* @date 17-2-2019
291+
*/
292+
293+
#include <bits/stdc++.h>
294+
using namespace std;
295+
296+
class MyStack{
297+
private:
298+
int top;
299+
int stack[100];
300+
int size;
301+
public:
302+
MyStack(int _size){
303+
top = -1;
304+
size = _size;
305+
}
306+
307+
bool isEmpty(){
308+
return top == -1;
309+
}
310+
311+
bool isFull(){
312+
return top == size - 1;
313+
}
314+
315+
int peek(){
316+
if(isEmpty()) {cout<<"Stack empty..\n"; return -1;}
317+
return stack[top];
318+
}
319+
320+
void push(int x){
321+
if(isFull()) cout<<"Stack full..\n";
322+
else stack[++top] = x;
323+
}
324+
325+
void pop(){
326+
if(isEmpty()) cout<<"Stack empty..\n";
327+
else top--;
328+
}
329+
};
330+
331+
int main(){
332+
MyStack stack(2);
333+
stack.push(1);
334+
stack.push(2);
335+
stack.push(3); // err
336+
cout<<stack.peek()<<endl;
337+
stack.pop();
338+
stack.pop();
339+
stack.pop(); // err
340+
cout<<stack.peek()<<endl;
341+
}
342+
```

day8/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,55 @@ int main() {
200200
}
201201
```
202202

203+
204+
=======
205+
### [Solution 2 by @profgrammer](./Cpp/profgrammer_editdistance.cpp)
206+
```cpp
207+
/*
208+
*@author: profgrammer
209+
*@date: 31-12-2018
210+
*/
211+
212+
#include <bits/stdc++.h>
213+
#define inf 100000000
214+
using namespace std;
215+
216+
string s1, s2;
217+
218+
int dp[1500][1500];
219+
220+
// function that returns minimum edits required to convert s1[0 .. i-1] into s2[0 .. j-1]
221+
int minEdit(int i, int j){
222+
// base cases. if s1 finishes we need j insertions, if s2 finishes we need i insertions
223+
if(i == 0) return j;
224+
if(j == 0) return i;
225+
// housekeeping for dp with memoisation
226+
if(dp[i][j] != inf) return dp[i][j];
227+
// if the last characters are the same, no need to change anything and move both pointers by 1 unit
228+
if(s1[i-1] == s2[j-1]) {
229+
return dp[i][j] = minEdit(i-1, j-1);
230+
}
231+
// 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
232+
int minReplace = 1 + minEdit(i-1, j-1);
233+
// 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
234+
int minDelete = 1 + minEdit(i-1, j);
235+
// 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]
236+
int minInsert = 1 + minEdit(i, j-1);
237+
// return min of these 3 values
238+
return dp[i][j] = min(minReplace, min(minDelete, minInsert));
239+
}
240+
241+
int main() {
242+
for(int i = 0;i < 1500;i++){
243+
for(int j = 0;j < 1500;j++) dp[i][j] = inf;
244+
}
245+
cin>>s1>>s2;
246+
cout<<"The minimum edit distance is: ";
247+
cout<<minEdit(s1.size(), s2.size())<<endl;
248+
}
249+
```
250+
251+
203252
### [Solution 3 by @divyakhetan](./Cpp/EditDistanceday8.cpp)
204253
205254
```cpp
@@ -249,6 +298,7 @@ int main(){
249298

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

301+
252302
```cpp
253303
/**
254304
* @author: aaditkamat
@@ -291,6 +341,7 @@ int main() {
291341
}
292342
```
293343
344+
294345
### C Implementation
295346
296347
### [Solution 1](./C/levenshtein_distance.c)

0 commit comments

Comments
 (0)