In this post, we will see one practice problem which can be useful for Infosys Infytq coding exam preparation.
Problem Statement: You will be given a number in the form of the string, extract out digits at odd index then square and merge them. The first 4 digits will be the required OTP which shows as output. If 4 digit OTP is not generated then give output -1.
Sample Test Cases:
Input 1: '34567' Output 1: 1636 Input 2: '0123' Output 2: -1 Input 3: '012345' Output 3: 1925 Input 4: '54892356' Output 4: 1681
Approach : You have to pick an odd index integer from the string then take a square of it and concatenate to each other. The final output should be the first 4 digits of it. If 4 digit OTP is not possible to generate then give output as -1.
Now let’s see the different ways to write the code for this:
Code 1: Using simple for loop with range() built-in function.
# Code : 1
if __name__ == "__main__" :
# Taking input from user
input_string = input()
# Empty string
otp_string = ""
# loop run from 0 to length of string - 1
for i in range(len(input_string)) :
# check if i is odd or not
# if odd then proceed with square
# and concatenation
if i % 2 :
# Converting string to integer
# and then doing the square of it
num_square = int(input_string[i]) ** 2
# Concatenating the num_square values
# with otp_string values
otp_string += str(num_square)
# if length of otp_string will become
# greater than or equal to 4 then
# print 4 digit answer and
# break out of the loop
if len(otp_string) >= 4 :
print(otp_string[ : 4])
break
# if for loop runs successfully without break
# then that means otp_string with length 4
# is not possible so then prints -1
# as an output
else:
print("-1")
Code 2: Using for loop with range() built-in function step concept.
# Code : 2
# Main Code
if __name__ == "__main__" :
# Taking input from user
input_string = input()
# Empty string
otp_string = ""
# Iterating from 1 to length of input string - 1
# with step of 2
for i in range(1, len(input_string), 2) :
# Converting string to integer
# and then doing the square of it
num_square = int(input_string[i]) ** 2
# Concatenating the num_square values
# with otp_string values
otp_string += str(num_square)
# if length of otp_string will become
# greater than or equal to 4 then
# print 4 digit answer and
# break out of the loop
if len(otp_string) >= 4 :
print(otp_string[ : 4])
break
# if for loop runs successfully without break
# then that means otp_string with length 4
# is not possible so then prints -1
# as an output
else:
print("-1")
Code 3: Using string slicing concept first then applying for loop on it.
# Code: 3
# Main Code
if __name__ == "__main__" :
# Taking input from user
input_string = input()
# Take out the required characters in one
# string using string slicing concept
required_string = input_string[1 : : 2]
# Empty string
otp_string = ""
for character in required_string :
otp_string += str(int(character) ** 2)
# if length of otp_string will become
# greater than or equal to 4 then
# print 4 digit answer and
# break out of the loop
if len(otp_string) >= 4 :
print(otp_string[ : 4])
break
# if for loop runs successfully without break
# then that means otp_string with length 4
# is not possible so then prints -1
# as an output
else:
print("-1")
Code 4: Using string slicing concept first then list comprehension and join() built-in function.
# Code: 4
# Main Code
if __name__ == "__main__" :
# Taking input from user
input_string = input()
# Take out the required characters in one
# string using string slicing concept
required_string = input_string[1 : : 2]
# create a list of square of each integral
# values using list comprehension
otp_string_list = [str(int(char) ** 2) for char in required_string]
# create a string from the list of characters
# using join() method
otp_string = "".join(otp_string_list)
# if length of otp_string will become
# greater than or equal to 4 then
# print 4 digit answer
if len(otp_string) >= 4 :
print(otp_string[ : 4])
# otp_string with 4 digit not possible
# then print -1 as an output
else:
print(-1)
Thanks for reading this blog.
Check out this post also: TCS Xplore CPA Python Coding Question -1 with Solution
If you have any doubts in understanding the above Code or Question then please comment below, we will try to clear your doubts.