Skip to main content
fixed oversight
Source Link
Daniel
  • 4.6k
  • 2
  • 18
  • 40

Instead of having those ifs you could use a translation table to speed things up (see str.maketrans and seestr.translate).

import string

def _build_translation_table():
    ascii_vowels = 'aeiou'
    d = {c: chr(ord(c)+1) for c in string.ascii_lowercase}
    d['z'] = 'a'             # z is a special case
    for k, v in d.items():   # and so are vowels
        if v in ascii_vowels:
            d[k] = v.upper()
    return str.maketrans(d)

_TABLE = _build_translation_table()

def LetterChanges(s):
    """Change letters in string s using a special algorithm.

    Replace every letter in the string with the letter following it in the
    alphabet (ie. c becomes d, z becomes a) ...

    >>> LetterChanges('qwertyuiop')
    'rxfsUzvjpq'
    >>> LetterChanges('1234567890')
    '1234567890'
    >>> LetterChanges('zxcvbnm')
    'AydwcOn'
    """

    return s.translate(_TABLE)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

I took the liberty to rename the parameter str to s for reasons already mentioned by others. _build_translation_table and _TABLE start with an underscore because it doesn't make much sense for them to be "public".

I also took some tests from Josay's answer and put them into the documentation string of the function, so that the doctest module can run them.

# python3 letterchanges.py -v
Trying:
    LetterChanges('qwertyuiop')
Expecting:
    'rxfsUzvjpq'
ok
Trying:
    LetterChanges('1234567890')
Expecting:
    '1234567890'
ok

Instead of having those ifs you could use a translation table to speed things up (see str.maketrans and see.translate).

import string

def _build_translation_table():
    ascii_vowels = 'aeiou'
    d = {c: chr(ord(c)+1) for c in string.ascii_lowercase}
    d['z'] = 'a'             # z is a special case
    for k, v in d.items():   # and so are vowels
        if v in ascii_vowels:
            d[k] = v.upper()
    return str.maketrans(d)

_TABLE = _build_translation_table()

def LetterChanges(s):
    """Change letters in string s using a special algorithm.

    Replace every letter in the string with the letter following it in the
    alphabet (ie. c becomes d, z becomes a) ...

    >>> LetterChanges('qwertyuiop')
    'rxfsUzvjpq'
    >>> LetterChanges('1234567890')
    '1234567890'
    >>> LetterChanges('zxcvbnm')
    'AydwcOn'
    """

    return s.translate(_TABLE)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

I took the liberty to rename the parameter str to s for reasons already mentioned by others. _build_translation_table and _TABLE start with an underscore because it doesn't make much sense for them to be "public".

I also took some tests from Josay's answer and put them into the documentation string of the function, so that the doctest module can run them.

# python3 letterchanges.py -v
Trying:
    LetterChanges('qwertyuiop')
Expecting:
    'rxfsUzvjpq'
ok
Trying:
    LetterChanges('1234567890')
Expecting:
    '1234567890'
ok

Instead of having those ifs you could use a translation table to speed things up (see str.maketrans and str.translate).

import string

def _build_translation_table():
    ascii_vowels = 'aeiou'
    d = {c: chr(ord(c)+1) for c in string.ascii_lowercase}
    d['z'] = 'a'             # z is a special case
    for k, v in d.items():   # and so are vowels
        if v in ascii_vowels:
            d[k] = v.upper()
    return str.maketrans(d)

_TABLE = _build_translation_table()

def LetterChanges(s):
    """Change letters in string s using a special algorithm.

    Replace every letter in the string with the letter following it in the
    alphabet (ie. c becomes d, z becomes a) ...

    >>> LetterChanges('qwertyuiop')
    'rxfsUzvjpq'
    >>> LetterChanges('1234567890')
    '1234567890'
    >>> LetterChanges('zxcvbnm')
    'AydwcOn'
    """

    return s.translate(_TABLE)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

I took the liberty to rename the parameter str to s for reasons already mentioned by others. _build_translation_table and _TABLE start with an underscore because it doesn't make much sense for them to be "public".

I also took some tests from Josay's answer and put them into the documentation string of the function, so that the doctest module can run them.

# python3 letterchanges.py -v
Trying:
    LetterChanges('qwertyuiop')
Expecting:
    'rxfsUzvjpq'
ok
Trying:
    LetterChanges('1234567890')
Expecting:
    '1234567890'
ok
added doctests
Source Link

Instead of having those ifs you could use a translation table to speed things up (see str.maketrans and see.translate).

import string

def _build_translation_table():
    ascii_vowels = 'aeiou'
    d = {c: chr(ord(c)+1) for c in string.ascii_lowercase}
    d['z'] = 'a'             # z is a special case
    for k, v in d.items():   # and so are vowels
        if v in ascii_vowels:
            d[k] = v.upper()
    return str.maketrans(d)

TABLE_TABLE = _build_translation_table()

def LetterChanges(s):
    """Change letters in string s using a special algorithm.

    Replace every letter in the string with the letter following it in the
    alphabet (ie. c becomes d, z becomes a) ...

    >>> LetterChanges('qwertyuiop')
    'rxfsUzvjpq'
    >>> LetterChanges('1234567890')
    '1234567890'
    >>> LetterChanges('zxcvbnm')
    'AydwcOn'
    """

    return s.translate(TABLE_TABLE)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

I took the liberty to rename the parameter str to s for reasons already mentioned by others. _build_translation_table startsand _TABLE start with an underscore because it doesn't make much sense for itthem to be "public".

I also took some tests from Josay's answer and put them into the documentation string of the function, so that the doctest module can run them.

# python3 letterchanges.py -v
Trying:
    LetterChanges('qwertyuiop')
Expecting:
    'rxfsUzvjpq'
ok
Trying:
    LetterChanges('1234567890')
Expecting:
    '1234567890'
ok

Instead of having those ifs you could use a translation table to speed things up (see str.maketrans and see.translate).

import string

def _build_translation_table():
    ascii_vowels = 'aeiou'
    d = {c: chr(ord(c)+1) for c in string.ascii_lowercase}
    d['z'] = 'a'             # z is a special case
    for k, v in d.items():   # and so are vowels
        if v in ascii_vowels:
            d[k] = v.upper()
    return str.maketrans(d)

TABLE = _build_translation_table()

def LetterChanges(s):
    return s.translate(TABLE)

I took the liberty to rename the parameter str to s for reasons already mentioned by others. _build_translation_table starts with an underscore because it doesn't make much sense for it to be "public".

Instead of having those ifs you could use a translation table to speed things up (see str.maketrans and see.translate).

import string

def _build_translation_table():
    ascii_vowels = 'aeiou'
    d = {c: chr(ord(c)+1) for c in string.ascii_lowercase}
    d['z'] = 'a'             # z is a special case
    for k, v in d.items():   # and so are vowels
        if v in ascii_vowels:
            d[k] = v.upper()
    return str.maketrans(d)

_TABLE = _build_translation_table()

def LetterChanges(s):
    """Change letters in string s using a special algorithm.

    Replace every letter in the string with the letter following it in the
    alphabet (ie. c becomes d, z becomes a) ...

    >>> LetterChanges('qwertyuiop')
    'rxfsUzvjpq'
    >>> LetterChanges('1234567890')
    '1234567890'
    >>> LetterChanges('zxcvbnm')
    'AydwcOn'
    """

    return s.translate(_TABLE)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

I took the liberty to rename the parameter str to s for reasons already mentioned by others. _build_translation_table and _TABLE start with an underscore because it doesn't make much sense for them to be "public".

I also took some tests from Josay's answer and put them into the documentation string of the function, so that the doctest module can run them.

# python3 letterchanges.py -v
Trying:
    LetterChanges('qwertyuiop')
Expecting:
    'rxfsUzvjpq'
ok
Trying:
    LetterChanges('1234567890')
Expecting:
    '1234567890'
ok
Source Link

Instead of having those ifs you could use a translation table to speed things up (see str.maketrans and see.translate).

import string

def _build_translation_table():
    ascii_vowels = 'aeiou'
    d = {c: chr(ord(c)+1) for c in string.ascii_lowercase}
    d['z'] = 'a'             # z is a special case
    for k, v in d.items():   # and so are vowels
        if v in ascii_vowels:
            d[k] = v.upper()
    return str.maketrans(d)

TABLE = _build_translation_table()

def LetterChanges(s):
    return s.translate(TABLE)

I took the liberty to rename the parameter str to s for reasons already mentioned by others. _build_translation_table starts with an underscore because it doesn't make much sense for it to be "public".