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

Discussion in 'Python' started by qbailey@ihets.org, Dec 10, 2012.

  1. Guest

    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.
    , Dec 10, 2012
    #1
    1. Advertising

  2. John Gordon Guest

    In <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Dec 10, 2012
    #2
    1. Advertising

  3. 2012/12/10 <>:
    > 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("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "XPMGTDHLYONZBWEARKJUFSCIQV"))

    'XPMGTD...SCIQV'
    >>>



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

    'XPMGTD...SCIQV'
    >>>


    hth,
    vbr
    Vlastimil Brom, Dec 10, 2012
    #3
  4. On Tue, Dec 11, 2012 at 9:38 AM, <> 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
    Chris Angelico, Dec 10, 2012
    #4
  5. duncan smith Guest

    On 10/12/12 22:38, 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


    >>> ''.join(mapping[ch] for ch in "ACE")

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

    'CEKG'
    >>>



    Duncan
    duncan smith, Dec 11, 2012
    #5
  6. Terry Reedy Guest

    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
    Terry Reedy, Dec 11, 2012
    #6
  7. Ross Ridge Guest

    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 <> 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]
    -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
    db //
    Ross Ridge, Dec 11, 2012
    #7
  8. Tim Delaney Guest

    On 12 December 2012 07:52, Ross Ridge <> 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 <> 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#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
    Tim Delaney, Dec 11, 2012
    #8
  9. 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


    mapping = dict(zip(alpha, key))

    --
    To email me, substitute nowhere->spamcop, invalid->net.
    Peter Pearson, Dec 11, 2012
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dd711
    Replies:
    6
    Views:
    873
    Alex Hunsley
    Oct 1, 2004
  2. Replies:
    3
    Views:
    399
  3. Rex
    Replies:
    2
    Views:
    285
  4. WaterWalk

    Need help on string manipulation

    WaterWalk, Mar 27, 2006, in forum: C Programming
    Replies:
    4
    Views:
    331
    Ben Bacarisse
    Mar 29, 2006
  5. lorlarz
    Replies:
    8
    Views:
    100
Loading...

Share This Page