Four Digit OTP
Four Digit OTP
1
Problem Statement
You are given a number in the form of 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.
2
SAMPLE INPUT 1
3
3 4 5 6 7
String 1 OTP:????
5
SAMPLE INPUT 2
6
0 1 2 3
String 2 OTP:????
Index [3] odd , pick and square = 3^2 = 9 = Next Two digit of OTP
Index [2], even = discard = don’t Pick
Index [1] odd , pick and square = 1^2 = 1 = First digit of OTP
8
SAMPLE INPUT 3
9
0 1 2 3 4 5
OTP:????
String 3
Check for Odd index
Index [5] odd , pick and square = 5^2 = 25 = Last two digit of OTP
OTP
11
Algorithm
Step 1: Initialization:
◦ Taking input from user ( string)
Step 2: Processing
◦ Loop run from 0 to length of string -1
◦ Check for odd index
◦ If odd then proceed for square
◦ and concatenation
12
Code
input_string = input()
otp_string = ""
for i in range(len(input_string)) :
if i % 2
num_square = int(input_string[i]) ** 2
otp_string += str(num_square)
if len(otp_string) >= 4 :
print(otp_string[ : 4])
break
else:
rint("-1")
13