Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can I extend a character limit or shorten the code?

+1 vote
asked Mar 28, 2022 by Joey (540 points)
edited Mar 28, 2022 by Joey
Alright, so I have this idea for a program that I would like to write to goof on some friends of mine with. I want to use the replace function to replace the letters they input with "binary" code and have a second replace function after that that will return a different message all together. The code I have so far is:

phrase = input()

if phrase in phrase:

phrase = phrase.replace('a', '0')

if phrase in phrase:

phrase = phrase.replace('b', '1')

etc. etc. etc. all the way to z but it is indented after every letter so that by the time I get down to t, u, v, etc. it is beyond the 120 character limitation in my pycharm. Is there a simplified way to do this? Or maybe a way to ignore the character limitation per line? Do I really need all those indentations? Am I just going about this from the hardest angle imaginable? Any insight and suggestions are welcome and appreciated.

1 Answer

0 votes
answered Mar 28, 2022 by Peter Minarik (79,800 points)

Loop through all the characters in your phrase and increment the character value by '0' - 'a' (yes, the difference between two characters).

In Python, this would look something like this:

phrase = input('?')
shift = ord('0') - ord('a')
chars = list(phrase)
length = len(chars)
i = 0
while i < length:
    chars[i] = chr(ord(chars[i]) + shift)
    i += 1
phrase = ''.join(chars)

commented Mar 28, 2022 by Joey (540 points)
so from the looks of this, and correct me if I'm wrong, this code is cycling through each of the characters in the length of the phrase input with the chars[i] but I'm a bit behind on the rest. I may be off by a bit... could you explain this bit of code with a lil more detail?
commented Mar 30, 2022 by Peter Minarik (79,800 points)
edited Mar 30, 2022 by Peter Minarik
If I got you correctly what you wanted to do is replace characters with some other characters.

The above code maps
'a' --> '0'
'b' --> '1'
'j' --> '9'
'k' --> ':'
'l' --> ';'
'y' --> 'H'
'z' --> 'I'

That is, we take the character codes starting from 'a' (char code 97) and rebase them to start from '0' (char code 48). Check the ASCII table for reference on the ASCII character codes: https://www.asciitable.com/

If this is not what you want, please, provide more detail on your mapping.

So this is what the above code does. It calculates the shift value between the two bases ('a' and '0') and applies the translation on every character code.

In Python, it's a bit tricky (impossible?) to directly change the characters in a string. For this, the following technique was applied.

We need to break the string into characters (list of characters), then we can apply the translations to the character codes. After this, we reassemble (join) the list of characters into a string again.

The following code turns a string (which really should be just a character) into a number (calling ord()), incrementing it by *shift*, then turning it back into a single character again (via chr()):

chr(ord(chars[i]) + shift)

I hope this is clearer now.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.