9 jupytext_version: 1.3.4
11 display_name: Python 3
16 ```python Collapsed="false"
17 from szyfrow.keyword_cipher import *
18 from szyfrow.column_transposition import *
19 from szyfrow.support.text_prettify import *
22 ```python Collapsed="false"
24 plaintext_a_filename = f'plaintext.{challenge_number}a.txt'
25 plaintext_b_filename = f'plaintext.{challenge_number}b.txt'
26 ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'
27 ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'
30 ```python Collapsed="false"
31 ca = open(ciphertext_a_filename).read()
33 cb = open(ciphertext_b_filename).read()
37 ```python Collapsed="false"
38 (word_a, wrap_a), score_a = keyword_break_mp(sca, fitness=Ptrigrams)
39 print(word_a, wrap_a, '\n')
40 pa = keyword_decipher(ca, word_a, wrap_a)
44 ```python Collapsed="false"
45 keyword_decipher(ca, 'akela', wrap_a)
48 ```python Collapsed="false"
49 word_a, score_a = simulated_annealing_break(sca, fitness=Ptrigrams,
50 plain_alphabet=string.ascii_lowercase, cipher_alphabet=keyword_cipher_alphabet_of('akkad', wrap_alphabet=KeywordWrapAlphabet.from_largest))
52 pa = keyword_decipher(ca, word_a)
56 ```python Collapsed="false"
57 pa = prettify(keyword_decipher(sca, 'akela', wrap_alphabet=KeywordWrapAlphabet.from_a))
61 ```python Collapsed="false"
62 open(plaintext_a_filename, 'w').write(pa)
65 ```python Collapsed="false"
66 (word_b, wrap_b), score_b = keyword_break_mp(scb, fitness=Ptrigrams)
67 print(word_b, wrap_b, '\n')
68 pb = keyword_decipher(cb, word_b, wrap_b)
72 ```python Collapsed="false"
73 pb = prettify(keyword_decipher(scb, word_b, wrap_b))
77 ```python Collapsed="false"
78 open(plaintext_b_filename, 'w').write(pb)
81 ```python Collapsed="false"
82 pb = """dear uncle wilhelm as you promised we thoroughly enjoyed our visit to your cousin in lincoln and
83 found it most informative stop the beautiful cathedral almost justified our visit on its own and our
84 hosts shared with us some interesting drawings of the towers you mentioned stop they graciously
85 allowed me to copy the sketches and explained much about how the towers are built and why stop as
86 usual i will send you my notes and sketches via our dear friend jessica who has promised to ensure
87 their safe delivery stop really i must congratulate our hosts in the local scout groups for the
88 marvellous way in which they have organised our travel though some of the scouts have been rather
89 more solicitous than we had expected asking rather a lot of questions about our plans stop generally
90 i hope they were satisfied with the answers that we gave but we feel that we are at risk of
91 overstaying our welcome so we will return to london on tuesday stop even though we have enjoyed our
92 time together there has been some debate among the group about where we should visit next stop there
93 are so many interesting sites to visit along the majestic thames estuary so we have decided to split
94 into two groups stop ralf will lead one party on a tour of the kent coast while i am very much
95 looking forward to exploring the essex marshes stop do let me know if you have a better idea but i
96 have been told that canewdons fifteenth century church affords an elevation with a wide view of
97 the surrounding lands in this otherwise rather flat landscape and is the site of another fascinating
98 tower which i will be certain to sketch for you stop finally if you have any further requests for
99 specific information then perhaps you could leave a message for me at the post office there stop karl
103 ```python Collapsed="false"
107 ```python Collapsed="false"
109 [pbs[i+1] for i, w in enumerate(pbs) if w == 'stop']
112 ```python Collapsed="false"
113 wcat(segment(cat(w[0] for w in pbs)))
116 ```python Collapsed="false"
117 wcat(segment(cat(w[1] for w in pbs if len(w) > 1)))
120 ```python Collapsed="false"
121 wcat(segment(cat(w[-1] for w in reversed(pbs))))
124 ```python Collapsed="false"
125 max([len(w) for w in pbs])
128 ```python Collapsed="false"
129 [len(s.strip().split()) for s in pb.split('stop')]
132 ```python Collapsed="false"
133 tt = ''.maketrans('rdf', 'RDF')
134 pbcap = wcat(pb.split()).translate(tt)
135 spbcap = wcat(spb.split()).translate(tt)
139 ```python Collapsed="false"
140 pbcap2 = cat(('\n' + c) if c.isupper() else c for c in pbcap)
144 ```python Collapsed="false"
148 ```python Collapsed="false"
149 cat(w[-1] for w in chunks(spb, 5))
152 ```python Collapsed="false"
153 locR = [i for i, c in enumerate(spb) if c == 'r']
154 locD = [i for i, c in enumerate(spb) if c == 'd']
155 locF = [i for i, c in enumerate(spb) if c == 'f']
156 len(locR), len(locD), len(locF)
159 ```python Collapsed="false"
166 triples += [(r, d, f, d - r)]
170 ```python Collapsed="false"
171 t = cat(c if c.isupper() else '.' for c in pbscap)
173 tcs = chunks(spb, 32)
174 stcs = [wcat([c[:col], c[col], c[col+1:]]) for c in tcs]
178 ```python Collapsed="false"
180 for l in range(1, 40):
182 cs = chunks(spb, l, fillvalue=' ')
183 t = cat(c[i] for c in cs)
185 results += [{'l': l, 'i': i, 't': t, 'f': f * l}]
189 ```python Collapsed="false"
190 list(sorted(results, key=lambda r: r['f']))[-4:]
193 ```python Collapsed="false"
194 rdfresults = [r for r in results if 'rdf' in r['t']]
198 ```python Collapsed="false"
202 ```python Collapsed="false"