Re: String manipulation in python..NEED HELP!!!!
On 10/12/12 22:38, qbai...@ihets.org wrote: I need help with a program i am doing. it is a cryptography program. i am given a regular alphabet and a key. i need to use the user input and use the regular alphabet and use the corresponding letter in the key and that becomes the new letter. i have the basic code but need help with how to mainpulate the string to do the encryption/decryption. please help here is my code so far: crypto.py Implements a simple substitution cypher alpha = ABCDEFGHIJKLMNOPQRSTUVWXYZ key = XPMGTDHLYONZBWEARKJUFSCIQV def main(): keepGoing = True while keepGoing: response = menu() if response == 1: plain = raw_input(text to be encoded: ) print encode(plain) elif response == 2: coded = raw_input(code to be decyphered: ) print decode(coded) elif response == 0: print Thanks for doing secret spy stuff with me. keepGoing = False else: print I don't know what you want to do... i really need help on how to encrypt it im not sure how to go about doing that please help. alpha = ABCDEFGHIJKLMNOPQRSTUVWXYZ key = XPMGTDHLYONZBWEARKJUFSCIQV mapping = {} for i, ch in enumerate(alpha): mapping[ch] = key[i] ''.join(mapping[ch] for ch in ACE) 'XMT' ''.join(mapping[ch] for ch in WORD) 'CEKG' Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: String manipulation in python..NEED HELP!!!!
On 12/10/2012 5:59 PM, John Gordon wrote: def encode(plain): '''Return a substituted version of the plain text.''' encoded = '' for ch in plain: encoded += key[alpha.index(ch)] return encoded The turns an O(n) problem into a slow O(n*n) solution. Much better to build a list of chars and then join them. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: String manipulation in python..NEED HELP!!!!
John Gordon wrote: def encode(plain): '''Return a substituted version of the plain text.''' encoded = '' for ch in plain: encoded += key[alpha.index(ch)] return encoded Terry Reedy tjre...@udel.edu wrote: The turns an O(n) problem into a slow O(n*n) solution. Much better to build a list of chars and then join them. There have been much better suggestions in this thread, but John Gordon's code above is faster than the equivilent list and join implementation with Python 2.6 and Python 3.1 (the newest versions I have handy). CPython optimized this case of string concatenation into O(n) back in Python 2.4. Ross Ridge -- l/ // Ross Ridge -- The Great HTMU [oo][oo] rri...@csclub.uwaterloo.ca -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/ db // -- http://mail.python.org/mailman/listinfo/python-list
Re: String manipulation in python..NEED HELP!!!!
On 12 December 2012 07:52, Ross Ridge rri...@csclub.uwaterloo.ca wrote: John Gordon wrote: def encode(plain): '''Return a substituted version of the plain text.''' encoded = '' for ch in plain: encoded += key[alpha.index(ch)] return encoded Terry Reedy tjre...@udel.edu wrote: The turns an O(n) problem into a slow O(n*n) solution. Much better to build a list of chars and then join them. There have been much better suggestions in this thread, but John Gordon's code above is faster than the equivilent list and join implementation with Python 2.6 and Python 3.1 (the newest versions I have handy). CPython optimized this case of string concatenation into O(n) back in Python 2.4. From What's New in Python 2.4: http://docs.python.org/release/2.4.4/whatsnew/node12.html#SECTION000121 String concatenations in statements of the form s = s + abc and s += abc are now performed more efficiently *in certain circumstances*. This optimization *won't be present in other Python implementations such as Jython*, so you shouldn't rely on it; using the join() method of strings is still recommended when you want to efficiently glue a large number of strings together. Emphasis mine. The optimisation was added to improve the situation for programs that were already using the anti-pattern of string concatenation, not to encourage people to use it. As a real-world case, a bug was recently found in Mercurial where an operation on Windows was taking orders of magnitudes longer than on Linux due to use of string concatenation rather than the join idiom (from ~12 seconds spent on string concatenation to effectively zero). Tim Delaney -- http://mail.python.org/mailman/listinfo/python-list
Re: String manipulation in python..NEED HELP!!!!
On Tue, 11 Dec 2012 16:39:27 +, duncan smith wrote: [snip] alpha = ABCDEFGHIJKLMNOPQRSTUVWXYZ key = XPMGTDHLYONZBWEARKJUFSCIQV mapping = {} for i, ch in enumerate(alpha): mapping[ch] = key[i] mapping = dict(zip(alpha, key)) -- To email me, substitute nowhere-spamcop, invalid-net. -- http://mail.python.org/mailman/listinfo/python-list
String manipulation in python..NEED HELP!!!!
I need help with a program i am doing. it is a cryptography program. i am given a regular alphabet and a key. i need to use the user input and use the regular alphabet and use the corresponding letter in the key and that becomes the new letter. i have the basic code but need help with how to mainpulate the string to do the encryption/decryption. please help here is my code so far: crypto.py Implements a simple substitution cypher alpha = ABCDEFGHIJKLMNOPQRSTUVWXYZ key = XPMGTDHLYONZBWEARKJUFSCIQV def main(): keepGoing = True while keepGoing: response = menu() if response == 1: plain = raw_input(text to be encoded: ) print encode(plain) elif response == 2: coded = raw_input(code to be decyphered: ) print decode(coded) elif response == 0: print Thanks for doing secret spy stuff with me. keepGoing = False else: print I don't know what you want to do... i really need help on how to encrypt it im not sure how to go about doing that please help. -- http://mail.python.org/mailman/listinfo/python-list
Re: String manipulation in python..NEED HELP!!!!
In d6779e35-32b8-417a-abf9-72454573b...@googlegroups.com qbai...@ihets.org writes: crypto.py Implements a simple substitution cypher alpha = ABCDEFGHIJKLMNOPQRSTUVWXYZ key = XPMGTDHLYONZBWEARKJUFSCIQV def main(): keepGoing = True while keepGoing: response = menu() if response == 1: plain = raw_input(text to be encoded: ) print encode(plain) elif response == 2: coded = raw_input(code to be decyphered: ) print decode(coded) elif response == 0: print Thanks for doing secret spy stuff with me. keepGoing = False else: print I don't know what you want to do... i really need help on how to encrypt it im not sure how to go about doing that please help. def encode(plain): '''Return a substituted version of the plain text.''' encoded = '' for ch in plain: encoded += key[alpha.index(ch)] return encoded -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, The Gashlycrumb Tinies -- http://mail.python.org/mailman/listinfo/python-list
Re: String manipulation in python..NEED HELP!!!!
2012/12/10 qbai...@ihets.org: I need help with a program i am doing. it is a cryptography program. i am given a regular alphabet and a key. i need to use the user input and use the regular alphabet and use the corresponding letter in the key and that becomes the new letter. i have the basic code but need help with how to mainpulate the string to do the encryption/decryption. please help here is my code so far: crypto.py Implements a simple substitution cypher alpha = ABCDEFGHIJKLMNOPQRSTUVWXYZ key = XPMGTDHLYONZBWEARKJUFSCIQV def main(): keepGoing = True while keepGoing: response = menu() if response == 1: plain = raw_input(text to be encoded: ) print encode(plain) elif response == 2: coded = raw_input(code to be decyphered: ) print decode(coded) elif response == 0: print Thanks for doing secret spy stuff with me. keepGoing = False else: print I don't know what you want to do... i really need help on how to encrypt it im not sure how to go about doing that please help. -- http://mail.python.org/mailman/listinfo/python-list Hi, if I understand correctly, for the data shown in the code, you may probably use the translate method of the string (and the corresponding maketrans method); cf.: python 2.7 import string ABCDEF...VWXYZ.translate(string.maketrans(ABCDEFGHIJKLMNOPQRSTUVWXYZ, XPMGTDHLYONZBWEARKJUFSCIQV)) 'XPMGTD...SCIQV' python 3.2 ABCDEF...VWXYZ.translate(.maketrans(ABCDEFGHIJKLMNOPQRSTUVWXYZ, XPMGTDHLYONZBWEARKJUFSCIQV)) 'XPMGTD...SCIQV' hth, vbr -- http://mail.python.org/mailman/listinfo/python-list
Re: String manipulation in python..NEED HELP!!!!
On Tue, Dec 11, 2012 at 9:38 AM, qbai...@ihets.org wrote: I need help with a program i am doing. it is a cryptography program. i am given a regular alphabet and a key. i need to use the user input and use the regular alphabet and use the corresponding letter in the key and that becomes the new letter. i have the basic code but need help with how to mainpulate the string to do the encryption/decryption. please help A few starting tips. Firstly, I'm going to assume here that this is a homework question; you haven't said so, but it seems rather more likely than the alternative (that you're actually going to use such an incredibly low-grade cipher and an interactive prompt like this). Please be honest about this; it's okay to ask for help, but we're not here to do your homework for you. (We do NOT want to help you to get a certificate you don't merit, then get a job using that certificate, and then write code that we'll be staring at in our next jobs. There are already more than enough incompetent programmers in the world; I'd rather that you either learn the material for real, or if you can't, fail the course honestly. Sorry if that sounds harsh, but I'm sure you'd rather that I didn't have a certificate entitling me to drive a truck on roads near you/your kids, because I do not know how to drive one safely.) Secondly, putting NEED HELP in your subject line doesn't, in fact, help. :) It just makes you sound demanding. So! On to the actual problem. What you need to do is find the letter that corresponds to the one you have. Decryption is the same as encryption but with the alpha and key switched, so I recommend creating a function that accepts those two as arguments - something like this: clear = ABCDEFGHIJKLMNOPQRSTUVWXYZ key = XPMGTDHLYONZBWEARKJUFSCIQV def crypt(msg,from,to): # and put your code in here # To encrypt: encrypted = crypt(original, clear, key) # To decrypt: message = crypt(encrypted, key, clear) The details of the encryption you can put together from John's and/or vbr's responses, but make sure you understand the code yourself. For example: What will each of them do with any non-alphabetic characters? Suppose your original message is HELLO, WORLD! - what will happen to the comma, space, and exclamation mark? Be sure you know *why* this is how it is, too. If you run into trouble, post your non-working code and exactly how it's not working, and we'll try to help you understand why it's not working. :) In the meantime, here's a document that you may want to familiarize yourself with: http://www.catb.org/esr/faqs/smart-questions.html It's a great explanation of the how and, more importantly, the why of asking questions of volunteer geeks. All the best! ChrisA -- http://mail.python.org/mailman/listinfo/python-list