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"
114 cat(pbs[i+1][0] for i, w in enumerate(pbs) if w == 'stop')
117 ```python Collapsed="false"
118 wcat(segment(cat(w[0] for w in pbs)))
121 ```python Collapsed="false"
122 wcat(segment(cat(w[1] for w in pbs if len(w) > 1)))
125 ```python Collapsed="false"
126 wcat(segment(cat(w[-1] for w in reversed(pbs))))
129 ```python Collapsed="false"
130 max([len(w) for w in pbs])
133 ```python Collapsed="false"
134 [len(s.strip().split()) for s in pb.split('stop')]
137 ```python Collapsed="false"
138 tt = ''.maketrans('rdf', 'RDF')
139 pbcap = wcat(pb.split()).translate(tt)
140 spbcap = wcat(spb.split()).translate(tt)
144 ```python Collapsed="false"
145 pbcap2 = cat(('\n' + c) if c.isupper() else c for c in pbcap)
149 ```python Collapsed="false"
153 ```python Collapsed="false"
154 cat(w[-1] for w in chunks(spb, 5))
157 ```python Collapsed="false"
158 locR = [i for i, c in enumerate(spb) if c == 'r']
159 locD = [i for i, c in enumerate(spb) if c == 'd']
160 locF = [i for i, c in enumerate(spb) if c == 'f']
161 len(locR), len(locD), len(locF)
164 ```python Collapsed="false"
171 triples += [(r, d, f, d - r)]
175 ```python Collapsed="false"
176 t = cat(c if c.isupper() else '.' for c in pbscap)
178 tcs = chunks(spb, 32)
179 stcs = [wcat([c[:col], c[col], c[col+1:]]) for c in tcs]
183 ```python Collapsed="false"
185 for l in range(1, 40):
187 cs = chunks(spb, l, fillvalue=' ')
188 t = cat(c[i] for c in cs)
190 results += [{'l': l, 'i': i, 't': t, 'f': f * l}]
194 ```python Collapsed="false"
195 list(sorted(results, key=lambda r: r['f']))[-4:]
198 ```python Collapsed="false"
199 rdfresults = [r for r in results if 'rdf' in r['t']]
203 ```python Collapsed="false"
207 ```python Collapsed="false"