0

Python playfair 暗号を作成しようとしていますが、途中でいくつかの問題が発生しています。次の情報を含む 5 x 5 のテーブルがあります。

 [['A', 'B', 'C', 'D', 'E'],
 ['F', 'G', 'H', 'I', 'Y'],
 ['K', 'L', 'M', 'N', 'O'],
 ['P', 'Q', 'R', 'S', 'T'],
 ['U', 'V', 'W', 'X', 'Z']]

一度に 2 文字を暗号化することになっています。入力暗号化 (B, N) が与えられると、結果の出力は DL になります。入力の最初の文字は、B と同じ行の文字を返す必要がありますが、列 N があります。誰かがそれを達成する方法を説明してくれることを望んでいました。

ここのフレンドリーなユーザーの助けを借りて、コードの一部は次のようになります。

def find_index(letter, table):
    for i,li in enumerate(table):
        try:
            j=li.index(letter)
            return i,j
        except ValueError:
            pass    

    raise ValueError("'{}' not in matrix".format(letter))
print "Row:Column"
print find_index('I', table)  


def encode(a, b):
    if a == b:
        print "ERROR: letters to encode_pair must be distinct"
    print find_index (a,table)
    print find_index (b, table)
4

5 に答える 5

0

find_index (a,table) と find_index (a,table) からの戻り値を保存する必要があります。私が正しく理解している場合、これは実装する必要がある種類のコードです。

def encode(a, b):
    if a == b:
        print "ERROR: letters to encode_pair must be distinct"
    print find_index (a,table)
    print find_index (b, table) 
    index_a= find_index (a,table)
    index_b = find_index (a,table)
    new_a_index = [index_a[0], index_b[1]]
    new_b_index = [index_b[0], index_a[1]]
    new_a = table[new_a_index]
    new_b = table[new_b_index]

最後のステップは 1 つのステップで行うことができますが、私が行っていることを確実に理解してもらい、誤解があれば訂正できるように、明確にしようとしていました。

于 2013-10-29T02:54:54.350 に答える
0

楽しかった…ありがとう。

In [2]:



class Encoder(object):
    m = [['A', 'B', 'C', 'D', 'E'],
     ['F', 'G', 'H', 'I', 'Y'],
     ['K', 'L', 'M', 'N', 'O'],
     ['P', 'Q', 'R', 'S', 'T'],
     ['U', 'V', 'W', 'X', 'Z']]    

    def encode(self, first, second):
        first_row_idx, first_col_idx = self.get_rowcol(first)
        second_row_idx, second_col_idx = self.get_rowcol(second)        
        encoded_first = self.m[first_row_idx][second_col_idx]
        encoded_second = self.m[second_row_idx][first_col_idx]
        return encoded_first, encoded_second

    def get_rowcol(self, letter):
        letter = letter.upper()
        for row_idx, row in enumerate(self.m):
            for col_idx, col_letter in enumerate(row):
                if col_letter == letter:
                    return row_idx, col_idx
        raise ValueError("({}) not found in matrix!".format(letter))


e = Encoder()
e.encode("B", "N")

Out[2]:
('D', 'L')
于 2013-10-29T02:55:39.477 に答える
0

これかも?

>>> from itertools import chain
>>> cl=[['A', 'B', 'C', 'D', 'E'],
 ['F', 'G', 'H', 'I', 'Y'],
 ['K', 'L', 'M', 'N', 'O'],
 ['P', 'Q', 'R', 'S', 'T'],
 ['U', 'V', 'W', 'X', 'Z']]
>>> M = list(chain(*cl))
>>> N = len(cl[0])
>>> def e(a, b):
        ia,ib = M.index(a), M.index(b)
        ar,ac = ia / N, ia % N
        br,bc = ib / N, ib % N
        return M[ar*N + bc], M[br*N + ac]

>>> e('B','N')
# ('D', 'L')
于 2013-10-29T02:57:41.673 に答える
0

これはあなたが探しているものですか:

In [25]: table = [['A', 'B', 'C', 'D', 'E'],
 ['F', 'G', 'H', 'I', 'Y'],
 ['K', 'L', 'M', 'N', 'O'],
 ['P', 'Q', 'R', 'S', 'T'],
 ['U', 'V', 'W', 'X', 'Z']]

In [26]: def encrypt(B, N):
    b = [L.index(B) for L in table if B in L][0]
    answer = [L for L in table if N in L][0][b]
    b = [L.index(N) for L in table if N in L][0]
    answer = answer,[L for L in table if B in L][0][b]
    return answer

In [27]: encrypt("B", "N")
Out[27]: ('L', 'D')
于 2013-10-29T02:44:41.487 に答える