I am trying to write a program to do the hill cipher technique of encryption.

I am stuck trying to break the large array of numbers representing letters etc. into multiple smaller arrays with one column and two rows

does anyone know how this could be done??

Here is the relevant part of my code

from __future__ import division
from pylab import *

# Converts an array of integers into alphabetical characters.
# Input: Array of integers mod 28.
# Output: A section of text of any length, composed only of
# lower case letters, spaces or full stops.
# Array can be of any length, but all entries must be
# integers in the range 0 to 27.
def intArrayToText(intArray):
alpha = 'abcdefghijklmnopqrstuvwxyz .'
toChar = dict([(i , alpha) for i in range(len(alpha))])

chars = map(lambda i: toChar, intArray)
text = ''.join(chars)

return text


# Converts a section of text into an array of integers.
# Input: Prompts the user to enter some text at run-time.
# Assumes the text is all on one line, and composed only of
# lower case letters spaces or full stops.
# Output: Array of integers modulo 28.
def inputTextToIntArray():
alpha = 'abcdefghijklmnopqrstuvwxyz .'
toInt = dict([(alpha, i) for i in range(len(alpha))])

text = raw_input("=> ")

textInts = map(lambda c: toInt[c], text)
intArray = array(textInts)

return intArray

print "Please enter some text"
msg = inputTextToIntArray()
print #a blank line
text = intArrayToText(msg)
print "Your text converted to integers in array is: ", msg

#separating msg into smaller fragments

for i in msg:
block = array([[i],[i]])
print i
print block

It seems that the indenting doesnt come up, im sure you know where to put appropriate indents

Recommended Answers

All 6 Replies

You need to use code tags when posting code in this forum, like so:
[code=python] # Your code in here

[/code]
That will preserve your indents so that your code is readable, and will entice other forum members to actually read your post.

Here's a way to split up a list using slicing:

>>> a = [1,2,3,4,5,6,7,8,9]
>>> b = [ a[:3], a[3:6], a[6:] ]
>>> b
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>>

Is this what you're asking about? It helps us to be more specific in your questions and also to follow all forum guidelines so that we can better answer your questions.

oh ok well here is the code with correct indenting

what i need the program to do is, transform a row array of numbers with a size depending on how much text the user inputs, to be turned into multiple 2x1 arrays containing the numbers.

so like if the user inputs something like "hello" this would get transformed into an array of numbers, lets say [2 3 4 5]. i need the program to take this array and change it so it becomes

[[2] [[4]
[3]] [5]] , where the 2 and 3 form one array and 4 and 5 form the other.

it is assumed the user will input a even number of letters.
it needs to be a loop which will stop when all the numbers of the original array have been transformed into many 2x1 arrays

from __future__ import division
from pylab import *

# Converts an array of integers into alphabetical characters.
#  Input: Array of integers mod 28.
#  Output:  A section of text of any length, composed only of 
#       lower case letters, spaces or full stops.
#   Array can be of any length, but all entries must be
#   integers in the range 0 to 27.
def intArrayToText(intArray):
    alpha = 'abcdefghijklmnopqrstuvwxyz .'
    toChar = dict([(i , alpha[i]) for i in range(len(alpha))])

    chars = map(lambda i: toChar[i], intArray)
    text = ''.join(chars)

    return text


#  Converts a section of text into an array of integers.
#   Input: Prompts the user to enter some text at run-time.
#       Assumes the text is all on one line, and composed only of 
#       lower case letters spaces or full stops. 
#   Output: Array of integers modulo 28.
def inputTextToIntArray():
    alpha = 'abcdefghijklmnopqrstuvwxyz .'
    toInt = dict([(alpha[i], i) for i in range(len(alpha))])

    text = raw_input("=> ")

    textInts = map(lambda c: toInt[c], text)
    intArray = array(textInts)
    
    return intArray

print "Please enter some text"
msg = inputTextToIntArray()
print #a blank line
text = intArrayToText(msg)
print "Your text converted to integers in array is: ", msg

#separating msg into smaller fragments

for i in msg:
    block = array([[msg[i]], [msg[0]]])
    print i
    print block

help plz....

Is there any reason that you're using an array instead of just the built-in list ? Does your code work or is it broken?

im using arrays because the "hill cipher" ie the thing im trying to program to work uses matrices and yes what i have works...just not the way i want it to. the only modification i need is with the last part of the code, after where i put the comment

#separating msg into smaller fragments

i have been working on it and this is my current code (just the end part after the separation comment)

#separating msg into smaller fragments
block = zeros(2)
t = size(msg)
j = 0
while j < t:
    for i in msg:
        block[0] = i
        block[1] = i
        j = j+1
        print block

out put gives me this:
Please enter some text
=> fish

Your text converted to integers in array is: [ 5 8 18 7]
[ 5. 5.]
[ 8. 8.]
[ 18. 18.]
[ 7. 7.]

but what i want it to do is give me this....
[5. 8.]
[18. 7.]

Here's a clue:

>>> for x in xrange(0, len(msg), 2):
...     print msg[x], msg[ x + 1 ]
...     
 5 8
18 7
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.