Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > String manipulation in python..NEED HELP!!!!

Reply
Thread Tools

String manipulation in python..NEED HELP!!!!

 
 
qbailey@ihets.org
Guest
Posts: n/a
 
      12-10-2012
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 theregular 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.
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      12-10-2012
In <(E-Mail Removed)> http://www.velocityreviews.com/forums/(E-Mail Removed) 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
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Vlastimil Brom
Guest
Posts: n/a
 
      12-10-2012
2012/12/10 <(E-Mail Removed)>:
> I need help with a program i am doing. it is a cryptography program. i amgiven 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 doingthat 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("ABCDE FGHIJKLMNOPQRSTUVWXYZ", "XPMGTDHLYONZBWEARKJUFSCIQV"))

'XPMGTD...SCIQV'
>>>



python 3.2
>>> "ABCDEF...VWXYZ".translate("".maketrans("ABCDEFGHI JKLMNOPQRSTUVWXYZ", "XPMGTDHLYONZBWEARKJUFSCIQV"))

'XPMGTD...SCIQV'
>>>


hth,
vbr
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-10-2012
On Tue, Dec 11, 2012 at 9:38 AM, <(E-Mail Removed)> wrote:
> I need help with a program i am doing. it is a cryptography program. i amgiven 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
 
Reply With Quote
 
duncan smith
Guest
Posts: n/a
 
      12-11-2012
On 10/12/12 22:38, (E-Mail Removed) 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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      12-11-2012
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

 
Reply With Quote
 
Ross Ridge
Guest
Posts: n/a
 
      12-11-2012
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 <(E-Mail Removed)> 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] (E-Mail Removed)
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
 
Reply With Quote
 
Tim Delaney
Guest
Posts: n/a
 
      12-11-2012
On 12 December 2012 07:52, Ross Ridge <(E-Mail Removed)> 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 <(E-Mail Removed)> 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...00000000000000

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

 
Reply With Quote
 
Peter Pearson
Guest
Posts: n/a
 
      12-11-2012
On Tue, 11 Dec 2012 16:39:27 +0000, 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation Chris ASP .Net 1 05-24-2004 02:19 PM
Advanced String Manipulation (C#) Aaron ASP .Net 2 01-02-2004 06:50 PM
String Manipulation Aaron ASP .Net 3 12-31-2003 03:39 PM
Perl string manipulation jared in ecs Perl 2 10-22-2003 05:36 PM
String Array Manipulation Problem Garfield ASP .Net 5 08-27-2003 10:07 PM



Advertisments