【问题标题】:Python- Encoding two lettersPython-编码两个字母
【发布时间】:2013-10-29 02:36:43
【问题描述】:

我正在尝试创建一个 python playfair 密码,但在此过程中遇到了一些问题。我有一个五乘五的表格,其中包含以下信息:

 [['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']]

我应该一次加密两个字母。当给定输入加密 (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)

【问题讨论】:

    标签: python


    【解决方案1】:

    您可以在 python 中使用 ord() 获取 char 的 ASCII 值。

    EX

    s='A'

    change_s=chr(ord(s)+2)

    所以结果是 change_s = 'C'

    【讨论】:

      【解决方案2】:

      您需要保存 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]
      

      最后一步可以一步完成,但我试图澄清以确保您理解我在做什么,并在我误解时纠正我

      【讨论】:

      • 您好,感谢您及时回复,但我似乎遇到了一些麻烦。当我尝试运行代码时,我收到以下错误:“TypeError: list indices must be integers, not list”有什么办法可以解决这个问题。
      • 我看到了我创建的问题。我没有在严格的代码中思考。您将希望最后四行中的每一行仅在两行中使这更简单code new_a = table[index_b[0]][index_a[1]]new_b = table[index_a[0]][index_b[1]] 我可能已经混淆了行/列,所以您可能需要重新排序,但这应该可以解决正在发生的 typeError因为 new_a 和 new_b 被提供索引列表而不是两个索引
      • 再次感谢您的回复,但我似乎仍然无法使其正常工作。也许我在解释任务方面做得很差。这就是程序应该如何运行:youtube.com/watch?v=quKhvu2tPy8 从 1:00 开始,解释规则。我试图使用您提出的想法来创建其余规则,但我无法这样做。如果您能够进一步帮助我,那就太棒了,但如果您不这样做,我理解。无论如何,谢谢你。
      • 我刚刚运行它,它可以工作。我认为您可能遇到输入问题,您是否收到此错误? Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError:
      • new_a = tablez[index_b[0]][index_a[1]] new_b = tablez[index_a[0]][index_b[1]] 实际上是正确的。我猜你遇到的问题是你试图传递一个名为 A 的变量而不是传递字符串 'A'
      【解决方案3】:

      这很有趣...谢谢。

      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')
      

      【讨论】:

        【解决方案4】:

        这是你要找的吗:

        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')
        

        【讨论】:

        • 我认为他也需要 D——广场的另一个角落。
        【解决方案5】:

        可能是这个?

        >>> 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')
        

        【讨论】:

          猜你喜欢
          • 2011-11-11
          • 2017-02-08
          • 1970-01-01
          • 2017-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多