Replace every character of string by character whose ASCII value is K times more than it

+1 vote

Secret Message

Description

You write all your passwords in a diary so that you don't forget them. But clearly this is too risky, so you came up with a simple plan, you will simply write it by shifting all the alphabets by a certain step. For eg: if you decide your step to be 3, then 'a' will become 'd', and 'k' will become 'n' and so for all alphabets. The last alphabets will simply circle back to 'a'. In this case, 'y' will become 'b' and so on. Now you just have to remember the step size, can then you can check the password anytime you want. You decided to write code to do this, now that you have learned coding in python. Your code will take in the step size and what is written in the diary and give out the real password.

----------------------------------------------------------------------
Input:
A list of two elements.
The first element will be a string consisting of only alphabets which is taken from the diary and the second element will be the step size.

Output:
A string denoting the password

----------------------------------------------------------------------
Sample input:
['ytKveh' 4]

Sample output:
upGrad

Explanation:
The password was 'upGrad'. Upon changing it by step of 4,
u became y,
p became t,
G became K,
r became v,
a became e,
d became h,
and thus what was written in the diary was ytKveh 

----------------------------------------------------------------------
Sample input:
['banana', 7]

Sample output:
utgtgt

Execution time limit

Default.

Dec 2, 2019 in Python by Vaibhav
• 130 points
16,712 views
import ast
mylist=ast.literal_eval(input())
stringMessage=mylist[0]
shift_value=mylist[1]
#start writing your code from here
def shift_string(message, n):
    list1=[]
    for i in message:
        ch = i
        base = ord('a' if ch.islower() else 'A')
        x = chr((ord(ch) - base - n) % 26 + base)
        list1.append(x)
    return("".join(list1))
print(shift_string(stringMessage,shift_value))

2 answers to this question.

0 votes

Hi @Vaibhav! In such a case what you can do is, 

ask the user for the password(which will be in the string format)

take each letter of the string and convert it into a character and use the ord() which will return its Unicode.

To this Unicode, you can add the count value and convert it back to a string.

Finally, print it'

You can use the following code:

password = input("Enter the password")
count = input("Enter the count")
final_pass = ""
for x in list(password):
    y = chr(ord(x) + count)
    final_pass = final_pass + y
print(final_pass)
answered Dec 5, 2019 by Kalgi
• 52,360 points
it does't works for i/p banana and 7
This will give issue when the last alphabet is Z and the requirement is to have only alphabets in the output.
0 votes

Refer below code for your problem statement

def swap_letters(word, step_count):
    final_word = ""
    for x in list(word):
#         for lowercase letters crossing the Ascii values
        if(ord(x)-step_count<97 and ord(x)>96):
            w = 123-abs(97-(ord(x)-step_count))
            y = chr(w)
            final_word = final_word + y
            
#           for uppercase letters crossing the Ascii values
        elif(ord(x)-step_count<65 and ord(x)<91):
            w = 91-abs(65-(ord(x)-step_count))
            y = chr(w)
            final_word = final_word + y
            
#           for normal letters within the Std Ascii Values
        else:
            y = chr(ord(x)-step_count)
            final_word = final_word + y
    return final_word
    
swap_letters(input(),int(input()))

answered May 17, 2020 by Kumar Sambhawam

edited May 18, 2020 by Gitika

Related Questions In Python

0 votes
1 answer

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Hello @kartik, Since different users might have different ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
22,885 views
0 votes
0 answers

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I couldn't figure out the problem. here my ...READ MORE

Dec 17, 2020 in Python by muammer
• 120 points
7,315 views
0 votes
1 answer

Python join: why is it string.join(list) instead of list.join(string)?

950down voteaccepted It's because any iterable can be ...READ MORE

answered May 15, 2018 in Python by aryya
• 7,450 points
693 views
0 votes
1 answer

Python join: why is it string.join(list) instead of list.join(string)?

This is because join is a "string" ...READ MORE

answered Jul 30, 2018 in Python by Priyaj
• 58,090 points
627 views
0 votes
1 answer

Why is it string.join(list) instead of list.join(string)?

It's because any iterable can be joined ...READ MORE

answered Dec 2, 2020 in Python by Gitika
• 65,910 points
487 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
+7 votes
8 answers

Difference for string comparison in Python: 'is' vs. ==

If we use "==" means both variables ...READ MORE

answered Sep 3, 2018 in Python by Parul Raheja
1,735 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP