0% found this document useful (0 votes)
4 views40 pages

21bcs1560_harsh Patil_advanced Programming Lab 1

The document outlines various experiments conducted in an Advanced Programming Lab course, detailing the implementation of concepts such as dynamic arrays, stacks, queues, linked lists, searching and sorting techniques, graphs, tree data structures, and strings. Each experiment includes objectives, code implementations, and learning outcomes. The document is submitted by a student named Harsh Patil with UID 21BCS1560.

Uploaded by

harshpatil01403
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)
4 views40 pages

21bcs1560_harsh Patil_advanced Programming Lab 1

The document outlines various experiments conducted in an Advanced Programming Lab course, detailing the implementation of concepts such as dynamic arrays, stacks, queues, linked lists, searching and sorting techniques, graphs, tree data structures, and strings. Each experiment includes objectives, code implementations, and learning outcomes. The document is submitted by a student named Harsh Patil with UID 21BCS1560.

Uploaded by

harshpatil01403
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/ 40

UNIVERSITY INSTITUTE OF ENGINEERING

Department of Computer Science & Engineering

Subject Name: Advance Programming Lab

Subject Code: 21CSP-314

Submitted to: Submitted by:

Barinderjit Kaur Name: harsh patil

UID: 21BCS1560
INDEX

Ex. List of Experiments Conduct Viva Record Total Remarks/Signature


No (MM: 12) (MM: 10) (MM: 8) (MM: 30)

1.1 To implement the concept of


Dynamic Array.

1.2 To implement the concept of


Stack and Queues.

1.3 Demonstrate the concept of


Linked List.

1.4 Implement the concept of


Searching and Sorting
techniques.

2.1 To implement the concept of


Graphs.

2.2 To implement the concept of


Tree Data Structure.

2.3 Demonstrate the concept of


string.

3.1 Implement the problems


based on Dynamic
Programming.

3.2 Implement the problems


based on Backtracking

3.3 Implement the problems


based on Branch and Bound
Experiment 1.1
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: To implement the concept of Dynamic Array.


Objective:
(a) https://www.hackerrank.com/challenges/dynamic-array/problem
(b) https://www.hackerrank.com/challenges/array-left-rotation/problem

Code:
(a) #include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int nseq, seq, size;
int nquery;
int lostans, a;
cin>>nseq;
cin>>nquery;
vector<vector<int>> v(nseq, vector<int>());
vector<int> q;

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


for(int j=0; j<3; j++){
cin>>a;
q.push_back(a);
}
if (q[0] == 1) {
v[(q[1] ^ lostans) % nseq].push_back(q[2]);
} else {
seq = (q[1] ^ lostans) % nseq;
size = v[seq].size();
lostans = v[seq][q[2] % size];
cout<<v[seq][q[2] % size]<<endl;
}
q.clear();
}
return 0;
}
(b)
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
unsigned long long int n,d;
cin>>n>>d;
unsigned long long int a[n];
unsigned long long int i;
for(i=0;i<n;i++)
cin>>a[i];
for(i=d;i<n;i++)
cout<<a[i]<<" ";
for(i=0;i<d;i++)
cout<<a[i]<<" ";
return 0;
}
Output:
(a)

(b)
Learning outcomes:
1. Implementation and the concept of Dynamic Array.
Experiment 1.2
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: To implement the concept of Stack and Queues.


Objective:
(a) https://www.hackerrank.com/challenges/equal-stacks/leaderboard
(b) https://www.hackerrank.com/challenges/queue-using-two-stacks/problem
Code:
(a) #include <bits/stdc++.h>
using namespace std;
int A[100005], B[100005], C[100005];
int main() {
int a, b, c;
cin >> a >> b >> c;
int sumA = 0, sumB = 0, sumC = 0;
for(int i=1; i<=a; i++)
{
cin >> A[i];
sumA+= A[i];
}
for(int i=1; i<=b; i++)
{
cin >> B[i];
sumB+= B[i];
}
for(int i=1; i<=c; i++)
{
cin >> C[i];
sumC+= C[i];
}
a = 1, b = 1, c = 1;
while(sumA!=sumB || sumB!=sumC || sumC!=sumA)
{
if(sumA>=sumC && sumA>=sumB)
{
sumA-= A[a];
a++;
}
else if(sumB>=sumC && sumB>=sumA)
{
sumB-= B[b];
b++;
}
else
{
sumC-= C[c];
c++;
}
}

cout << sumA << endl;


return 0;
}
(b) #include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
queue<int>q;
int n;
cin>>n;
while(n--){
int t;
cin>>t;
if(t==1){
cin>>t;
q.push(t);

}
else if(t==2){q.pop();}
else{
cout<<q.front()<<endl;
}
}
return 0;
}
Output:
(a)
(b)

Learning outcomes:
Implementation of stacks and queues
Experiment 1.3
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: Demonstrate the concept of Linked List.


Objective: https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-
list/problem
Code:
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* Insert(Node *head,int data)
{
Node *n = new Node();
n->data=data;
if(head==NULL)
{
return head=n;
}
else
{
Node *rhead = head;
while(head->next!=NULL)
{
head=head->next;
}
head->next =n;
return rhead;
}
}
Output:

Learning outcomes:
Concept of linked list
Experiment 1.4
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: Implement the concept of Searching and Sorting techniques.


Objective:
(a) https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-
linked-list/problem
(b) https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem
Code:
(a) #include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define MAXE 210
int A[200010];
int F[MAXE];
int median2(int D) {
int p = 0;
for (int i = 0; i < MAXE; i++) {
p += F[i];
if (p * 2 > D) {
return 2 * i;
} else if (p * 2 == D) { for (int j = i + 1; ; j++) { if (F[j]) {
return i + j;
}
}
}
}
return -1;
}
int main() {
int N, D;
cin >> N >> D;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
int result = 0;
for (int i = 0; i < N; i++) {
if (i >= D) {
if (A[i] >= median2(D)) {
++result;
}
F[A[i - D]]--;
}
F[A[i]]++;
}
cout << result << endl;
return 0;
}
(b)
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

int main() {
long long n,m,temp;
cin>>n;
vector<int> a;
for(long long i=0;i<n;i++) {
cin >> temp;
a.push_back(temp);
}
cin>>m;
vector<int> b;
for(long long i=0;i<m;i++){
cin >> temp;
b.push_back(temp);
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
long long i=0,j=0;
while(i<n && j<m){
if(a[i]==b[j]) {
i++;j++;
b[j-1]=0;
}
else if(a[i]>b[j])j++;
else i++;
}
set<int> st;
for(i=0;i<m;i++) {

if(b[i]!=0) st.insert(b[i]);
}
for(set<int>::iterator it = st.begin();it!=st.end();it++){ cout<<*it<<" ";
}
cout<<endl;
return 0;
}
Output:
(a)
(b)

Learning outcomes: Implement the concept of Searching and Sorting techniques.


Experiment 2.1
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: To implement the concept of Graphs.


Objective:
https://www.hackerrank.com/challenges/dijkstrashortreach/problem
Code:
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cctype>
#include <limits>
#include <climits>
#include <fstream>
using namespace std;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vector<int> > vvi;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define DEC(i,a,b) for(int i=(a); i>=b; --i)
#define FORV(v,i) for(int i=0;i<v.size();++i)
#define FORD(v,i) for(int i=v.size()-1;i>=0;--i)
#define all(c) (c).begin(),(c).end()
#define sz(c) int((c).size())
#define pb push_back
#define ull unsigned long long
#define ll long long
#define MOD 1000000007ULL
typedef vector<vector<pair<int,int> > > Graph;
class Comparator
{
public:
int operator() ( const pair<int,int>& p1, const pair<int,int> &p2)
{
return p1.second>p2.second;
}
};
void dijkstra(const Graph &G,const int &S,vector<int> &d)
{
d.resize(G.size(),INT_MAX);
priority_queue<pair<int,int>, vector<pair<int,int> >, Comparator> Q;
d[S] = 0;
Q.push(make_pair(S,d[S]));
while(!Q.empty())
{
int u = Q.top().first;
Q.pop();
for(unsigned int i=0; i < G[u].size(); i++)
{
int v = G[u][i].first;
int w = G[u][i].second;
if(d[v] > d[u]+w)
{
d[v] = d[u]+w;
Q.push(make_pair(v,d[v]));
}
}
}
}
int main()
{
int t;
ios_base::sync_with_stdio(false);
cin>>t;
stringstream ss;
FOR(i,0,t)
{
int n,m;
cin>>n>>m;
Graph V(n);
FOR(j,0,m)
{
int x,y,w;
cin>>x>>y>>w;
x--;
y--;
V[x].push_back(make_pair(y,w));
V[y].push_back(make_pair(x,w));
}
int S;
cin>>S;
S--;
vector<int> d;
dijkstra(V,S,d);
FORV(d,j)
{
if(j!=S)
{
if(d[j] == INT_MAX)
ss<<-1<<" ";
else
ss<<d[j]<<" ";
}
}
ss<<"\n";
}
cout<<ss.str();
return 0;
}
Output:

Learning outcomes: implement the concept of Graphs.


Experiment 2.2
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: To implement the concept of Tree Data Structure.


Objective:
https://www.hackerrank.com/contests/101hack33/challenges/longest-
path/problem
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N = 100010;
int col[N];
class node
{
public :
vi near;
};
node a[N];
bool bf1[N],bf2[N];
int dis[N];
int alpha;
int res;
void bfs1(int j)
{
queue<int> q;
q.push(j);
bf1[j]=1;
while(!q.empty())
{
alpha = q.front();
q.pop();
vi v = a[alpha].near;
for(int i=0;i<v.size();i++)
{
if(!bf1[v[i]])
{
bf1[v[i]]=1;
q.push(v[i]);
}
}
}
}
void bfs2()
{
queue<int> q;
q.push(alpha);
dis[alpha]=0;
bf2[alpha]=1;
while(!q.empty())
{
int top = q.front();
res=max(res,dis[top]+1);
q.pop();
vi v = a[top].near;
for(int i=0;i<v.size();i++)
{
if(!bf2[v[i]])
{
bf2[v[i]]=1;
dis[v[i]]=dis[top]+1;
q.push(v[i]);
}
}
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%d",&col[i]);
}
for(int i=2;i<=n;i++)
{
int x;
scanf("%d",&x);
if( (col[x]==1)&&(col[i]==1))
{
a[i].near.push_back(x);
a[x].near.push_back(i);
}
}
res=0;
for(int i=1;i<=n;i++)
{
if(col[i]==0)continue;
if(bf1[i])
continue;
bfs1(i);
bfs2();
}
cout<<res<<endl;
}
Output:

Learning outcomes:
implement the concept of Tree Data Structure.
Experiment 2.3
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: Demonstrate the concept of string.


Objective: https://www.hackerrank.com/challenges/strong-
password/problem
Code:
#include<bits/stdc++.h>
#define rep(i,start,lim) for(lld i=start;i<lim;i++)
#define repd(i,start,lim) for(lld i=start;i>=lim;i--)
#define scan(x) scanf("%lld",&x)
#define print(x) printf("%lld ",x)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define br printf("\n")
#define sz(a) lld((a).size())
#define YES printf("YES\n")
#define NO printf("NO\n")
#define all(c) (c).begin(),(c).end()
using namespace std;
#define INF 1011111111
#define LLINF 1000111000111000111LL
#define EPS (double)1e-10
#define MOD 1000000007
#define PI 3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
lld powm(lld base,lld exp,lld mod=MOD) {lld ans=1;while(exp){if(exp&1)
ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
typedef vector<lld> vlld;
typedef pair<lld,lld> plld;
typedef map<lld,lld> mlld;
typedef set<lld> slld;
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl '\n'
#define N 1000005
#define fre freopen("1.in","r",stdin); freopen("1.out","w",stdout);
int main()
{
sync;
lld n;
string s;
lld up =0 , low = 0,special = 0, digit= 0;
cin>>n;
cin>>s;
rep(i,0,n) {
if(s[i]>='A' and s[i]<='Z') up = 1;
else if(s[i]>='a' and s[i]<='z') low = 1;
else if(s[i]>='0' and s[i]<='9') digit = 1;
else special = 1;
}
lld ans = (!up) + (!low) + (!digit) + (!special);
if(ans + n < 6) {
cout<<(ans + (6-n-ans));
}
else {
cout<<ans;
}
return 0;
}
Output:

Learning outcomes: implementation of strings


Experiment 3.1
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: Implement the problems based on Dynamic Programming.


Objective: https://www.hackerrank.com/challenges/sherlock-and-
cost/problem
Code:
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<stack>
#include<cmath>
using namespace std;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define pb push_back
#define ss second
#define ff first
#define vi vector<int>
#define vl vector<ll>
#define s(n) scanf("%d",&n)
#define ll long long
#define mp make_pair
#define PII pair <int ,int >
#define PLL pair<ll,ll>
#define inf 1000*1000*1000+5
#define v(a,size,value) vv a(size,value)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define tri pair < int , PII >
#define TRI(a,b,c) mp(a,mp(b,c))
#define xx ff
#define yy ss.ff
#define zz ss.ss
#define vii vector < PII >
#define vll vector< PLL >
#define viii vector < tri >
#define vs vector<string>
#define foreach(v,c) for( typeof((c).begin()) v = (c).begin(); v != (c).end();
++v)
#define fill(a,v) memset(a,v,sizeof a)
#define printall(a) rep(i,sz(a))cout<<a[i]<<endl;
ll pow(ll num,ll power){if(power==1)return num; ll t =
pow(num,power/2);return power%2?t*t*num:t*t;}
//int check(int);
//int binarysearch(int len)//finds last 1 in 11111000
//{int lo = 1,hi = len,mid,flag;while(lo<=hi){mid = hi+lo>>1;(flag =
check(mid))?lo = mid+1:hi = mid-1;}return mid-1+flag;}
const int N = 1000*100+5;
//int check(int a){}
int dp[N][3];int a[N];
int main()
{
ios::sync_with_stdio(false);
int i,j,n,t;
cin>>t;
while(t--)
{
cin>>n;
rep(i,N)a[i]=0;
rep(i,n)cin>>a[i];
if(n==1){cout<<0<<endl;continue;}
rep(i,N)dp[i][0] = dp[i][1] = 0;
dp[1][0] = abs(a[0] - 1);
dp[1][1] = max(abs(a[1] - 1),abs(a[1] - a[0]));
f(i,2,n)
{
dp[i][0] = max(dp[i-1][0] , dp[i-1][1] +
abs(a[i-1] - 1));
dp[i][1] = max(dp[i-1][1] + abs(a[i] -a[i-
1]),dp[i-1][0] + abs(a[i] - 1));
}
cout<<max(dp[n-1][0],dp[n-1][1])<<endl;
// f(i,1,n)cout<<dp[i][0]<<" "<<dp[i][1]<<endl;
}
cin>>i;
}
Output:

Learning outcomes: Implement the problems based on Dynamic


Programming.
Experiment 3.2
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: Implement the problemsbased on Backtracking


Objective: https://www.hackerrank.com/challenges/subset-sum/problem
Code:
-- Enter your code here. Read input from STDIN. Print output to STDOUT
import qualified Data.Set as Set
import qualified Data.Map as Map
import Data.List(sort)
import Data.Maybe(fromJust)
prefsum :: (Ord a, Num a) => [a] -> [a]
prefsum x = scanl1 (+) (reverse $ sort x)
create :: (Ord a, Integral a) => [a] -> (Map.Map a a, Set.Set a)
create x = (Map.fromList (zip l [1..]) , Set.fromList l)
where l = prefsum x
solve :: (Ord a, Integral a) => (Map.Map a a, Set.Set a) -> a -> a
solve (mp, st) x = case Set.lookupGE x st of
Nothing -> -1
Just a -> fromJust (Map.lookup a mp)
main :: IO()
main = do
g <- getContents
let lst = map read (words (lines g !!1))
(mp, st) = create lst
qu = map read (drop 3 (lines g))
ans = map (solve (mp, st)) qu
mapM_ print ans
return ()
Output:

Learning outcomes: Implement the problems based on Backtracking


Experiment 3.3
Name: harsh patil UID: 21BCS1560
Branch: CSE Semester: 5
Subject: Advance programming lab-1 Section: A
Date of Performance:17-06-25 Subject Code: 21CSP-314

Aim: Implement the problems based on Branch and Bound.


Objective: https://www.hackerrank.com/challenges/assignment/problem
Code:
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define get_lines() {int c = 0; while ((c = getchar()) != 10 && c != EOF);}
typedef long double LD;
int M, N;
const LD eps = 1e-9, pi = M_PI;
const int INF = 1 << 28;
LD p1[252], dp[252][252];
LD count_prob_less(int m) {
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for (int l = 1; l <= M; l++) {
for (int n = 1; n <= M; n++) {
dp[l][n] = dp[l][n - 1] + dp[l - 1][n - 1];
if (n - 1 - m >= 0)
dp[l][n] -= dp[l - 1][n - 1 - m];
}
}
LD up = 0, down = 0;
LD C = N, CM = 1;
for (int g = 1; g <= M; g++) {
up += dp[g][M] * C;
down += C * CM;
CM /= g;
CM *= (M - g);
C /= g + 1;
C *= (N - g);
}
return up / down;
}
void get_answer() {
LD res = 0;
for (int i = 1; i <= M; i++)
p1[i] = count_prob_less(i);
for (int i = 1; i <= M; i++)
res += i * (p1[i] - p1[i - 1]);
cout << fixed << setprecision(5) << res << endl;
}
int main() {
int t;
cin >> t;
get_lines();
for (int i = 0; i < t; i++) {
cin >> M >> N;
get_answer();
}
return 0;
}
Output:

Learning outcomes: Implement the problems based on Branch and Bound.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy