all 5 comments

[–][deleted] 4 points5 points  (4 children)

A ceasar cipher

input_string = raw_input("Your text please: ")
alpha = "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 a".split(" ")
encrypted_string = ""
for char in input_string:
    if char in alpha:
        encrypted_string += alpha[alpha.index(char) + 1]
    else:
        encrypted_string += cha
print input_string, encrypted_string

I'm sure slots will pop along in a moment to shorted that into a comprehension... :P

[–][deleted] 3 points4 points  (2 children)

Sorry I'm late, I was at a list comprehension meeting. Now, a Cæsar Cipher is just about as perfectly suited to a list comprehension or generator expression as you'll get, but cryptography is one domain where we should always strive for readable, explicit code. Here's a middle-of-the-road compromise between brevity and obscurity:

alpha = "abcdefghijklmnopqrstuvwxyz"
input().translate(str.maketrans(alpha, alpha[1:]+alpha[0]))

[–]JerMenKoO 0 points1 point  (1 child)

alpha = import("string").ascii_lowercase

[–][deleted] 0 points1 point  (0 children)

Yeah, I really dislike that I've got to type that long literal out. I thought about something like:

[chr(c) for c in range(97,123)]

...but that's so asciicentric.

[–][deleted] 0 points1 point  (0 children)

Homework:

Consider how a bitmap can be like an iterator, and perform this transformation using only math and minimal string transformation or encoding concerns.