Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

Hello world | PBT | DAILY TEST | 14 JUNE CODING ONLY

 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:

def rev(string):
    string = string[::-1]
    return string

def demo():
    n = int(input())
    arr = []
    for i in range(n):
        arr.append(input())
    new_arr = []
    for i in arr:
        new_arr.append(rev(i))

    count = 0
    for i in arr:
        if i in new_arr:
            count+=1

    print(count)
demo()


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()


Problem 3:

Problem Statement
Jack is bored in english class and decides to play a game with you. He will ask you Q questions and
in each question he will give you a string S Such that it has length at most 500. You have to tell him
if the string can be partitioned into two substrings s1 and s2 such that s1 is present in the dictionary
s2 is present in the dictionary s1+s2 is equal to S Note that s1 could be equal to s2. They need not
be distinct strings. The list of words in the dictionary will be given to you initially and using this
answer Jack's questions.
Input Format
The first line contains a single integer N, denoting the number of strings in the dictionary The next N
lines contains a single string each denoting the words in the dictionary The next line contains a
single integer Q : the number of questions you will be asked The next Q line contains a single string
each which will be given to you
Constraints
1<=n<=100
Output Format
Your output must consist of Q lines , each of which you must answer with “Yes” if the string can be
partitioned into two strings s1 and s2 as given in the problem statement Or “No” if the string cannot
be partitioned

PYTHON 3:

def demo():
    n = int(input())
    arr = []
    for i in range(n):
        arr.append(input())
    q = int(input())
    q_arr = []
    for i in range(q):
        q_arr.append(input())

    #Corner cases: if S1 and S2, both are same strings
    ques = {}
    for i in q_arr:
        ques[i] = "No"
    for i in q_arr:
        for j in arr:
            if (j+j) == (i):
                ques[i] = "Yes"


    for i in q_arr:
        count = 0
        length_i = len(i)
        for j in arr:
            length_j = len(j)
            # print(j,i,length_i,length_j)
            if str(j) in str(i):
                length_i = length_i - length_j
                count += 1    
                # print(i,j,count)
        if length_i == 0 and count == 2:
            ques[i] = "Yes"

    kk = ques.values()
    for i in kk:
        print(i)
    print()
demo()

Post a Comment

Previous Post Next Post