Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to create an OTP by squaring and concatenating the odd digits of a number
The task is to create a One-Time Password (OTP) by squaring and concatenating the odd digits of a given number. This technique extracts odd digits from a number, squares each one, and joins them together to form a secure OTP.
Input Output Scenarios
Following are the input-output scenarios for creating an OTP by squaring and concatenating the odd digits of a number ?
# Example 1
number = 123456789
print(f"Input number: {number}")
# Extract odd digits: 1, 3, 5, 7, 9
# Square them: 1, 9, 25, 49, 81
# Concatenate: "19254981"
otp = "19254981" # Expected result
print(f"Output OTP: {otp}")
Input number: 123456789 Output OTP: 19254981
The odd digits in the number are 1, 3, 5, 7, 9. Squaring each of these digits gives us 1, 9, 25, 49, 81. Concatenating these squared digits together gives us the OTP 19254981.
# Example 2
number = 54321
print(f"Input number: {number}")
# Extract odd digits: 5, 3, 1
# Square them: 25, 9, 1
# Concatenate: "2591"
otp = "2591" # Expected result
print(f"Output OTP: {otp}")
Input number: 54321 Output OTP: 2591
The odd digits in the input number are 5, 3, and 1. Squaring these digits gives us 25, 9, and 1. Concatenating these squared digits gives us the OTP 2591.
Approach
We can follow the below steps to create an OTP by squaring and concatenating the odd digits of a given number ?
Define a function that takes a number as input.
Initialize an empty list to store the squared odd digits.
Iterate over the digits of the number.
For each digit, check if it is odd by using the modulo operator (digit % 2 != 0). If the digit is odd, square it (digit ** 2) and store it.
After processing all the digits, join the squared digits together using the join method, which concatenates the elements of a list into a single string. The squared digits are joined without any separator, resulting in a single string.
Return the concatenated OTP string.
Method 1: Using While Loop
This approach extracts digits from right to left using mathematical operations ?
def create_otp_while(number):
odd_digits = []
while number > 0:
digit = number % 10
if digit % 2 != 0: # Check if digit is odd
odd_digits.append(str(digit ** 2))
number //= 10
otp = "".join(odd_digits[::-1]) # Reverse to maintain original order
return otp
# Test the function
number = 789
print("Input number:", number)
otp = create_otp_while(number)
print("OTP:", otp)
Input number: 789 OTP: 4981
Method 2: Using String Conversion
This method converts the number to a string and processes each character ?
def create_otp_string(number):
odd_digits = [int(digit) for digit in str(number) if int(digit) % 2 != 0]
otp = "".join(str(digit**2) for digit in odd_digits)
return otp
# Test the function
number = 12349
print("Input number:", number)
otp = create_otp_string(number)
print("OTP:", otp)
Output number: 12349 OTP: 1981
Complete Example
Here's a comprehensive example testing both methods with multiple inputs ?
def create_otp(number):
"""Create OTP by squaring and concatenating odd digits"""
odd_digits = [int(digit) for digit in str(number) if int(digit) % 2 != 0]
otp = "".join(str(digit**2) for digit in odd_digits)
return otp
# Test with multiple numbers
test_numbers = [123456789, 54321, 789, 2468, 13579]
for num in test_numbers:
otp = create_otp(num)
print(f"Number: {num} ? OTP: {otp}")
Number: 123456789 ? OTP: 19254981 Number: 54321 ? OTP: 2591 Number: 789 ? OTP: 4981 Number: 2468 ? OTP: Number: 13579 ? OTP: 19254981
Key Points
Only odd digits (1, 3, 5, 7, 9) are processed
Each odd digit is squared before concatenation
The order of digits is preserved from left to right
If no odd digits exist, an empty string is returned
The string method is more concise and readable
Conclusion
Creating an OTP by squaring odd digits is an effective way to generate secure passwords. The string conversion method provides cleaner code, while the mathematical approach offers better understanding of digit extraction.
