문제

I've currently created a 3x3 matrix in python using numpy (initializing every value to 0). I would like to create a small python program that brute forces EVERY possible KEY combination in the matrix. For example:

[1, 0, 0
 0, 0, 0
 0, 0, 0]

[1, 1, 0
 0, 0, 0
 0, 0, 0]

etc... all the way to:

[9, 9, 9
 9, 9, 9
 9, 9, 9]

Seems very trivial but for some reason can't wrap my head around it. The reason I'm doing this is because I want to get the inverse of EVERY matrix combination (which is easy using numpy) and multiply it by another matrix until I get a solution I'm looking for... Essentially I'm trying to brute for the Crypto Hill Cipher.

Your help is greatly appreciated!

도움이 되었습니까?

해결책 2

This should do it i think.

from itertools import combinations_with_replacement
import numpy as np

x = np.empty((3,3), dtype=int)

for comb in combinations_with_replacement(range(10),9):
    x.flat[:] = comb

다른 팁

If your alphabet is just the 10 digits then what you're doing there is technically called "counting in base 10" ;-)

At each step increment the last digit (bottom right). If it was 9, wrap it around to 0 and increment the next-to-last digit, and so on until after 10 billion steps the top digit wraps.

It might also be possible to do something more efficient with itertools.product, but since that doesn't produce the numpy matrices you need, maybe not.

If your alphabet is 26 characters then you might be waiting a while for this to finish running, since 26**10 is a rather large number.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top