Skip to main content
1 / 4
peta
  • 13
  • 3

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indicesというエラーの修正案を募集したいです

エラーメッセージ

IndexError                                Traceback (most recent call last)
<ipython-input-49-7b8715759b73> in <cell line: 73>()
     78       rot3_perm = datalist[k]
     79       enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
---> 80       cipher_tentative = enigma.get_text(plane)
     81       decrypt = enigma.get_text(cipher_tentative)
     82       if 'ABC' in decrypt == True:

2 frames
<ipython-input-49-7b8715759b73> in get_text(self, text)
     53                 if cnt2 % self.size == 0:
     54                     cnt3 += 1
---> 55                 m = self.get_matrix(cnt1, cnt2, cnt3)
     56                 vec = (vec * m).A[0]
     57                 for n, x in enumerate(vec):

<ipython-input-49-7b8715759b73> in get_matrix(self, cnt1, cnt2, cnt3)
     20         plugbord = np.mat([self.identity[n] for n in self.plug_perm])
     21         reflector = np.mat([self.identity[n] for n in self.ref_perm])
---> 22         rotor1 = np.mat([self.identity[n] for n in rot1_perm])
     23         rotor2 = np.mat([self.identity[n] for n in rot2_perm])
     24         rotor3 = np.mat([self.identity[n] for n in rot3_perm])

<ipython-input-49-7b8715759b73> in <listcomp>(.0)
     20         plugbord = np.mat([self.identity[n] for n in self.plug_perm])
     21         reflector = np.mat([self.identity[n] for n in self.ref_perm])
---> 22         rotor1 = np.mat([self.identity[n] for n in rot1_perm])
     23         rotor2 = np.mat([self.identity[n] for n in rot2_perm])
     24         rotor3 = np.mat([self.identity[n] for n in rot3_perm])

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

プログラム

import numpy as np
import random

class Enigma():
    def __init__(self, plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm):
        self.alphabet = alphabet
        self.char_dict = {x: n for n, x in enumerate(alphabet)}
        self.size = len(alphabet)
        self.identity = np.eye(self.size)
        self.plug_perm = plug_perm
        self.ref_perm = ref_perm
        self.plane = plane

    def get_matrix(self, cnt1, cnt2, cnt3):
        rot_rotor_perm = list(range(self.size))

        col_perm_mat = np.mat([self.identity[n - 1] for n in rot_rotor_perm])
        row_perm_mat = col_perm_mat.T

        plugbord = np.mat([self.identity[n] for n in self.plug_perm])
        reflector = np.mat([self.identity[n] for n in self.ref_perm])
        rotor1 = np.mat([self.identity[n] for n in rot1_perm])
        rotor2 = np.mat([self.identity[n] for n in rot2_perm])
        rotor3 = np.mat([self.identity[n] for n in rot3_perm])

        m = (plugbord *
             (row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1) *
             (row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2) *
             (row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3) *
             reflector *
             ((row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3)).I *
             ((row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2)).I *
             ((row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1)).I *
             plugbord)
        return m

    def get_text(self, text):
        after = ''
        tmp = ''
        cnt1 = 0
        cnt2 = 0
        cnt3 = 0
        for char in text:
            tmp = ''
            if char not in self.alphabet:
                tmp += char
            else:
                vec = [0] * self.size
                vec[self.char_dict[char]] = 1
                cnt1 += 1
                if cnt1 % self.size == 0:
                    cnt2 += 1
                if cnt2 % self.size == 0:
                    cnt3 += 1
                m = self.get_matrix(cnt1, cnt2, cnt3)
                vec = (vec * m).A[0]
                for n, x in enumerate(vec):
                    if int(x) == 1:
                        tmp = self.alphabet[n]
                        break
            after += tmp
        return after

alphabet = 'ABCDEFGH'
plane = 'DGHABCH'
plug_perm = [1, 2, 3, 4, 5, 6, 7, 8]
ref_perm = [8, 7, 6, 5, 4, 3, 2, 1]
plug_perm = [x - 1 for x in plug_perm]
ref_perm = [x - 1 for x in ref_perm]
I = []
J = []
K = []
for i in range (0,40319):
  for j in range (0,40319):
    for k in range (0,40319):
      rot1_perm = datalist[i]
      rot2_perm = datalist[j]
      rot3_perm = datalist[k]
      enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
      cipher_tentative = enigma.get_text(plane)
      decrypt = enigma.get_text(cipher_tentative)
      if 'ABC' in decrypt == True:
        I.append(i)
        J.append(j)
        K.append(k)

cipher = enigma.get_text(plane)
decrypt = enigma.get_text(cipher)
print(cipher)
print(decrypt)
peta
  • 13
  • 3