Skip to content

Commit

Permalink
Added Lab-04 and Lab-05
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatkhanna1999 committed Jan 3, 2019
1 parent aeed3e9 commit d4125ee
Show file tree
Hide file tree
Showing 5 changed files with 413 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Lab-04/Lab-04q1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323
#define debug(x) cout<<"Case "<<x<<": "
#define For(i,n) for(long long i=0;i<n;i++)
#define Frabs(i,a,b) for(long long i = a; i < b; i++)
#define Frabr(i,a,b) for(long long i = a; i >=b; i--)
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector < pii > vpii;
typedef vector < pll > vpll;
typedef vector <string> vs;

//Handle:cyber_rajat


int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
sync;
queue<ll> que;
ll q;
cin>>q;
while(q--)
{
ll x;
cin>>x;
if(x==1)
{
ll a;
cin>>a;
que.push(a);
}
else if(x==2)
{
que.pop();
}
else
{
cout<<que.front()<<endl;
}
}
return 0;
}

134 changes: 134 additions & 0 deletions Lab-04/Lab-04q2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323
#define debug(x) cout<<"Case "<<x<<": "
#define For(i,n) for(long long i=0;i<n;i++)
#define Frabs(i,a,b) for(long long i = a; i < b; i++)
#define Frabr(i,a,b) for(long long i = a; i >=b; i--)
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector < pii > vpii;
typedef vector < pll > vpll;
typedef vector <string> vs;

//Handle:cyber_rajat

string ltrim(const string &);
string rtrim(const string &);

/*
* Complete the 'isSymmetric' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/

string isSymmetric(string s) {
int i=0;
stack<char>s1;
bool ans=true;
while(i<s.size())
{
if(s[i]=='(' || s[i]=='{' || s[i]=='[')
s1.push(s[i]);
else if(s[i]==')')
{
char c=s1.top();
s1.pop();
if(c!='(' )
{
ans=false;
break;
}
}
else if(s[i]==']')
{
char c=s1.top();
s1.pop();
if(c!='[')
{
ans=false;
break;
}
}
else if(s[i]=='}')
{
char c=s1.top();
s1.pop();
if(c!='{')
{
ans=false;
break;
}
}
i++;
}
if(!s1.empty())
ans=false;

if(ans==false)
{
return "NO";
}
else
{
return "YES";
}

}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string t_temp;
getline(cin, t_temp);

int t = stoi(ltrim(rtrim(t_temp)));

for (int t_itr = 0; t_itr < t; t_itr++) {
string s;
getline(cin, s);

string result = isSymmetric(s);

fout << result << "\n";
}

fout.close();

return 0;
}

string ltrim(const string &str) {
string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {
string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);

return s;
}

98 changes: 98 additions & 0 deletions Lab-04/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Lab-02

## Question-1

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed.

A basic queue has the following operations:

Enqueue: add a new element to the end of the queue.
Dequeue: remove the element from the front of the queue and return it.
In this challenge, you must first implement a queue using two stacks. Then process queries, where each query is one of the following types:

1 x: Enqueue element into the end of the queue.
2: Dequeue the element at the front of the queue.
3: Print the element at the front of the queue.
Input Format

The first line contains a single integer, , denoting the number of queries.
Each line of the subsequent lines contains a single query in the form described in the problem statement above. All three queries start with an integer denoting the query , but only query is followed by an additional space-separated value, , denoting the value to be enqueued.

Constraints

It is guaranteed that a valid answer always exists for each query of type .
Output Format

For each query of type , print the value of the element at the front of the queue on a new line.

Sample Input

10
1 42
2
1 14
3
1 28
3
1 60
1 78
2
2
Sample Output

14
14
Explanation

We perform the following sequence of actions:

Enqueue 42;queue={42} .
Dequeue the value at the head of the queue,42 ;queue= {}.
Enqueue 14;queue={14} .
Print the value at the head of the queue,14 ;queue={14} .
Enqueue 28;queue={24,28}.
Print the value at the head of the queue,14;queue={14,28} .
Enqueue 60;queue={14,28,60} .
Enqueue 78;queue={14,28,60,78}.
Dequeue the value at the head of the queue,14 ;queue={28,60,78} .
Dequeue the value at the head of the queue,28 ;queue={60,78}

## Question-2

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ].

Two brackets are considered to be a matched pair if the an opening bracket i.e., (, [, or { occurs to the left of a closing bracket i.e., ), ], or } of the exact same type. There are three types of matched pairs of brackets: [], {}, and ().

A matching pair of brackets is not symmetric if the set of brackets it encloses are not matched. For example, {[(])} is not symmetric because the contents in between { and } are not symmetric. The pair of square brackets encloses a single, unsymmetric opening bracket, (, and the pair of parentheses encloses a single, unsymmetric closing square bracket, ].

By this logic, we say a sequence of brackets is considered to be symmetric if the following conditions are met:

• It contains no unmatched brackets.

• The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets.

• Given strings of brackets, determine whether each sequence of brackets is symmetric. If a string is symmetric, print YES on a new line; otherwise, print NO on a new line.

Input Format

The first line contains a single integer n, denoting the number of strings. Each line of the subsequent lines consists of a single string,s, denoting a sequence of brackets.

Constraints

1 ≤ n ≤ 10^3 1 ≤ length(s) ≤ 10^3

Output Format

For each string of brackets, print YES or NO on a new line.

Sample Input 0

3
{[()]}
{[(})}
{{[[(())]]}}
Sample Output 0

YES
NO
YES
77 changes: 77 additions & 0 deletions Lab-05/Lab-05.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323
#define debug(x) cout<<"Case "<<x<<": "
#define For(i,n) for(long long i=0;i<n;i++)
#define Frabs(i,a,b) for(long long i = a; i < b; i++)
#define Frabr(i,a,b) for(long long i = a; i >=b; i--)
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector < pii > vpii;
typedef vector < pll > vpll;
typedef vector <string> vs;

//Handle:cyber_rajat

int main()
{
sync;
ll g;
cin>>g;
while(g--)
{
ll a1[100005]={0},a2[100005]={0};
queue<ll> q1,q2; //Making ques and vector
ll n,m,x;
cin>>n>>m>>x;
ll cnt=0,cnt1=0,cnt2=0,curr_sum=0,sum1=0,sum2=0;
for(ll i=0;i<n;i++)
{
ll a;
cin>>a;
a1[i]=a;
q1.push(a);
}
for(ll i=0;i<m;i++)
{
ll b;
cin>>b;
a2[i]=b;
q2.push(b);
}
ll k=0,v=0;
while(k<n && curr_sum+a1[k]<=x)
{
curr_sum+=a1[k];//summing one queue
k++;
cnt++;
}
while(v<m && k>=0)
{
curr_sum+=a2[v];//summing one and popping other
v++;
while(curr_sum>x && k>0)
{
k--;
curr_sum=curr_sum-a1[k];
}
if(curr_sum<=x)
{
cnt=max(cnt,k+v);
}
}
cout<<cnt<<endl;
}
return 0;
}

Loading

0 comments on commit d4125ee

Please sign in to comment.