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
import stringand thenalph = string.lowercase