0% found this document useful (0 votes)
20 views16 pages

IT Skills Day 4

Uploaded by

Riya Riyaz
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)
20 views16 pages

IT Skills Day 4

Uploaded by

Riya Riyaz
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/ 16

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

IT SKILLS DAY 5
Student Name: Kumar Aryan UID: 21BCS3117
Branch: BE-CSE Section/Group: CC_643 B
Semester: 6th Date of Performance: 03.06.24
Subject Name: IT Skills
Question – 1: Implement Stack using Queues

Code:

#include <iostream>

using namespace std;

int main() {

int t;

scanf("%d", &t);

while(t--) {

int n;

scanf("%d", &n);

int A[n];

for (int i = 0; i < n; i++) {

scanf("%d", &A[i]);

if(A[0] < A[i]) {

A[0] = A[i];

printf("%d\n", A[0]);

return 0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
Output:

Question – 2: Simplify Path

Code:

class Solution {

public:

string simplifyPath(string path) {

string originalString = path;

originalString = originalString + "/";

string substringToReplace = "/./";

string newSubstring = "/";

size_t pos = originalString.find(substringToReplace);

while (pos != string::npos) {

originalString.replace(pos, substringToReplace.length(), newSubstring);

pos = originalString.find(substringToReplace);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

// Secondly Relacing All //

substringToReplace = "//";

newSubstring = "/";

pos = originalString.find(substringToReplace);

while (pos != string::npos) {

originalString.replace(pos, substringToReplace.length(), newSubstring);

pos = originalString.find(substringToReplace);

// Finally Replacing /../ and Previous path/Directory.

substringToReplace = "/../";

pos = originalString.find(substringToReplace);

while(pos != string::npos) {

int cnt = 0;

int newPos = 0;

for(int i=pos-1;i>=0;i--) {

if(originalString[i] != '/') {

cnt++;

newPos = i;

} else {

newPos = i;

cnt++;

break;

}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

originalString.replace(newPos, cnt, "");

pos = originalString.find(substringToReplace);

originalString.replace(pos, substringToReplace.length(), newSubstring);

pos = originalString.find(substringToReplace);

if(originalString.size()>1) {

if(originalString[originalString.size()-1]=='/') {

originalString.pop_back();

return originalString;

};

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Question – 3: Edit Distance


Code:
class Solution {
public:
int spaceoptimise(string& word1,string& word2){
vector<int> curr(word2.length()+1,0);
vector<int> next(word2.length()+1,0);
for(int j=0;j<word2.length();j++)
next[j]=word2.length()-j;
for(int i=word1.length()-1;i>=0;i--){
curr[word2.length()]=word1.length()-i;
for(int j=word2.length()-1;j>=0;j--){
int ans=0;
if(word1[i]==word2[j])
ans=next[j+1];
else{
int replaced=1+next[j+1];
int deleted=1+next[j];
int inserted=1+curr[j+1];
ans=min(replaced,min(deleted,inserted));
}
curr[j]=ans;
}
next=curr;
}
return next[0];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
int minDistance(string word1, string word2) {
if(word1.length()==0)
return word2.length();
if(word2.length()==0)
return word1.length();
return spaceoptimise(word1,word2);
}
};
Output:

Question – 4: Baseball Game

Code:
class Solution {
public:
int calPoints(vector<string>& operations);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

bool isNumber(string line, int &n);


};

int Solution::calPoints(vector<string>& operations) {


int i, size = operations.size(), n;
char *p;
vector<int> v;
for (i = 0; i < size; ++i) {

if (isNumber(operations[i], n) == true) {
v.push_back(n);
} else if (operations[i] == "+") {
v.push_back( v[v.size()-1] + v[v.size()-2] );
} else if (operations[i] == "D") {
v.push_back( 2*v[v.size()-1]);
} else {
v.pop_back();
}
}
size = v.size();
for (i = 0, n = 0; i < size; ++i) {
n+=v[i];
}
return n;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

bool Solution::isNumber(string line, int &n) {


char* p;
int i, size = line.size();
for (i = 0; i < size; ++i) {
if (i == 0 && line[i] == '-') {
continue;
} else if (line[i] < '0' || line[i] > '9') {
return false;
}
}
n = strtol(line.c_str(), &p, 10);
return true;
}

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Question – 5: Valid Parenthesis String


Code:
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

Output:

Question – 6: Design Circular Queue


Code:
class MyCircularQueue {
public:
vector<int> q;
int first;
int size;

MyCircularQueue(int k) {
q = vector<int>(k, 0);
first = 0;
size = 0;
}

bool enQueue(int value) {


if(isFull()) return false;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

q[(first + size) % q.size()] = value;


size++;
return true;
}

bool deQueue() {
if (isEmpty()) return false;
first = (first + 1) % q.size();
size--;
return true;
}

int Front() {
if (isEmpty()) return -1;
return q[first];
}

int Rear() {
if (isEmpty()) return -1;
return q[(first + size - 1) % q.size()];
}

bool isEmpty() {
return size == 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

bool isFull() {
return size == q.size();
}
};
Output:

Question – 7: Necklace
Code:
#include <iostream>
#include <queue>
using namespace std;

void rotateNecklace(queue<int>& necklace, int k) {


for (int i = 0; i < k; ++i) {
int pearl = necklace.front();
necklace.pop();
necklace.push(pearl);
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int main() {
int T, n, k;
cin >> T;
while (T--) {
cin >> n >> k;
queue<int> necklace;

// Read the integers on the pearls


for (int i = 0; i < n; i++) {
int pearlValue;
cin >> pearlValue;
necklace.push(pearlValue);
}

// Correct k to be within the bounds and rotate the queue


k %= n;
rotateNecklace(necklace, k);

// Print the new order of pearls


while (!necklace.empty()) {
cout << necklace.front() << " ";
necklace.pop();
}
cout << "\n";
}
return 0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
Output:

Question – 8: Droof Fires Bracket


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

int main() {
int t;
cin>>t;
while(t--){
string s;cin>>s;
int q;cin>>q;
int q_arr[q];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

for(int i=0;i<q;i++) cin>>q_arr[i];


stack<long long int> st ;
long long int n=s.size();
long long int ans_arr[n+1];ans_arr[n]=-1;
for(int i=n-1;i>=0;i--){
if (s[i]==')'){
st.push(i+1); ans_arr[i]=ans_arr[i+1];
}
else{
if (st.size()==0) ans_arr[i]=-1;
else{
ans_arr[i]=st.top();
st.pop();
}
}
}
for(int i=0;i<q;i++){
cout<<ans_arr[q_arr[i]-1]<<"\n";
}
}
return 0;
}

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like