0% found this document useful (0 votes)
41 views

Python_Answer For Cognizant Questions

Uploaded by

Yuvaraj V
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Python_Answer For Cognizant Questions

Uploaded by

Yuvaraj V
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Magical Library Bookshelves

Question: In a magical Library, each bookshelf is represented by a two-dimensional array A


where each row of the 2D array A[i] represents the series value of a book. A row is
considered magical if the sum of the odd values of the series of a book is even. Your task is
to find and return an integer value representing the number of magical rows.

Code:

def count_magical_rows(shelves):

magical_rows = 0

for shelf in shelves:

if sum(book for book in shelf if book % 2 != 0) % 2 == 0:

magical_rows += 1

return magical_rows

# Input

n = int(input())

m = int(input())

shelves = [list(map(int, input().split())) for _ in range(n)]

# Output

print(count_magical_rows(shelves))

Sample Input:

Copy
3
3
1 2 3
4 5 6
7 8 9

Sample Output:

Copy
2

2. Ancient Journal Decryption


Question: An ancient journal was found containing an encrypted message. The encryption
used in the journal shifts each character one position forward in the alphabet. Your task is to
return the decrypted string by shifting each character one position back in the alphabet.

Code:

def decrypt(s):

return ''.join(chr((ord(c) - 97 - 1) % 26 + 97) for c in s)

# Input

encrypted = input()

# Output

print(decrypt(encrypted))

Sample Input:

Copy
bcd

Sample Output:

Copy
abc

3. Conditional Subarrays

Question: You have a sorted box of numbers stored in an integer array A, and you want to
find all the "mini boxes" (subarrays) that meet a special rule. This rule says that the sum of
the smallest and biggest number in each mini box must be less than or equal to a given
number x. Your task is to find and return an integer value representing the number of mini
boxes that can be made which follow the rule.

Code:

def count_subarrays(arr, x):

left, right = 0, len(arr) - 1

count = 0

while left <= right:


if arr[left] + arr[right] <= x:

count += right - left + 1

left += 1

else:

right -= 1

return count

# Input

n = int(input())

x = int(input())

arr = list(map(int, input().split()))

# Output

print(count_subarrays(arr, x))

Sample Input:

Copy
5
10
1 2 3 4 5

Sample Output:

Copy
8

4. Red Pen Green Pen

Question: You are a teacher writing N numbers on the classroom whiteboard. You use a
green pen for odd numbers and a red pen for even numbers. Your task is to find and return an
integer value representing the number of times you need to switch from the green pen to the
red pen while writing these numbers.

Code:

def count_switches(numbers):

switches = 0
is_green = numbers[0] % 2 != 0

for num in numbers[1:]:

if num % 2 == 0 and is_green:

switches += 1

is_green = False

elif num % 2 != 0 and not is_green:

is_green = True

return switches

# Input

N = int(input())

numbers = list(map(int, input().split()))

# Output

print(count_switches(numbers))

Sample Input:

Copy
6
70 23 13 26 72 19

Sample Output:

Copy
1

5. Sectional Garden

Question: A gardener is tasked with watering different sections of a large garden over several
days. There's a minimum amount of water M liters that each section must receive within a
specified number of consecutive D days. Your task is to find and return an integer value
representing the maximum total amount of water that can be provided to any section over any
streak of D consecutive days, ensuring that the total water is at least M liters. If no such
streak meets the requirement, return 0.

Code:

def max_water(arr, D, M):


max_water = 0

for i in range(len(arr) - D + 1):

total = sum(arr[i:i+D])

if total >= M:

max_water = max(max_water, total)

return max_water

# Input

D = int(input())

M = int(input())

N = int(input())

arr = list(map(int, input().split()))

# Output

print(max_water(arr, D, M))

Sample Input:

Copy
2
5
6
4 3 1 0 2 1

Sample Output:

Copy
7

6. Palindromic String Count

Question: Jack broke a toy which consisted of a string S. The toy was broken in such a way
that some letters were still present, and some were lost. Jack decided to replace the missing
letters with '?'. He can generate palindrome strings by replacing '?' with any letter from 'a' to
'z'. Jack has a favorite number M. Your task is to find and return an integer value representing
the total number of palindromic strings modulo M that he can generate by replacing '?' with
any letter from 'a' to 'z'.

Code:
def count_palindromes(s, M):

count = 1

left, right = 0, len(s) - 1

while left <= right:

if s[left] == '?' and s[right] == '?':

count = (count * 26) % M

elif s[left] == '?' or s[right] == '?':

count = count % M

elif s[left] != s[right]:

return 0

left += 1

right -= 1

return count

# Input

M = int(input())

s = input()

# Output

print(count_palindromes(s, M))Sample Input:

1000000007
a??b

Sample Output:

26

You might also like