2

Winkleson here with another question for a http://singpath.appspot.com problemset question. I am a beginner programmer who would like some guidance whenever I can. Anyways I came up to this question here and I am not sure how to proceed without wasting time by comparing EVERY.SINGLE.LETTER. in if statements. So I would love any hints/solutions to cut down on the coding for this question. I'll post what I have so far (not much). Thanks in advance!

Question:


Substitution Encryption

Create a program that can be used to encrypt and decrypt a string of letters. The function should take input of a string to encode and a string of letters giving the new order of the alphabet. The second string contains all characters of the alphabet but in a new order. This order tells what letters to swap. The first letter in the second string should replace all the a's in the first string. The third letter of the second string should replace all c's in the first string. Your solution should be in all lower case. Be careful of punctuation and digits (these should not change).

Examples (calls):

>>> encrypt('hello banana','qwertyuiopasdfghjklzxcvbnm')
'itssg wqfqfq'

>>> encrypt('itssg wqfqfq','kxvmcnophqrszyijadlegwbuft')
'hello banana'

>>> encrypt('gftw xohk xzaaog vk xrzxnkh','nxqlzhtdvfepmkoywrjiubscga')
'this code cannot be cracked'

>>> encrypt('mkhzbc id hzw pwdh vtcgxtgw ube fbicg ozth kbx tew fbicg','monsrdgticyxpzwbqvjafleukh')
'python is the best language for doing what you are doing'

My code:


def encrypt(s, realph):
    
    alph = 'abcdefghijklmnopqrstuvwxyz' #Regular Alphabet
    news = '' #The decoded string   
        
    #All comparison(s) between realph and alph    
        
    for i in range(len(realalph)):        
        
        #Comparison Statement here too.
        news = ''.join(alph) 
    
    return news

As you can see this is obviously the equivalent to failed pseudocode... As always any suggestions and/or solutions would be amazing! Thanks in advance! - Winkleson

4
  • 2
    Just a tip, if you dont't know about it. If you prefer, you don't need to enter the right ordered alphabet yourself, you can do a import string and then alph = string.lowercase Commented Nov 21, 2012 at 19:52
  • @NiclasNilsson Better than reciting the alphabet outloud like a clown XD But isn't the command alph = string.ascii_lowercase ? I forgot all about it :P Thanks for the tip/reminder! Commented Nov 21, 2012 at 19:57
  • Both work. Depends on if you want it to be locale aware or not. ascii_lowercase never changes so might be better for this purpose. Commented Nov 21, 2012 at 20:01
  • @NiclasNilsson Okay then it is something I didn't know :P Thanks for sharing! Commented Nov 21, 2012 at 20:06

3 Answers 3

4

Here's a translation solution.

from string import maketrans

def encrypt(s, scheme):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    translation = maketrans(alphabet, scheme)
    return s.translate(translation)

Strings have a built in translate method allowing you to switch single letters out with other letters. Incredibly snappy, and quite useful.

Sign up to request clarification or add additional context in comments.

2 Comments

THANKYOU. That snipet would have been so useful to know in the past. This is just one of the many reason's I love StackOverflow. You can always learn new solutions or code out of simple questions. I guess I should look more into Python's Std library... Anyways thankyou for this solution. I would love to see other methods to solve the question but this seems as simplistic as it can get. Thanks again!
I just thought of something... could you do the same thing with a combination of upper and lower case character's by holding them all into the alphabet and scheme holding both versions of the alphabet (with different positions of course). I don't see why it wouldn't :)
1

I'd create a map['src_letter'] => 'dst_letter'. There's also translations: Replace characters in string from dictionary mapping

1 Comment

Interesting, I was thinking about using dicts (Key,value pairs) but I am not too familiar with them :P Thanks for the link I'll check it out!
1

Here's how I'd do it:

  1. Iterate over every character in the string.
  2. Find the index of that character in your alphabet. Save it.
  3. Take the character from your new alphabet that is in the same index.
  4. Append it to your news string.

Pseudocode:

output = ''

for character in your_string:
    index = index of character in original_alphabet
    new_character = new_alphabet[character]

    add new_character to output

2 Comments

Yeah that's the basic gameplan... Any ideas on how to do it pythonicly though? I would end up with one if statement per character or one really long conjoined (or operator) if statement. Should I throw it into a loop for every char or...? Thanks for your input!
Thanks that makes a bit more sense to me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.