Create a function (in Python) transdecode(s, shape, limit=4) which takes a strin
ID: 3717932 • Letter: C
Question
Create a function (in Python) transdecode(s, shape, limit=4) which takes a string, permuted by transencode, and decodes it by brute force, trying all possible permutations, checking each one using wordsegment. It returns all possible strings of with more than limit-1 words
FYI: transencode(s, shape) converts a string into a matrix of the given shape, and random permutes both the rows and columns (using random.shuffle()). It returns a flattened string.
>>> q1 = transencode(q, [2,3])
>>> q1 'wotneo'
>>> q2 = transdecode(q1, [2,3], 2)
>>> q2 ['wot neo', 'wto noe', 'ow teno', 'ot we on', 'two one', 'to woen', 'neo wot', 'noew to', 'eno owt', 'eo not w', 'one two', 'oen to w']
Explanation / Answer
def makematrix(s,shape):
pass
def flatten(array):
pass
def permute_rows(arr, neworder):
pass
def permute_cols(arr, neworder):
pass
def transencode(s, shape):
pass
def transdecode(s, shape, limit=4):
pass
>>> x39 = makematrix(x, [3,9])
>>> x93 = makematrix(x, [9,3])
>>> x39
array([['o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r'],
['e', 'e', 'f', 'o', 'u', 'r', 'f', 'i', 'v'],
['e', 's', 'i', 'x', 's', 'e', 'v', 'e', 'n']],
dtype='<U1')
>>> x93
array([['o', 'n', 'e'],
['t', 'w', 'o'],
['t', 'h', 'r'],
['e', 'e', 'f'],
['o', 'u', 'r'],
['f', 'i', 'v'],
['e', 's', 'i'],
['x', 's', 'e'],
['v', 'e', 'n']],
dtype='<U1')
>>> permute_rows(x39, [2,0,1])
array([['e', 's', 'i', 'x', 's', 'e', 'v', 'e', 'n'],
['o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r'],
['e', 'e', 'f', 'o', 'u', 'r', 'f', 'i', 'v']],
dtype='<U1')
>>> permute_rows(x39, [1,2,0])
array([['e', 'e', 'f', 'o', 'u', 'r', 'f', 'i', 'v'],
['e', 's', 'i', 'x', 's', 'e', 'v', 'e', 'n'],
['o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r']],
dtype='<U1')
>>> permute_cols(x93, [2,0,1])
array([['e', 'o', 'n'],
['o', 't', 'w'],
['r', 't', 'h'],
['f', 'e', 'e'],
['r', 'o', 'u'],
['v', 'f', 'i'],
['i', 'e', 's'],
['e', 'x', 's'],
['n', 'v', 'e']],
dtype='<U1')
>>> permute_cols(x93, [1,2,0])
array([['n', 'e', 'o'],
['w', 'o', 't'],
['h', 'r', 't'],
['e', 'f', 'e'],
['u', 'r', 'o'],
['i', 'v', 'f'],
['s', 'i', 'e'],
['s', 'e', 'x'],
['e', 'n', 'v']],
dtype='<U1')
>>> q = 'onetwo'
>>> q1 = transencode(q, [2,3])
>>> q1
'wotneo'
>>> q2 = transdecode(q1, [2,3], 2)
>>> q2
['wot neo', 'wto noe', 'ow teno', 'ot we on', 'two one', 'to woen', 'neo wot', 'noew to', 'eno owt', 'eo not w', 'one two', 'oen to w']
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.