Python Forum

Full Version: help with replacing charactersdef fix(block): alf = ['a', 'b', 'c', ' in a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to take a string and replace every letter in the string with the second to next letter in the alphabet. eg(a => c, b => d). My code changes the string does not save and will change back to the original string when changing the next letter. Thanks for any help


def fix(block):
    alf = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    cur = 0
    old = 0
    new = 2

    while cur < 26:
        new_block = []
        get = block.replace(alf[old], alf[new])
        old = old + 1
        new = new + 1
        cur = cur + 1
        return get
(Apr-18-2017, 01:43 PM)Marshall_99 Wrote: [ -> ]I want to take a string and replace every letter in the string with the second to next letter in the alphabet. eg(a => c, b => d). My code changes the string does not save and will change back to the original string when changing the next letter. Thanks for any help


def fix(block):
    alf = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    cur = 0
    old = 0
    new = 2

    while cur < 26:
        new_block = [] #nevermind this 
        get = block.replace(alf[old], alf[new])
        old = old + 1
        new = new + 1
        cur = cur + 1
        return get
Here are the issues in your code:
  1. You always replace in the same block - overwriting get with the latest replacement operation result
  2. You don't check crossing list boundaries
  3. Remember p.1? It does not matter, because you exit after first iteration!
  4. You may end up replacing character many times - if you replace 'a' with 'c', then you'll replace 'c' with 'e', etc
  5. There are better ways to manage loop!
  6. There is module string that contains ascii_lowercase constant - well, that you may not have known
  7. Variable names - well, they are not very successful
Since you have shown some effort - here is the proper way to do it (I hope it is not homework)
from string import ascii_lowercase
def fix(word, char_offset=2):
    new_word = ''
    for char_ in word:
        # Get index of replacement character and keep it below 26, so for "z" it will be "b"
        char_index = ascii_lowercase.find(char_)
        if char_index > -1:
            replacement_index =  (char_index + char_offset) % 26
            new_word += ascii_lowercase[replacement_index]
         else:
            new_word += char_
    return new_word
PS I did not test the code - if there are some glitches, I leave fixing them to you as an excercise
(Apr-18-2017, 07:14 PM)volcano63 Wrote: [ -> ]here is the proper way to do it (I hope it is not homework)
Okay way,he will get full score if teacher is not full out Pythonic python
Look like a typicality homework task.
So when we are in solution modus. 
from string import ascii_lowercase

def fix(word, char_offset=2):
    char = ascii_lowercase
    for index,item in enumerate(word, char_offset):
        yield char[index]
Test:
>>> s = 'abcde'
>>> ''.join(fix(s))
'cdefg'
>>> list(fix(s))
['c', 'd', 'e', 'f', 'g']
(Apr-18-2017, 08:45 PM)snippsat Wrote: [ -> ]
(Apr-18-2017, 07:14 PM)volcano63 Wrote: [ -> ]here is the proper way to do it (I hope it is not homework)
Okay way,he will get full score if teacher is not full out Pythonic python
Look like a typicality homework task.
So when we are in solution modus. 
from string import ascii_lowercase

def fix(word, char_offset=2):
    char = ascii_lowercase
    for index,item in enumerate(word, char_offset):
        yield char[index]

Wow! Could you please explain how that runs without exception?
>>> list(fix('xyz'))

['c', 'd', 'e']
Ooops, aren't you a sneaky one?
(Apr-18-2017, 09:00 PM)volcano63 Wrote: [ -> ]Ooops, aren't you a sneaky one?
He a little quick,tested only start Blush
(Apr-18-2017, 09:22 PM)snippsat Wrote: [ -> ]
(Apr-18-2017, 09:00 PM)volcano63 Wrote: [ -> ]Ooops, aren't you a sneaky one?
He a little quick,tested only start Blush

I should have caught it earlier, but after couple of weeks of insomnia .... Cry And it looked so convincing  Confused
So a fix.
def fix(word, char_offset=2):
   a = ord('a')
   return ''.join(chr((ord(char) - a + char_offset) % 26 + a) for char in word.lower())
Test:
>>> fix('abc')
'cde'
>>> fix('xyz')
'zab'
I think also that deque that has build in rotate,
and maybe combo with str.maketrans could be okay.
(Apr-18-2017, 11:20 PM)snippsat Wrote: [ -> ]I think also that deque that has build in rotate,
and maybe combo with str.maketrans could be okay.

Now you are showing off  LOL
Thank you both for all your help! Don't worry it was not homework, just my own challenge.