这是表格http://en.wikipedia.org/wiki/File:Vigen%C3%A8re_square_shading.svg
如何在Python中实现此表?这是什么方式?
关于如何检查例如Row:L& G栏成为R欢迎。
答案 0 :(得分:4)
>>> letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> mat = [(letters * 2)[i:i+26] for i in range(26)]
>>> print '\n'.join(mat)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG
IJKLMNOPQRSTUVWXYZABCDEFGH
JKLMNOPQRSTUVWXYZABCDEFGHI
KLMNOPQRSTUVWXYZABCDEFGHIJ
LMNOPQRSTUVWXYZABCDEFGHIJK
MNOPQRSTUVWXYZABCDEFGHIJKL
NOPQRSTUVWXYZABCDEFGHIJKLM
OPQRSTUVWXYZABCDEFGHIJKLMN
PQRSTUVWXYZABCDEFGHIJKLMNO
QRSTUVWXYZABCDEFGHIJKLMNOP
RSTUVWXYZABCDEFGHIJKLMNOPQ
STUVWXYZABCDEFGHIJKLMNOPQR
TUVWXYZABCDEFGHIJKLMNOPQRS
UVWXYZABCDEFGHIJKLMNOPQRST
VWXYZABCDEFGHIJKLMNOPQRSTU
WXYZABCDEFGHIJKLMNOPQRSTUV
XYZABCDEFGHIJKLMNOPQRSTUVW
YZABCDEFGHIJKLMNOPQRSTUVWX
ZABCDEFGHIJKLMNOPQRSTUVWXY
或者,您可以动态计算元素:
def get_elem(row, col):
return 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[(ord(row) + ord(col)) % 26]
print get_elem('L', 'G')
答案 1 :(得分:1)
我喜欢在可能的情况下使用标准库,以避免滚动我自己的代码并无意中遗漏(比如意外错位或忘记了一封信。)
import string
import collections
def vigsquare(printable=False):
'''
Returns a string like a vigenere square,
printable joins each row with a newline so it's literally square
printable=False (defaul) joins without newlines for easier
searching by row and column index
'''
alpha = string.ascii_uppercase
rotater = collections.deque(alpha)
vigsquare_list = []
for i in xrange(26):
vigsquare_list.append(''.join(rotater))
rotater.rotate(-1)
if printable:
return '\n'.join(vigsquare_list)
else:
return ''.join(vigsquare_list)
和
def vigenere(row, column):
'''
Return a character from a vigenere square by
row and column letter.
vigenere('L', 'G') returns 'R'
'''
alpha = string.ascii_uppercase
rowindex = alpha.find(row)
columnindex = alpha.find(column)
return vigsquare()[rowindex*26 + columnindex]
print vigsquare(printable=True)
vigenere('L', 'G')
给你:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG
IJKLMNOPQRSTUVWXYZABCDEFGH
JKLMNOPQRSTUVWXYZABCDEFGHI
KLMNOPQRSTUVWXYZABCDEFGHIJ
LMNOPQRSTUVWXYZABCDEFGHIJK
MNOPQRSTUVWXYZABCDEFGHIJKL
NOPQRSTUVWXYZABCDEFGHIJKLM
OPQRSTUVWXYZABCDEFGHIJKLMN
PQRSTUVWXYZABCDEFGHIJKLMNO
QRSTUVWXYZABCDEFGHIJKLMNOP
RSTUVWXYZABCDEFGHIJKLMNOPQ
STUVWXYZABCDEFGHIJKLMNOPQR
TUVWXYZABCDEFGHIJKLMNOPQRS
UVWXYZABCDEFGHIJKLMNOPQRST
VWXYZABCDEFGHIJKLMNOPQRSTU
WXYZABCDEFGHIJKLMNOPQRSTUV
XYZABCDEFGHIJKLMNOPQRSTUVW
YZABCDEFGHIJKLMNOPQRSTUVWX
ZABCDEFGHIJKLMNOPQRSTUVWXY
和
'R'
这里有另一个看起来很有趣的解决方案,但我不相信它,因为我不理解它,所以我会对我的代码和其他代码进行单元测试,看看它是否可靠。
def vig_2(row, col):
return string.ascii_uppercase[(ord(row) + ord(col)) % 26]
比较两种方法的单位测试。第二个(从NPE借来,我现在欠他一个upvote)如果它是正确的话可能会更有效率。:
import unittest
class VigTestCase(unittest.TestCase):
def test_vigenere(self):
self.assertEqual(vigenere('L', 'G'), 'R')
def test_vigsquare(self):
self.assertEqual(vigsquare(printable=False), 'ABCDEFGHIJKLMNOPQRSTUVWXYZBCDEFGHIJKLMNOPQRSTUVWXYZACDEFGHIJKLMNOPQRSTUVWXYZABDEFGHIJKLMNOPQRSTUVWXYZABCEFGHIJKLMNOPQRSTUVWXYZABCDFGHIJKLMNOPQRSTUVWXYZABCDEGHIJKLMNOPQRSTUVWXYZABCDEFHIJKLMNOPQRSTUVWXYZABCDEFGIJKLMNOPQRSTUVWXYZABCDEFGHJKLMNOPQRSTUVWXYZABCDEFGHIKLMNOPQRSTUVWXYZABCDEFGHIJLMNOPQRSTUVWXYZABCDEFGHIJKMNOPQRSTUVWXYZABCDEFGHIJKLNOPQRSTUVWXYZABCDEFGHIJKLMOPQRSTUVWXYZABCDEFGHIJKLMNPQRSTUVWXYZABCDEFGHIJKLMNOQRSTUVWXYZABCDEFGHIJKLMNOPRSTUVWXYZABCDEFGHIJKLMNOPQSTUVWXYZABCDEFGHIJKLMNOPQRTUVWXYZABCDEFGHIJKLMNOPQRSUVWXYZABCDEFGHIJKLMNOPQRSTVWXYZABCDEFGHIJKLMNOPQRSTUWXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVWYZABCDEFGHIJKLMNOPQRSTUVWXZABCDEFGHIJKLMNOPQRSTUVWXY')
def test_vig2(self):
for i in string.ascii_uppercase:
for j in string.ascii_uppercase:
self.assertEqual(vig_2(i, j), vigenere(i, j))
unittest.main()
...
----------------------------------------------------------------------
Ran 3 tests in 0.038s
OK
所以看起来NPE有一个非常好的解决方案!