PBT CODING ONLY
WAIT FOR MCQ DO NOT SUBMIT CODING SECTION
PROBLEM 1:
Problem Statement
Kumar is back and this time he wants to make a list of all the groceries he needs to buy .He writes
the list as a text file in his computer and saves it and goes to sleep.However there is a problem here.
Due to random electrical fluctuations the computer’s memory gets shuffled and all the words in
Kumar’s
grocery list get reversed . When he wakes up the next day he notices that some items in
the changed list were also present in the unchanged list For example let’s assume kumar stores the
list as: abcd efgh hgfe Overnight the list becomes: dcba hgfe efgh In this case the grocery items
hgfe and efgh were both present in the second list Given a list of N groceries you must report to
kumar how many groceries were preserved after the list undergoes modification due to the
electrical fluctuation
Input Format
The first line contains a single integer N denoting the number of items in his grocery list The next N
lines contain a single string per line denoting an item in the list
Constraints
1<=N<=100000
Length of each string is atmost 20
Output Format
The output must consist of a single integer denoting how many items in the original list were
preserved
Python 3:
Problem 2:
Problem Statement
Peter wants to play a game with you.He gives you N strings as S1 S2 S3....SN and he will ask you Q
questions In each question he will give you a number M and X. He wants to know if the longest
common prefix of the first M strings has length at least X If it is greater than or equal to X report to
him “Yes” or report to him “No"
Input Format
The first line contains a single integer N denoting the number of strings he will give you initially The
next N line contains a single string per line The next line contains a single integer Q denoting the
number of questions he will ask you The next Q line contains two integers M X denoting the number
of strings from the first string he wants you to consider and the length he wants it to have
respectively
Constraints
1<=M<=N<=10000 1<=X<=20 Each string has length at most 20
Output Format
Your output must consist of Q lines each line containing one of the following : “Yes” or “No”
PYTHON3:
from itertools import combinations
def demo():
n = int(input())
arr = []
for i in range(n):
arr.append(input())
q_arr= []
q = int(input())
for i in range(q):
q_arr.append(list(map(int,input().split())))
# print(q_arr)
ques = {}
for i in range(len(q_arr)):
ques[i] = "No"
# print(ques)
for kru,i in enumerate(q_arr):
M = i[0]
X = i[1]
comb = combinations(arr,M)
for j in comb:
check = []
for k in j:
check.append(k[:X])
count = check.count(check[0])
if count == M:
ques[kru] = "Yes"
break
kk = ques.values()
for i in kk:
print(i)
print()
demo()
Post a Comment