Assistance required: Beginner Python Programming

This is the place for queries that don't fit in any of the other categories.

Assistance required: Beginner Python Programming

Postby Rippa18 » Sun Mar 31, 2013 10:20 am

i am writing a encrypting program which process is as follows
- Spaces in a message replaced by X's
-Each word in the original message is reversed
- Consecutive sequences of words, called “blocks”, have the order of the words
reversed. The size of these blocks is a parameter to the encryption function and acts
as the “key”. For example, if the message was 'THE PRICE OF FREEDOM IS
ETERNAL VIGILENCE' and the block size is four then each block of four words
will be reversed, producing 'FREEDOM OF PRICE THE VIGILENCE ETERNAL
IS' (ignoring the other two steps above). Notice that the last block only has three
words in this case, but it is still reversed nonetheless.

now i've been able to replace the spaces and reverse everything, but the last step has stumped me.

the following code is what i have so far.

Code: Select all
def encrypt (words, block):
    words = words [::-1]
    midpoint = len(words)/block
    first_half = words[0:midpoint]
    second_half = words[midpoint:]
    words = first_half + second_half
    words = words.replace(' ', 'X')
    return words

def decrypt (wordsde, block):
    wordsde = wordsde[::-1]
    midpoint = len(wordsde) / block
    first_half = wordsde[:midpoint]
    second_half = wordsde[midpoint:]
    wordsde = first_half + second_half
    wordsde = wordsde.replace('X', ' ')
    wordsde = wordsde.strip()
    return wordsde


but everytime it can't read the block or something:

Code: Select all
File "__main__", line 18, in __main__
Failed example:
    encrypt('WHO WATCHES THE WATCHERS', 2) # Test 4
Expected:
    'SEHCTAWXOHWXSREHCTAWXEHT'
Got:
    'SREHCTAWXEHTXSEHCTAWXOHW'
Trying:
    encrypt('PARANOIA IS OUR PROFESSION', 3) # Test 5
Expecting:
    'RUOXSIXAIONARAPXNOISSEFORP'
**********************************************************************
File "__main__", line 22, in __main__
Failed example:
    encrypt('PARANOIA IS OUR PROFESSION', 3) # Test 5
Expected:
    'RUOXSIXAIONARAPXNOISSEFORP'
Got:
    'NOISSEFORPXRUOXSIXAIONARAP'
Trying:
    encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4) # Test 6
Expecting:
    'MODEERFXFOXECIRPXEHTXECNELIGIVXLANRETEXSI'
**********************************************************************
File "__main__", line 26, in __main__
Failed example:
    encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4) # Test 6
Expected:
    'MODEERFXFOXECIRPXEHTXECNELIGIVXLANRETEXSI'
Got:
    'ECNELIGIVXLANRETEXSIXMODEERFXFOXECIRPXEHT'


PLEASE HELP
Rippa18
 
Posts: 1
Joined: Sun Mar 31, 2013 10:09 am

Re: Assistance required: Beginner Python Programming

Postby Mekire » Sun Mar 31, 2013 3:12 pm

First split the sentence into separate words in a list using split.
Then iterate through the list with a for loop; reverse each word; and insert it in a new list appropriately.
The insertion index point will be something like:
Code: Select all
(i//size)*size
Then just turn the list back into a string using join.
(that last step can also be used to add the "X"s for spaces.)

I'm trying to avoid just giving you a solution so give this a try first.

-Mek
User avatar
Mekire
 
Posts: 981
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests