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

Q

qbailey

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.
 
J

John Gordon

In said:
""" 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
 
V

Vlastimil Brom

2012/12/10 said:
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.


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

python 3.2
hth,
vbr
 
C

Chris Angelico

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
 
D

duncan smith

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.
mapping[ch] = key

''.join(mapping[ch] for ch in "ACE") 'XMT'
''.join(mapping[ch] for ch in "WORD") 'CEKG'


Duncan
 
T

Terry Reedy

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.
 
R

Ross Ridge

John said:
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 said:
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
 
T

Tim Delaney

John said:
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 said:
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#SECTION0001210000000000000000

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top