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

Four Digit OTP

The document describes generating a 4-digit one-time password (OTP) from a string of numbers. It extracts the digits at odd indices from the string, squares them, and concatenates them to form the OTP. If fewer than 4 digits are generated, the output is -1. Sample inputs and outputs are provided to illustrate extracting 1636 from "3456" and 1925 from "01245", or outputting -1 for the string "1234". Pseudocode explains the algorithm by taking the input, looping through indices, squaring odd digits, concatenating them, and printing the first 4 digits or -1.

Uploaded by

Gaurav Yadav 093
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Four Digit OTP

The document describes generating a 4-digit one-time password (OTP) from a string of numbers. It extracts the digits at odd indices from the string, squares them, and concatenates them to form the OTP. If fewer than 4 digits are generated, the output is -1. Sample inputs and outputs are provided to illustrate extracting 1636 from "3456" and 1925 from "01245", or outputting -1 for the string "1234". Pseudocode explains the algorithm by taking the input, looping through indices, squaring odd digits, concatenating them, and printing the first 4 digits or -1.

Uploaded by

Gaurav Yadav 093
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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:????

Check for Odd index

Index [4], even = discard = don’t Pick


Index [3] odd , pick and square = 6^2 = 36 = Last Two digit of OTP
Index [2], even = discard = don’t Pick
Index [1] odd , pick and square = 4^2 = 16 = First Two digit of OTP
Index [0], even = discard = don’t Pick
4
Input 3 4 5 6 7
String 1

Output 1636 1636


OTP

☺ That’s the work to do ☺

5
SAMPLE INPUT 2

6
0 1 2 3
String 2 OTP:????

Check for Odd index

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

Index [0], even = discard = don’t Pick


7
Input 3 4 5 6 7
String 2

Output -1 ( As no four ----


digits are formed) OTP
Not Generated

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

Index [4], even = discard = don’t Pick


Index [3] odd , pick and square = 3^2 = 9 = Next digit of OTP

Index [2], even = discard = don’t Pick


Index [1] odd , pick and square = 1^2 = 1 = First digit of OTP

Index [0], even = discard = don’t Pick


10
Input 0 1 2 3 4 5
String 3

Output 1925 1925

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

◦ Converting string to integer and then squaring


◦ Concatenating the values
◦ If length of string is >= 4 ,
◦ Then print the 4 digit number
◦ Else print -1
Step 3: Output
◦ 4 digit number

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy