X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=cipher.py;h=6b49d46974171e26edd615cdbb8e8a7985bf6720;hb=36ca1e6815f68220d42758ee5bbf92c1de554490;hp=ebba6d622c5c182b604dcef27fe92aa34565667c;hpb=c50c0b62ecdf5138d6c4b3677aba8035c8481f0c;p=cipher-tools.git diff --git a/cipher.py b/cipher.py index ebba6d6..6b49d46 100644 --- a/cipher.py +++ b/cipher.py @@ -2,6 +2,7 @@ import string import collections import norms import logging +from itertools import zip_longest from segment import segment # To time a run: @@ -61,6 +62,33 @@ def ngrams(text, n): """ return [tuple(text[i:i+n]) for i in range(len(text)-n+1)] +def every_nth(text, n): + """Returns n strings, each of which consists of every nth character, + starting with the 0th, 1st, 2nd, ... (n-1)th character + + >>> every_nth(string.ascii_lowercase, 5) + ['afkpuz', 'bglqv', 'chmrw', 'dinsx', 'ejoty'] + >>> every_nth(string.ascii_lowercase, 1) + ['abcdefghijklmnopqrstuvwxyz'] + >>> every_nth(string.ascii_lowercase, 26) + ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] + """ + split_text = [text[i:i+n] for i in range(0, len(text), n)] + return [''.join(l) for l in zip_longest(*split_text, fillvalue='')] + +def combine_every_nth(split_text): + """Reforms a text split into every_nth strings + + >>> combine_every_nth(every_nth(string.ascii_lowercase, 5)) + 'abcdefghijklmnopqrstuvwxyz' + >>> combine_every_nth(every_nth(string.ascii_lowercase, 1)) + 'abcdefghijklmnopqrstuvwxyz' + >>> combine_every_nth(every_nth(string.ascii_lowercase, 26)) + 'abcdefghijklmnopqrstuvwxyz' + """ + return ''.join([''.join(l) for l in zip_longest(*split_text, fillvalue='')]) + + def letter_frequencies(text): """Count the number of occurrences of each character in text