0

I have been doing simple programming challenges all day, trying to learn and practice. I however, always seems to fail at efficiency. Without using built_in code (such as the encode method), is there anyway that I could improve my program's efficiency (my efficiency in general)?

import string
alph = string.ascii_lowercase
def encrypt(text):

    encryption = ""

    for character in text:

        index = 0
        shift = 0
        for letter in alph:                       
            if letter == character:

                if index > 23:
                    shift = abs(26 - (index+3))
                    encryption += alph[shift]
                    break
                shift = index + 3
                encryption += alph[shift]

            index += 1

    return encryption

def decrypt(text):

    decryption = ""

    for character in text:

        index = 0
        shift = 0
        for letter in alph:                       
            if letter == character:

                if index < 3:
                    shift = abs(26 - (index+3))
                    decryption += alph[shift]
                    break
                shift = index - 3
                decryption += alph[shift]

            index += 1

    return decryption
zeffii
  • 534
  • 9
  • 22
Jacob Worldly
  • 96
  • 1
  • 2
  • 6
  • 1
    This is currently very broad -- it already would be much better if you'd asked about the efficiency of some construct. You don't define what efficiency is (in writing, runtime performance, ...) for you though, so it's impossible to tell what the correct answer would be. – Benjamin Bannier Dec 07 '12 at 09:09

4 Answers4

1

You could use slices and str.maketrans, str.translate (see Python.org : string):

import string

def rot3_encode(s):
    return s.translate(
            string.maketrans(
                # 'abcdefghijklmnopqrstuvwxyz'
                string.ascii_lowercase,
                # 'defghijklmnopqrstuvwxyz' + 'abc'
                string.ascii_lowercase[3:] + string.ascii_lowercase[:3] # 
                )
            )

without using translate and maketrans :

def rot3(s):
    # 'abcdefghijklmnopqrstuvwxyz'
    original_alphabet = string.ascii_lowercase 
    # 'defghijklmnopqrstuvwxyz' + 'abc'
    encoded_alphabet = string.ascii_lowercase[3:] + string.ascii_lowercase[:3]
    encoded_string = ''
    for character in s:
        # look at what index your character is in the original alphabet
        encoded_string += encoded_alphabet[original_alphabet.index(character)]
    return encoded_string

for exemple:

rot3('afz')
# 'a' is at index 0 of 'abcdefghijklmnopqrstuvwxyz'
# -> you will append to your encoded string the character at index 0 of 'defghijklmnopqrstuvwxyzabc' ('d')
# 'f' is at index 5 of 'abcdefghijklmnopqrstuvwxyz'
# -> you will append to your encoded string the character at index 5 of 'defghijklmnopqrstuvwxyzabc' ('i')
# ...
>>>'dic'
Community
  • 1
  • 1
rxdazn
  • 1,380
  • 1
  • 14
  • 30
1

using string formatting "%s%s" (encryption, newvalue) is 2x faster, compared using += and + the difference gets even bigger with larger strings.

see String concatenation vs. string substitution in Python

Community
  • 1
  • 1
Stephan
  • 3,679
  • 3
  • 25
  • 42
0

Instead of explicit index += 1 you can for example use for index, letter in enumerate(alph):. This shrinks the code a little and tracks iteration index automatically.

zeffii
  • 534
  • 9
  • 22
0

Invoking it this way to see where the time is consumed is your most basic tool for improving performance...

 python -m cProfile foo.py

See here for more

John Mee
  • 50,179
  • 34
  • 152
  • 186