Пример #1
0
def encode(text, k_val):
    output = []
    current = sympy.Matrix()
    for c in text:
        ascii_val = ord(c)
        for i in range(6, -1, -1):
            if current.shape[1] == k_val:
                output.append(current)
                current = sympy.Matrix()
            bit = pow(2, i)
            if ascii_val >= bit:
                ascii_val -= bit
                if current.shape[1] == 0:
                    current = sympy.Matrix([1])
                else:
                    current = current.col_insert(current.shape[1],
                                                 sympy.Matrix([1]))
            else:
                if current.shape[1] == 0:
                    current = sympy.Matrix([0])
                else:
                    current = current.col_insert(current.shape[1],
                                                 sympy.Matrix([0]))
    nonempty = current.shape[1]
    final = sympy.Matrix([1] * nonempty).T
    if final.shape[1] == 0:
        final = sympy.Matrix([0] * nonempty).T
    elif not nonempty == k_val:
        current = current.row_join(sympy.Matrix([0] * (k_val - nonempty)).T)
        final = final.row_join(sympy.Matrix([0] * (k_val - nonempty)).T)
        mix = Permutation.random(final.shape[1]).array_form
        randomize = []
        for i in mix:
            randomize.append(final[i])
        final = sympy.Matrix(randomize).T
    output.append(current)
    output.append(final)
    if args.v:
        for o in output:
            sympy.pprint(o)
    if args.o:
        with gzip.open(args.o, 'wb') as f:
            f.write(pickle.dumps(output))
Пример #2
0
import sys, time, subprocess, random, string
from sympy.combinatorics import Permutation

Permutation.print_cyclic = False

total = 5
success = 0
maxlen = 10
maxpower = 100000

for i in range(total):
    len = random.randrange(1, maxlen)
    power = random.randrange(-maxpower, maxpower)
    perm = Permutation.random(len)

    proc = subprocess.Popen(['./stress', str(len)] +
                            list(map(str, perm.array_form)),
                            stdout=subprocess.PIPE)
    c = proc.stdout.readline()
    result = int(c)
    #result = Permutation(list(map(lambda x: int(x) - 1, str.split(str(c)[3:-2]))))

    if (result == 1) == perm.is_even:
        success = success + 1
    else:
        print("Tried " + str(perm))
        print("Expected " + str(perm.is_even))
        print("Got " + str(result))
        print()

print(str(success) + "/" + str(total))