Python_Answer For Cognizant Questions
Python_Answer For Cognizant Questions
Code:
def count_magical_rows(shelves):
magical_rows = 0
magical_rows += 1
return magical_rows
# Input
n = int(input())
m = int(input())
# Output
print(count_magical_rows(shelves))
Sample Input:
Copy
3
3
1 2 3
4 5 6
7 8 9
Sample Output:
Copy
2
Code:
def decrypt(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:
count = 0
left += 1
else:
right -= 1
return count
# Input
n = int(input())
x = int(input())
# Output
print(count_subarrays(arr, x))
Sample Input:
Copy
5
10
1 2 3 4 5
Sample Output:
Copy
8
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
switches += 1
is_green = False
is_green = True
return switches
# Input
N = int(input())
# 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:
total = sum(arr[i:i+D])
if total >= M:
return max_water
# Input
D = int(input())
M = int(input())
N = int(input())
# Output
print(max_water(arr, D, M))
Sample Input:
Copy
2
5
6
4 3 1 0 2 1
Sample Output:
Copy
7
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
count = count % M
return 0
left += 1
right -= 1
return count
# Input
M = int(input())
s = input()
# Output
1000000007
a??b
Sample Output:
26