forked from exercism/python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
28 lines (21 loc) · 775 Bytes
/
example.py
File metadata and controls
28 lines (21 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from math import ceil, sqrt
import sys
if sys.version_info[0] == 2:
from itertools import izip_longest as zip_longest
else:
from itertools import zip_longest
def cipher_text(plain_text):
plain_text = _cleanse(plain_text)
square_size = int(ceil(sqrt(len(plain_text))))
square = _chunks_of(plain_text, square_size)
return ' '.join([''.join(column)
for column in zip_longest(*square, fillvalue=' ')])
def _cleanse(text):
"""Lowercase a string and remove punctuation and whitespace
"""
return ''.join([character for character in text
if character.isalnum()]).lower()
def _chunks_of(text, num):
if len(text) <= num:
return [text]
return [text[:num]] + _chunks_of(text[num:], num)