PROBLEM LINK:
Author: Setter’s name
Editorialist: Editorialist’s name
DIFFICULTY:
SIMPLE-EASY
PROBLEM:
You are assigned the task to store a password after encryption to ensure its security. You decide to replace every character of the input with a digit.
QUICK EXPLANATION:
The problem can be solved either by recursion or by a while loop structure. The main task is to keep on adding the digits till you get a single digit number.
EXPLANATION:
Input the string str.
Traverse str to extract each character. If the character is an alphabet, find out its position in the english alphabet (start from 0).
For character a, position =0
b, position =1
Similarly, z, position =25
Find out the sum of this position and the character’s position in the input string str.
If the character is a digit, add the digit and the position of the digit in str.
Keep adding the digits of the resultant sum until you obtain a single digit.
SOLUTIONS:
Setter's Solution
def dig(n):
summ=0
while(n>0):
summ+=n%10
n=n//10
if summ>9:
summ=dig(summ)
return summ
def encode(msg):
msg=msg.lower()
s="abcdefghijklmnopqrstuvwxyz"
num="1234567890"
d={}
for i in range(26):
d[s[i]]=i
l=[]
for i in range(len(msg)):
if msg[i] in s:
n=d[msg[i]]+i
n=dig(n)
elif msg[i] in num:
n=int(msg[i])+i
n=dig(n)
else:
n=msg[i]
l.append(n)
for i in range(len(l)):
if i !=len(l)-1:
print(l[i], end=" ")
else:
print(l[i])
msg=input()
encode(msg)
Editorialist's Solution
def dig(n):
summ=0
while(n>0):
summ+=n%10
n=n//10
if summ>9:
summ=dig(summ)
return summ
def encode(msg):
msg=msg.lower()
s="abcdefghijklmnopqrstuvwxyz"
l=[]
i=0
while i <len(msg):
if msg[i] in s:
n=ord(msg[i])-ord('a')+i
n=dig(n)
elif msg[i]>='0' and msg[i]<='9':
n=int(msg[i])+i
n=dig(n)
else:
n=msg[i]
l.append(n)
i=i+1
for i in range(len(l)):
if i !=len(l)-1:
print(l[i], end=" ")
else:
print(l[i])
msg=input()
encode(msg)