0% found this document useful (0 votes)
5 views3 pages

Five Questions Code

The document contains multiple programming questions with solutions in Python and C++. Question 1 involves counting non-overlapping intervals, Question 2 focuses on finding the longest common subsequence of two strings, Question 3 calculates population demographics based on literacy rates, Question 4 computes a formula involving multiple variables, and Question 5 provides a C++ program for adding fractions. Each question includes input handling and relevant calculations.

Uploaded by

jayendrarathod07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Five Questions Code

The document contains multiple programming questions with solutions in Python and C++. Question 1 involves counting non-overlapping intervals, Question 2 focuses on finding the longest common subsequence of two strings, Question 3 calculates population demographics based on literacy rates, Question 4 computes a formula involving multiple variables, and Question 5 provides a C++ program for adding fractions. Each question includes input handling and relevant calculations.

Uploaded by

jayendrarathod07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 1

# You are given


n = int(input())
if n == 0:
print(0)
else:
pairs = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: x[1])
count = 1
curr_end = pairs[0][1]
for i in range(1, n):
if pairs[i][0] > curr_end:
count += 1
curr_end = pairs[i][1]
print(count)

Question 2
# Dr. Sophia is
import sys
from functools import lru_cache

sys.setrecursionlimit(2005)

s1 = input()
s2 = input()
m, n = len(s1), len(s2)

dp = [[0] * (n + 1) for _ in range(m + 1)]


for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

@lru_cache(maxsize=None)
def find_lcs_strings(i, j):
if i == 0 or j == 0:
return {""}
if s1[i - 1] == s2[j - 1]:
return {lcs + s1[i - 1] for lcs in find_lcs_strings(i - 1, j - 1)}
res = set()
if dp[i][j] == dp[i][j - 1]:
res.update(find_lcs_strings(i, j - 1))
if dp[i][j] == dp[i - 1][j]:
res.update(find_lcs_strings(i - 1, j))
return res

all_lcs = sorted(find_lcs_strings(m, n))


print("All possible unique LCS combinations:")
for lcs in all_lcs:
print(lcs)
print(f"Length of LCS: {dp[m][n]}")

Question 3
# Krishna is a
tot = int(input())
lit = int(input())
men = int(input())
menlitper = int(input())

totlit = (tot * lit) // 100


totilit = tot - totlit

totmen = (tot * men) // 100


totwo = tot - totmen

menlit = (tot * menlitper) // 100


wolit = totlit - menlit
menilit = totmen - menlit
woilit = totwo - wolit

print(f"Men population={totmen}")
print(f"Women population={totwo}")
print(f"Literates={totlit}")
print(f"Illiterates={totilit}")
print(f"Men literates={menlit}")
print(f"Women literates={wolit}")
print(f"Men illiterates={menilit}")
print(f"Women illiterates={woilit}")

Question 4
# James is a
m = int(input())
g = int(input())
a = float(input())
p = int(input())
t = int(input())

print(f"{(m / a) * g + p + t:.2f}")
Question 5
// Write a program

#include <iostream>

#include <algorithm>

using namespace std;

int main() {

int n1, d1, n2, d2;

cin >> n1 >> d1 >> n2 >> d2;

int num = n1 * d2 + n2 * d1, den = d1 * d2;

int g = __gcd(num, den);

cout << num / g << '/' << den / g;

You might also like