AP 2 - Gandu

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

DEPARTMENT OF

COMPUTER SCIENCE &


ENGINEERING
Experiment 2.2
Student Name: G Gandhi UID:21BCS11070
Branch: BE-CSE Section/Group: 21BCS_IOT-642 B
Semester: 5th Date of Performance:
Subject Name: Advance Programming Lab-1 Subject Code: 21CSP-314

Aim: Graphs: To implement the concept of Tree Data Structure.

Objective (a): Given a pointer to the root of a binary tree, print the top view of the
binary tree.
The tree as seen from the top the nodes, is called the top view of the tree.
Code (a):

void topView(Node * root)


{
queue<pair<int,Node*>> q;
q.push(make_pair(0,root));
map<int,Node*> ans;
for(auto i=q.front();
!q.empty();
q.pop(),i=q.front())
{
if(!i.second) continue;
ans.insert(i);
q.push(make_pair(i.first+1,i.second->right));
q.push(make_pair(i.first-1,i.second->left));
}
for(auto i:ans) cout<<i.second->data<<" ";
}

Name – Gandhi Uid – 21BCS11070


DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Output (a):

Name – Gandhi Uid – 21BCS11070


DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Objective (b): In this challenge, you are required to implement inorder traversal of a
tree.
Complete the inorder function in your editor below, which has 1 parameter: a
pointer to the root of a binary tree. It must print the values in the tree's inorder
traversal as a single line of space-separated values.

Code (b):
void inOrder(Node *root)
{
if(root->left)
{
inOrder(root->left);
}
cout << root->data << '
'; if(root->right)
{ inOrder(root->right);
}
}
Output (b):

Name – Jaya Shankar Uid – 21BCS8722


DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING

Learning Outcomes:
• Learnt to build the logic to find out the solution of problem and achieve all
test cases.
• Learnt to interpret the problem and find out better approach to solve
particular problem.
• Learnt the concept and implementation of Tree.
• To gain critical understanding of problem solving on Hackerrank platform.

Time Complexity:
 Worst-case performance: O(n)
 Best-case performance: O(1)
 Average performance: O(log n)
 Worst-case space complexity: O(1)

Name – Jaya Shankar Uid – 21BCS8722


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 2.3

Student Name: G Gandhi UID: 21BCS11070


Branch: CSE Section/Group: 642-B
Semester: 5th Date of Performance: 22-09-2023
Subject Name: AP Lab-1 Subject Code: 21CSP-314

1. Aim: String Algorithms: Demonstrate the concept of string.

2. Objective

A. A pangram is a string that contains every letter of the alphabet. Given a


sentence determine whether it is a pangram in the English alphabet. Ignore
case. Return either pangram or not pangram as appropriate.

B. There is a sequence of words in CamelCase as a string of letters, , having


the following properties:
 It is a concatenation of one or more words consisting of English letters.
 All letters in the first word are lowercase.
 For each of the subsequent words, the first letter is uppercase and rest
of the letters are lowercase.
3. Code:
Input:
A.
#include <bits/stdc++.h>
using namespace std;

string pangrams(string s) {
map<char,int> m;
transform(s.begin(), s.end(), s.begin(), ::tolower);
int n = s.length();
for(int i = 0; i < n; i++){
m.insert({char(s[i]),i});
}
for(char c = 'a'; c <= 'z' ; c++){
if(m.find((c)) == m.end()){
return "not pangram";
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return "pangram";
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string s;
getline(cin, s);

string result = pangrams(s);


fout << result << "\n";
fout.close();

return 0;
}

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

int camelcase(string s) {
// numberofcapitalletters+1
int n = s.length();
int count = 0;
for(int i = 0; i < n; i++){
if(s[i] >= 'A' && s[i] <= 'Z'){
count++;
}
}
return count+1;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string s;
getline(cin, s);

int result = camelcase(s);

fout << result << "\n";

fout.close();

return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Screenshot of Outputs:
A.

B.

Learning And Outcomes:

 In this practical, We learned the concept of stringin Hacker Rank.

You might also like