List problems in C code ported to Python

B

Bengt Richter

OK, here's the Python code and the corresponding C code:

def init_mach():
import string
#setup rotor data
i=1
j=0
for j in j<26, j+1:
I urge you to explore interactively. And do it line by line
(and even expression by expression when you get mystifying results) until you
are more familiar with the language.

E.g., what do you think the values of j will be here? ...
True 1

Surprised?
(True, 2)

Hm, surprised at the 2 here?
(True, 1)

IOW, your 'for' line was equivalent to iterating through a length-2 tuple
for j in (True, 1): # since if j is 0, j<26 is True, and j+1 is 1
What you probably wanted was j starting with 0 and ending with 25, which you get by
...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

(The comma at the end of the print statement is to get a space before
the next output instead of a newline).
data[4],[j] = (ref_rotor[j] - 'A'+26) % 26
Had you tried an interactive experiment, you would have had some real questions to ask ;-)
... data[4],[j] = (ref_rotor[j] - 'A'+26) % 26
...
Traceback (most recent call last):
File "<stdin>", line 2, in ?
NameError: name 'ref_rotor' is not defined

Ok, fix that with something ... data[4],[j] = (ref_rotor[j] - 'A'+26) % 26
...
Traceback (most recent call last):
File "<stdin>", line 2, in ?
TypeError: unsupported operand type(s) for -: 'str' and 'str'

You might have had to ask at this point what that meant, but you could get closer by
trying the expressions and their terms:
>>> (ref_rotor[j] - 'A'+26) % 26
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Pare it down
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Check pieces
'A'

Substitute to make painfully clear
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for -: 'str' and 'str'

What is 'A' ? <type 'str'>

Hm, characters are strings, not char types that are directly type compatible
with int. It takes a conversion. To get from single-char string to its integer code
and back:
'A'

Another experiment:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: ord() expected a character, but string of length 2 found


Now you are in a position to use ord, and get a little further:
... data[4],[j] = (ord(ref_rotor[j]) - ord('A')+26) % 26
...
Traceback (most recent call last):
File "<stdin>", line 2, in ?
TypeError: unpack non-sequence

Was the right hand side a problem?
>>> j 0
>>> (ord(ref_rotor[j]) - ord('A')+26) % 26
0

Apparently not, so what it was trying to do was
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unpack non-sequence

What was it trying to unpack? And why?
Well, you might have known if you had been through the tutorial, but at least
you would have had a specific question about a specific error at this point.

Note: Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unpack non-sequence

I.e., when you have commas (or even one) on the left hand side of an assignment,
that is asking python to unpack a sequence on the right hand side and assign to
corresponding items on the left. So e.g.,
1
You can unpack any sequence of the same length, and strings happen to be sequences:
'Y'

Ok, enough of that. The comma was obviously a problem so, try it without:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'data' is not defined

I could go on, but IMO if you expect help, you should do your part, and at least
post code that you have tried and which compiles or runs up to the point where
it shows an error that you need help with.

And show a verbatim copy/paste from your interactive session, not uncompilable
and obviously totally untested stuff.
for i in i<4, i+1:
step[i-1] = step_data[order[i-1]]
for j in j<26, j+1:
data,[j] = (rotor[order[i-1]],[j]-'A'+26)%26
data[8-i],[data,[j]] = j


void
init_mach( void )
{
int i, j;
int ds;
int u, v;

/* setup rotor data */
for (j=0;j<26;j++)
data[4][j] = ((int)ref_rotor[j]-'A'+26)%26;

for (i=1;i<4;i++)
{
step[i-1] = step_data[order[i-1]];
for (j=0;j<26;j++)
{
data[j] = ((int)(rotor[order[i-1]][j])-'A' + 26) % 26;
data[8-i][data[j]] = j;
}
}

Now, do I need to start boning up on lists and how to use them or am I
missing the bigger picture?? Again, for the complete code see
http://home.earthlink.net/~lvraab. I'm not asking you to do it for me,
just some pointers on going about this.


You are obviously not yet familiar with python, but a few hours with the
introduction and tutorials should help a lot. Then post something that
compiles. Or a snippet that does something you don't understand.

This is about all I have patience for this time. I really have other stuff to do.

Regards,
Bengt Richter
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top