#! /usr/bin/python3 import wordsegment import numpy import random import itertools import binascii from Crypto.Util.strxor import strxor import hw7a ## try hw7a.main() for test examples ## also, see seti.py ## problem 1 # load the bigrams, unigrams, etc. S = wordsegment.Segmenter() S.load() p1 = S.segment("thisisatest") ## problem 2 p2 = "thisisanotherlittlestring" ## use numpy array functions p2a = numpy.asarray(list(p2)) p2a2 = p2a.reshape([5,5]) ## note: this is destructive def swap_cols(arr, frm, to): arr[:,[frm, to]] = arr[:,[to, frm]] ## note: this is destructive def swap_rows(arr, frm, to): arr[[frm, to],:] = arr[[to, frm],:] #p2ac = swap_cols(p2a2, 1, 2) #p2ar = swap_rows(p2a2, 1, 2) p2apc = hw7a.permute_cols(p2a2, [4,3,2,1,0]) p2apr = hw7a.permute_rows(p2a2, [4,3,2,1,0]) p2shuffle = list(range(5)) random.shuffle(p2shuffle) ## for transdecode, can try all possible permutations p2x = list(range(3)) p2xi = itertools.permutations(p2x) ## or just p2xip = itertools.permutations(range(3)) ## problem 3 ## use trial and error with swap functions ## problem 4 ## for encodexorbase64(string, key) ## need to adjust key to be same length as string ## convert strings to base64 p4 = b"thisisatest" p4b64 = binascii.b2a_base64(p4) ## from Crypto module p4xor = strxor(p4b64,p4b64) p4againb64 = strxor(p4xor,p4b64) p4again = binascii.a2b_base64(p4againb64) # problem 5 ## see UNIX shell scripts discussion