4 "signature": "sha256:285f7a82eaf5cc815e9a686ebce8aa2cfbdecb14f2473c9b5dfe0aedafcd727c"
15 "from cipher import *\n",
16 "from cipherbreak import *\n",
28 "def railfence_encipher(message, height, fillvalue=' '):\n",
29 " \"\"\"Railfence cipher\n",
31 " >>> railfence_encipher('hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers', 2, fillvalue='!')\n",
32 " 'hlohraateerishsslnpeefetotsigaleccpeselteevsmhatetiiaogicotxfretnrifneihr!'\n",
33 " >>> railfence_encipher('hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers', 3, fillvalue='!')\n",
34 " 'horaersslpeeosglcpselteevsmhatetiiaogicotxfretnrifneihr!!lhateihsnefttiaece!'\n",
35 " >>> railfence_encipher('hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers', 5, fillvalue='!')\n",
36 " 'hresleogcseeemhetaocofrnrner!!lhateihsnefttiaece!!ltvsatiigitxetifih!!oarspeslp!'\n",
37 " >>> railfence_encipher('hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers', 10, fillvalue='!')\n",
38 " 'hepisehagitnr!!lernesge!!lmtocerh!!otiletap!!tseaorii!!hassfolc!!evtitffe!!rahsetec!!eixn!'\n",
40 " sections = chunks(message, (height - 1) * 2, fillvalue=fillvalue)\n",
41 " # Add the top row\n",
42 " rows = [s[0] for s in sections]\n",
43 " # process the middle rows of the grid\n",
44 " for r in range(1, height - 1):\n",
45 " rows += [s[r] + s[-r] for s in sections]\n",
46 " # process the bottom row\n",
47 " rows += [s[height - 1] for s in sections]\n",
48 " return ''.join(rows)"
59 "pt = 'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers'\n",
71 "sections = chunks(pt, (height - 1) * 2, fillvalue='!')\n",
72 "n_sections = len(sections)\n",
73 "# Add the top row\n",
74 "rows = [' '.join([s[0] for s in sections])]\n",
75 "# process the middle rows of the grid\n",
76 "for r in range(1, height - 1):\n",
77 " # rows += [''.join([s[r:r+1] + s[n_sections-r:n_sections-r+1] for s in sections])]\n",
78 " rows += [''.join([s[r] + s[-r] for s in sections])]\n",
79 "# process the bottom row\n",
80 "# rows += [' '.join([s[height - 1:height] for s in sections])]\n",
81 "rows += [' '.join([s[height - 1] for s in sections])]\n",
89 "output_type": "pyout",
92 "['h a r s e t l e',\n",
93 " 'eevatiacoreifhr!',\n",
94 " 'lraeislefosaeps!',\n",
95 " 'lesheioitftrni!!',\n",
96 " 'ohteshnpetigcc!!',\n",
109 "language": "python",
114 "output_type": "pyout",
115 "prompt_number": 197,
134 "sections = chunks(pt, (height - 1) * 2, fillvalue='')\n",
135 "# Add the top row\n",
136 "rows = [' '.join([s[0] for s in sections])]\n",
137 "# process the middle rows of the grid\n",
138 "for r in range(1, height-1):\n",
139 " print(r, height*2-r-2, ':', sections[0][r:r+1], sections[0][height*2-r-2:height*2-r-1])\n",
140 " rows += [''.join([s[r:r+1] + s[height*2-r-2:height*2-r-1] for s in sections])]\n",
141 " # rows += [''.join([s[r] + s[-r] for s in sections])]\n",
142 "# process the bottom row\n",
143 "rows += [' '.join([s[height - 1:height] for s in sections])]\n",
144 "# rows += [' '.join([s[height - 1] for s in sections])]\n",
147 "language": "python",
151 "output_type": "stream",
162 "output_type": "pyout",
163 "prompt_number": 198,
165 "['h a r s e t l e',\n",
166 " 'eevatiacoreifhr',\n",
167 " 'lraeislefosaeps',\n",
168 " 'lesheioitftrni',\n",
169 " 'ohteshnpetigcc',\n",
182 "language": "python",
187 "output_type": "pyout",
188 "prompt_number": 199,
207 "ct = ''.join(c for c in ''.join(rows) if c != ' ')\n",
210 "language": "python",
215 "output_type": "pyout",
216 "prompt_number": 200,
218 "'harsetleeevatiacoreifhrlraeislefosaepslesheioitftrniohteshnpetigcctmtgxne'"
228 "n_sections = math.ceil(len(pt) / ((height - 1) * 2))\n",
231 "language": "python",
236 "output_type": "pyout",
237 "prompt_number": 202,
249 "padding_to_add = n_sections * (height - 1) * 2 - len(pt)\n",
252 "language": "python",
257 "output_type": "pyout",
258 "prompt_number": 203,
270 "row_lengths = [n_sections] * (height - 1) * 2\n",
273 "language": "python",
278 "output_type": "pyout",
279 "prompt_number": 204,
281 "[8, 8, 8, 8, 8, 8, 8, 8, 8, 8]"
291 "for i in range((height - 1) * 2 - 1, (height - 1) * 2 - (padding_to_add + 1), -1):\n",
292 " row_lengths[i] -= 1\n",
295 "language": "python",
300 "output_type": "pyout",
301 "prompt_number": 205,
303 "[8, 8, 8, 7, 7, 7, 7, 7, 7, 7]"
313 "folded_row_lengths = [row_lengths[0]]\n",
314 "for i in range(1, height-1):\n",
315 " folded_row_lengths += [row_lengths[i] + row_lengths[-i]]\n",
316 "folded_row_lengths += [row_lengths[height - 1]]\n",
319 "language": "python",
324 "output_type": "pyout",
325 "prompt_number": 206,
327 "[8, 15, 15, 14, 14, 7]"
339 "for i in folded_row_lengths:\n",
340 " rows += [ct[row_start:row_start + i]]\n",
344 "language": "python",
349 "output_type": "pyout",
350 "prompt_number": 207,
353 " 'eevatiacoreifhr',\n",
354 " 'lraeislefosaeps',\n",
355 " 'lesheioitftrni',\n",
356 " 'ohteshnpetigcc',\n",
367 "down_rows = [rows[0]]\n",
369 "for i in range(1, height-1):\n",
370 " down_rows += [''.join([c for n, c in enumerate(rows[i]) if n % 2 == 0])]\n",
371 " up_rows += [''.join([c for n, c in enumerate(rows[i]) if n % 2 == 1])]\n",
372 "down_rows += [rows[-1]]\n",
375 "language": "python",
380 "output_type": "pyout",
381 "prompt_number": 208,
383 "(['harsetle', 'evtaoefr', 'lailfses', 'lseottn', 'otsneic', 'tmtgxne'],\n",
384 " ['eaicrih', 'reseoap', 'ehiifri', 'hehptgc'])"
394 "up_rows.reverse()\n",
395 "down_rows + up_rows"
397 "language": "python",
402 "output_type": "pyout",
403 "prompt_number": 164,
422 "''.join(c for r in zip_longest(*(down_rows + up_rows), fillvalue='') for c in r)"
424 "language": "python",
429 "output_type": "pyout",
430 "prompt_number": 165,
432 "'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers'"
442 "def rfe(message, height, fillvalue=''):\n",
443 " sections = chunks(message, (height - 1) * 2, fillvalue=fillvalue)\n",
444 " n_sections = len(sections)\n",
445 " # Add the top row\n",
446 " rows = [''.join([s[0] for s in sections])]\n",
447 " # process the middle rows of the grid\n",
448 " for r in range(1, height-1):\n",
449 " rows += [''.join([s[r:r+1] + s[height*2-r-2:height*2-r-1] for s in sections])]\n",
450 " # process the bottom row\n",
451 " rows += [''.join([s[height - 1:height] for s in sections])]\n",
452 " # rows += [' '.join([s[height - 1] for s in sections])]\n",
453 " return ''.join(rows)"
455 "language": "python",
466 "language": "python",
471 "output_type": "pyout",
472 "prompt_number": 177,
474 "'hresleogcseeemhetaocofrnrnerlhateihsnefttiaeceltvsatiigitxetifihoarspeslp'"
486 "language": "python",
491 "output_type": "pyout",
492 "prompt_number": 178,
504 "def rfd(message, height, fillvalue=''):\n",
505 " n_sections = math.ceil(len(message) / ((height - 1) * 2))\n",
506 " padding_to_add = n_sections * (height - 1) * 2 - len(message)\n",
507 " row_lengths = [n_sections] * (height - 1) * 2\n",
508 " for i in range((height - 1) * 2 - 1, (height - 1) * 2 - (padding_to_add + 1), -1):\n",
509 " row_lengths[i] -= 1\n",
510 " folded_row_lengths = [row_lengths[0]]\n",
511 " for i in range(1, height-1):\n",
512 " folded_row_lengths += [row_lengths[i] + row_lengths[-i]]\n",
513 " folded_row_lengths += [row_lengths[height - 1]]\n",
516 " for i in folded_row_lengths:\n",
517 " rows += [message[row_start:row_start + i]]\n",
519 " down_rows = [rows[0]]\n",
521 " for i in range(1, height-1):\n",
522 " down_rows += [''.join([c for n, c in enumerate(rows[i]) if n % 2 == 0])]\n",
523 " up_rows += [''.join([c for n, c in enumerate(rows[i]) if n % 2 == 1])]\n",
524 " down_rows += [rows[-1]]\n",
525 " up_rows.reverse()\n",
526 " return ''.join(c for r in zip_longest(*(down_rows + up_rows), fillvalue='') for c in r)"
528 "language": "python",
538 "print(rfe(pt, h))\n",
541 "language": "python",
545 "output_type": "stream",
548 "haspolsevsetgifrifrlatihnettaeelemtiocxernhorersleesgcptehaiaottneihesfic\n"
553 "output_type": "pyout",
554 "prompt_number": 221,
556 "'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers'"
566 "for h in range(2, 51):\n",
567 " assert rfd(rfe(pt, h), h) == pt\n",
568 " print(h, ':', rfd(rfe(pt, h), h) == pt)"
570 "language": "python",
574 "output_type": "stream",
637 "language": "python",
642 "output_type": "pyout",
643 "prompt_number": 224,
655 "rfe('hellotherefriends', 4)"
657 "language": "python",
662 "output_type": "pyout",
663 "prompt_number": 228,
665 "'hhieterelorfnsled'"
675 "rfd('hhieterelorfnsled', 4)"
677 "language": "python",
682 "output_type": "pyout",
683 "prompt_number": 229,
685 "'hellotherefriends'"
695 "for h in [3, 5, 7]:\n",
698 "language": "python",
702 "output_type": "stream",
705 "horaersslpeeosglcpselteevsmhatetiiaogicotxfretnrifneihrlhateihsnefttiaece\n",
706 "hresleogcseeemhetaocofrnrnerlhateihsnefttiaeceltvsatiigitxetifihoarspeslp\n",
707 "haspolsevsetgifrifrlatihnettaeelemtiocxernhorersleesgcptehaiaottneihesfic\n"
717 "language": "python",