0

Okay, so what I want to do, is to take the following code, and make it so that way it keeps continuning for all of the word.

keycode = raw_input()
matrix = [0, 0, 0, 0]
for x in range(0, len(keycode)):
    y = ord(keycode[x])
    if x > 3 and x <= 7:
        x = x -4
    if x > 7 and x <= 11:
        x = x -8
    if x > 11 and x <= 15:
        x = x -12
    if x > 15 and x <= 19:
        x = x -16
    matrix[x] = matrix[x] + y

The part I want to continue is the if statements... I'm thinking maybe using the len(keycode) somehow, but I'm a bit confused on where to go from here. Any help would be appreciated.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by hondros
0

What about something like this (making use of integer division):

keycode = raw_input()
matrix = [0, 0, 0, 0]
for x in range(0, len(keycode)):
    y = ord(keycode[x])
    factor = x / 4
    x = x - (4 * factor)
    matrix[x] = matrix[x] + y

Not sure if that's what you were shooting for?

0

Hey, that'd work. But I just figured it out after I posted. Here's what I used:

for x in range(0, len(keycode)):
	y = ord(keycode[x])
	while x > 3:
		x = x -4
	matrix[x] = matrix[x] + y

Then again, yours is probably more efficient. But I tested a keycode of a length of 1,024 charecters, and it finished in about the same time. Thanks for the suggestion though :D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.