0

I have this method:

def encode(message, rails):
    result = ""
    dataRails = []
    activeRail = 0
    # allocate multi-dimensional array...seems inefficient
    for r in range(rails):
        dataRails.append([])

    # copy characters from message into one of the arrays, or .
    for m in message:
        count = 0
        # for each rail either give it the character or a .
        while count <= rails:
            print("Count: ", str(count), " Active Rail:", str(activeRail))
            if activeRail == count:
                dataRails[activeRail][count] = m
            else:
                dataRails[activeRail][count] = "."
            count += 1

        activeRail += 1
        if activeRail >= rails:
            activeRail = 0

    for r in dataRails:
        print(r)
    # ill calculate result once I get the above right    
    return result

Message is a string of text. Rails is a number. The encode method would be called like this:

encode('XOXOXOXOXOXOXOXOXO', 2)

In this case, I want to allocate my dataRails as 2 elements, each the same length as input message. I would replace elements in the arrays with either a letter from message or a ".".

But, it seems rather inefficient to use a loop to allocate dataRails.

When I run this, I get the following error:

dataRails[activeRail][count] = m
IndexError: list assignment index out of range

The print statement looks like valid index values:

Count:  0  Active Rail: 0

It feels like there are two problems with my multi-dimensional array. 1) allocation and 2) access.

If there is a duplicate question, please post a link. I searched through stackoverflow. Answers that seemed relevant relied on a library, which I did not want to use. I have just started learning python and want to learn the basics of python, not using libraries.

Are arrays really that difficult in python or did I make it more difficult than it needs to me?

Thanks Matt

Edit: Ultimately, if the message input was this "WE ARE DIS", the return of this method would be "WEERDSAI". I am working through a tutorial on exercism.io. I haven't completed the method yet. I am stuck on the array I defined.

5
  • Can you show some desired inputs/outputs? For your multidimensional arrays, you can do this: dataRails = [[] for _ in range(rails)] Commented Mar 1, 2018 at 18:19
  • 1
    Well, for starters you're dealing with lists. Arrays are a separate data type that can't change size dynamically. However, even with lists you can't access elements beyond the list size, so you probably change the assignment to ... .apppend(m) Commented Mar 1, 2018 at 18:24
  • Why is that the expected output? You've used while and counters instead of for loops, multiple checks on one variable, and skipped string slices altogether. A simple Caesar cipher would be much shorter, so I'm not sure what you're trying to accomplish; the example has too many duplicate letters to be intuitive. Commented Mar 1, 2018 at 18:28
  • @Prune the purpose is to implement a rail fence cipher, not a Caesar cipher. A Caesar cipher may be simpler, but that's not the exercise. I did reduce the example. Hopefully that helps. However, the purpose of this post isn't to solve the problem for me. The purpose is to help me fix my array manipulation. Commented Mar 1, 2018 at 19:53
  • Got it; the cypher name was enough of a clue. All you need is an array of strings, one string per rail. The above append comments should get you to the next set of problems. :-) Commented Mar 1, 2018 at 20:06

1 Answer 1

1

I suggest that you keep a list of strings, rather than a multi-dimensional list. Just a couple of simple changes:

# Initialize empty rails
dataRails = [""] * rails
...
        if activeRail == count:
            dataRails[activeRail] += m
        else:
            dataRails[activeRail] += '.'

I tested with "We Are Dis":

W...A... ...s...
.e...r...D..
.. ...e...i.

Now you can deal with removing non-letters and properly reversing direction. Is that good for now?

Sign up to request clarification or add additional context in comments.

1 Comment

yes, that is clear. I was over complicating the data structure. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.