0% found this document useful (0 votes)
10 views

Assignment 5

Uploaded by

Anmol Mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Assignment 5

Uploaded by

Anmol Mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Assignment - 5
Student Name: Anmol Mittal UID: 21BCS9235
Branch: BE-CSE Section/Group:21BCS_FL_602/B
Semester: 6th Date of Performance: 03/06/2024
Subject Name: IT Skills

1. Implement stack using queues:


class MyStack {
public:
queue<int> q;
MyStack() {
}
void push(int x) {
q.push(x);
for(int i=0; i<q.size()-1; i++){
q.push(q.front());
q.pop();
}
}
int pop() {
int a= q.front();
q.pop();
return a;
}
int top() {
return q.front();
}
bool empty() {
return q.empty();
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

2. Simplify path:
class Solution {
public:
string simplifyPath(string path) {
stack<string> s;
stringstream ss(path);
string dir;
while (getline(ss, dir, '/')) {
if (dir.empty() || dir == ".") {
continue;
} else if (dir == "..") {
if (!s.empty()) {
s.pop();
}
} else {
s.push(dir);
}
}
string res;
while (!s.empty()) {
res = "/" + s.top() + res;
s.pop();
}
return res.empty() ? "/" : res;
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Edit Distance:

class Solution {
public:
int minDistance(string word1, string word2) {
const int m = word1.length();//first word length
const int n = word2.length();//second word length
// dp[i][j] := min # of operations to convert word1[0..i) to word2[0..j)
vector<vector<int>> dp(m + 1, vector<int>(n + 1));

for (int i = 1; i <= m; ++i)


dp[i][0] = i;

for (int j = 1; j <= n; ++j)


dp[0][j] = j;

for (int i = 1; i <= m; ++i)


for (int j = 1; j <= n; ++j)
if (word1[i - 1] == word2[j - 1])//same characters
dp[i][j] = dp[i - 1][j - 1];//no operation
else
dp[i][j] = min({dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]}) + 1;
//replace //delete //insert
return dp[m][n];
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Baseball Game:
class Solution {
public:
int calPoints(vector<string>& opr) {
vector<int>v;
for(int i=0;i<opr.size();i++){
int sz=v.size();
if(opr[i]!="D" && opr[i]!="C" && opr[i]!="+"){
v.emplace_back(stoi(opr[i]));
}
if(opr[i]=="+"){
v.emplace_back(v[sz-1]+v[sz-2]);
}
if(opr[i]=="D"){
v.emplace_back(2*v[sz-1]);
}
if(opr[i]=="C"){
v.pop_back();
}
}
long long ans=accumulate(v.begin(),v.end(),0);
return ans;

}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Valid Parenthesis String:

class Solution {
public:
bool checkValidString(string s) {
int leftMin = 0, leftMax = 0;

for (char c : s) {
if (c == '(') {
leftMin++;
leftMax++;
} else if (c == ')') {
leftMin--;
leftMax--;
} else {
leftMin--;
leftMax++;
}
if (leftMax < 0) return false;
if (leftMin < 0) leftMin = 0;
}

return leftMin == 0;
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Design Circular Queue:


class Node{
public:
int data;
Node* next;
Node(int data){
this->data=data;
this->next=NULL;
}
};
class MyCircularQueue {
public:
Node *head,*last;
int curr,maxi;
MyCircularQueue(int k) {
head=NULL;
last=NULL;
curr=0;
maxi=k;
}

bool enQueue(int value) {


if(curr==maxi)return false;
if(head==NULL){
head=new Node(value);
last=head;
}
else{
Node *newnode=new Node(value);
last->next=newnode;
last=last->next;
}
curr++;
return true;
}
bool deQueue() {
if(curr==0)return false;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

head=head->next;
curr--;
return true;
}

int Front() {
if(curr==0)return -1;
return head->data;
}

int Rear() {
if(curr==0)return -1;
return last->data;
}

bool isEmpty() {
return curr==0;
}

bool isFull() {
return curr==maxi;
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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

int main() {
int N;
cin>>N;
while(N--)
{
int n, k, in;
cin>>n>>k;
queue<int> first;
queue<int> final;
for(int i=0;i<k;i++)
{
cin>>in;
first.push(in);
}
for(int i=k;i<n;i++)
{
cin>>in;
final.push(in);
}
while(!first.empty())
{
final.push(first.front());
first.pop();
}
while(!final.empty())
{
cout<<final.front()<<" ";
final.pop();
}
cout<<endl;
}
return 0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

8. Droof Fires Brackets:


#include <stdio.h>
#include<string.h>
int main(){
long int t,i,k,l,b[10000000],a[10000000],q,x,j;
char s[10000000];
scanf("%ld ",&t);
for(i=0;i<t;i++){
scanf("%s ",s);
l=strlen(s);
k=0;
for(j=0;j<l;j++){
b[j]=-1;
}
for(j=0;j<l;j++){
if(s[j]=='('){
a[k]=j;
k++;
}
if(s[j]==')'){
if(k>0){
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

b[a[k-1]]=j;
k--;
}
}
}

for(j=l-1;j>=0;j--){
if(b[j]!=-1)
break;
}
for(k=j-1;k>=0;k--){
if(b[k]==-1){
if(s[k]==')'&&b[k+1]!=-1){
b[k]=b[k+1];
}
}
}
scanf("%ld",&q);
for(j=0;j<q;j++){
scanf("%ld",&x);
if(b[x-1]!=-1){
printf("%ld\n",b[x-1]+1);
}
else
printf("-1\n");
}
}
return 0;
}

You might also like