21BCS4853 Saurav Sirsat

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Assignment-1
Student Name: Saurav Sirsat UID: 21BCS4853
Branch: CSE Section/Group: FL-602-B
Semester: 6 th Date of Performance: 24/05/24
Subject Name: IT Skills

Q1 MoEngage noticed that some users are not aware of the practice page on
CodeChef, and some others are not aware of the rated contests on CodeChef.
So, MoEngage wants to send an email to the users depending on which of the
following groups they fall into:
If the user has never submitted on the practice page then send an email with
the text:
https://www.codechef.com/practice
If the user has submitted on the practice page, but never participated in a
contest then send an email with the text:
https://www.codechef.com/contests
If the user has submitted on the practice page as well as participated in a
contest then send an email with the text: https://discuss.codechef.com so
that they may discuss with other members of the community. Help
MoEngage by writing a program that takes as input two integers 𝐴A
and 𝐵B where:
𝐴=1A=1 if the user has submitted on the practice page and 00 otherwise.
𝐵=1B=1 if the user has participated in a contest and 00 otherwise. Output
the appropriate link to be displayed in the email.

Code and Output

def main():
# Read the input values
A, B = map(int, input().split())

# Determine the correct URL based on the values of A and B


if A == 0:
print("https://www.codechef.com/practice")
elif A == 1 and B == 0:
print("https://www.codechef.com/contests")
elif A == 1 and B == 1:
print("https://discuss.codechef.com")
if __name__ == "__main__":
main()

Q2. There is a kingdom in land of fire and ice known as westros. The
kingdom is ruled by mad king Amen Targareyan. Further more Westros is
divided into two territories. One territory known as ice because it is always
snowing and other Fire beacuse of volcano near by area. There is rebellion to
overthrow the mad king to bring peace to Westros. There is a secret attack on
red fort by rebellion force lead by Luke Skywalker. There is snitch in
rebellion force. He trades the secret attack plan to mad king. However mad
king has superpower to sense disturbance in the force. He sends out his best
knights to scout the rebellion force. He got to know that rebel force outmatch
city guards one to hundred. Moreover he came to know that Luke is last jedi
who can detroy him.In order to win this battle against rebels, the mad king
decided to use N common folks as soldiers. He select a bunch of people from a
territory and train them .Now king can only select odd number of people
from land of fire and from land of ice in one go. He cannot select bunch of
people consecutively from either territory.In how may ways the mad king
could assemble his army to battle against rebel forces ? Note: The number of
people in each territory is infinite

Code and Output

#include <iostream> #include


<vector>
using namespace std;

const int MAX_N = 1000;


void computeWays(vector<int>& dp, int maxN, int M) {
vector<int> dp_fire(maxN + 1, 0);
vector<int> dp_ice(maxN + 1, 0);

dp[0] = 1; // Base case: one way to assemble an army of size 0 (do nothing)

for (int n = 1; n <= maxN; ++n) {


dp_fire[n] = 0;
dp_ice[n] = 0;
for (int k = 1; k <= n; k += 2) { // k is odd
if (n - k >= 0) {
dp_fire[n] = (dp_fire[n] + dp[n - k]) % M;
dp_ice[n] = (dp_ice[n] + dp[n - k]) % M;
}
}
dp[n] = (dp_fire[n] + dp_ice[n]) % M;
}
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int
T;
cin >> T;

vector<pair<int, int>> queries(T);


int maxN = 0;

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


cin >> queries[i].first >> queries[i].second;
if (queries[i].first > maxN) { maxN =
queries[i].first;
}
}

vector<int> dp(maxN + 1, 0);

for (auto& query : queries) {


int N = query.first; int M =
query.second; if (dp[N] ==
0 && N != 0) {
computeWays(dp, N, M);
}
cout << dp[N] % M << "\n";
}

return 0;
}

Q3. Given an input string (s) and a pattern (p), implement wildcard pattern
matching with support for '?' and '*' where:
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence). The
matching should cover the entire input string (not partial).

Code and Output

class Solution {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();

// DP table with default false values


boolean[][] dp = new boolean[m + 1][n + 1];

// Empty pattern matches empty string


dp[0][0] = true;

// Initialize dp for patterns with *


for (int j = 1; j <= n; ++j) { if
(p.charAt(j - 1) == '*') {
dp[0][j] = dp[0][j - 1];
}
}

// Fill the DP table for


(int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (p.charAt(j - 1) == '*') {
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
} else if (p.charAt(j - 1) == '?' || s.charAt(i - 1) == p.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
}
}
}

return dp[m][n];
}
}

Q4. A machine recycles scrap bottles, the size of the bottle is categorised into
types. For 𝑛n number of bottles you have to calculate the number of bottles in
each category type. Size to category table:-
BOTTLE SIZE: | 1,2 - TYPE 1 | 3,4 - TYPE 2 | 5,6 - TYPE 3

Code and Output

#include <iostream> #include


<vector>
using namespace std;
int main() {
int T;
cin >> T;

while (T--) {
int N;
cin >> N;

vector<int> bottles(N);
for (int i = 0; i < N; i++) {
cin >> bottles[i];
}

// Counters for each type


int type1 = 0, type2 = 0, type3 = 0;

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


if (bottles[i] == 1 || bottles[i] == 2) {
type1++;
} else if (bottles[i] == 3 || bottles[i] == 4) {
type2++;
} else if (bottles[i] == 5 || bottles[i] == 6) {
type3++;
}
}

// Print the results for the current test case


cout << "TYPE 1 - " << type1 << "\n"; cout
<< "TYPE 2 - " << type2 << "\n"; cout <<
"TYPE 3 - " << type3 << "\n";
}

return 0;
}
Q5. You are being given a number N . (1<=N<=1000). You have to print the
sum of digits of that particular number.

Code and Output

#include <iostream> #include


<string>
using namespace std;

int sumOfDigits(const string& number) {


int sum = 0; for (char
digit : number) { sum
+= digit - '0';
return sum;
}

int main() {
int T; cin
>> T;
cin.ignore();

while (T--) {
string number;
getline(cin, number);
cout << sumOfDigits(number) << endl;
}

return 0;
}

Q6. Chef knows about two languages spoken in Chefland, but he is not
proficient in any of them. The first language contains lowercase English
letters between 'a' and 'm' inclusive and the second language contains only
uppercase English letters between 'N' and 'Z' inclusive.
Due to Chef's limited vocabulary, he sometimes mixes the languages when
forming a sentence — each word of Chef's sentence contains only characters
from one of the languages, but different words may come from different
languages.
You are given a sentence as a sequence of 𝐾K words 𝑆1,𝑆2,…,𝑆𝐾S1,S2,…,SK.
Determine whether it could be a sentence formed by Chef, i.e. if it contains
only the characters from the two given languages and each word contains
only characters from a single language.
Code and Output

#include <iostream>
#include <vector> #include
<string>
using namespace std;

// Function to check if a word belongs to the first language


bool isFirstLanguage(const string& word) {
for (char ch : word) {
if (ch < 'a' || ch > 'm') {
return false;
}
}
return true;
}

// Function to check if a word belongs to the second language


bool isSecondLanguage(const string& word) {
for (char ch : word) {
if (ch < 'N' || ch > 'Z') {
return false;
}
}
return true;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
cin.ignore(); // Ignore the newline character after the number of test cases

while (T--) {
int K; cin
>> K;
bool validSentence = true;

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


string word;
cin >> word;

if (!isFirstLanguage(word) && !isSecondLanguage(word)) {


validSentence = false;
}
}
if (validSentence) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}

return 0;
}
Q7. Given a string s representing a valid expression, implement a basic
calculator to evaluate it, and return the result of the evaluation.
Note: You are not allowed to use any built-in function which evaluates strings
as mathematical expressions, such as eval().

Code and Output

class Solution { public:


int calculate(string s) {
stack<int> nums;
stack<int> ops;
int result = 0; int
number = 0;
int sign = 1;

for (int i = 0; i < s.length(); ++i) {


char c = s[i];

if (isdigit(c)) {
number = number * 10 + (c - '0');
} else if (c == '+' || c == '-') {
result += sign * number; number
= 0; sign = (c == '+') ? 1 : -1;
} else if (c == '(') {
nums.push(result);
ops.push(sign);
result = 0;
sign = 1; } else if (c == ')')
{ result += sign *
number;
number = 0;

result *= ops.top(); ops.pop();


result += nums.top(); nums.pop();
}
}

result += sign * number;

return result;
}
};

Q8. A digit string is good if the digits (0-indexed) at even indices are even and
the digits at odd indices are prime (2, 3, 5, or 7).
For example, "2582" is good because the digits (2 and 8) at even positions are
even and the digits (5 and 2) at odd positions are prime.
However, "3245" is not good because 3 is at an even index but is not even.
Given an integer n, return the total number of good digit strings of length n.
Since the answer may be large, return it modulo 109 + 7.
A digit string is a string consisting of digits 0 through 9 that may contain
leading zeros.

Code and Output

class Solution { public:


int countGoodNumbers(long long n) {
const int MOD = 1e9 + 7; long long
evenChoices = 5; // 0, 2, 4, 6, 8 long long
oddChoices = 4; // 2, 3, 5, 7

// Calculate the total number of valid combinations for even indices long
long evenTotal = power(evenChoices, (n + 1) / 2, MOD);

// Calculate the total number of valid combinations for odd indices


long long oddTotal = power(oddChoices, n / 2, MOD);

// Multiply the two totals to get the final result


long long result = (evenTotal * oddTotal) % MOD;

return static_cast<int>(result);
}

private:
// Function to calculate (base^exponent) % MOD efficiently using binary
exponentiation
long long power(long long base, long long exponent, int MOD) {
long long result = 1; while (exponent > 0) { if
(exponent % 2 == 1) {
result = (result * base) % MOD;
}
base = (base * base) % MOD;
exponent /= 2;
}
return result;
}
};

You might also like