Matthew Schell
Matthew Schell

Reputation: 679

How to split strings of different lengths?

I'm trying to make my own encryption algorithm but I don't know how to split strings that have different lengths into a list. Here is my code:

import random
from calculations import test_divisibility  # This is a function that returns which numbers can divide evenly into the given number

def encrypt(text):
    div_by = test_divisibility(len(text))
    if div_by is None:  # If length of string is a prime number, figure out an alternate way to encrypt the string.
        pass

    divisor = random.choice(div_by)

original = "This is the string to be hashed into the untitled hashing algorithm."

print(encrypt(original))

The function will pick a random number from the returned list and divide that number into the length of the message to encrypt. The result of that number should be the number of slices the string should have.

Take for example the string "This is the string to be encrypted into my custom-made algorithm". If you pass the string to the function you will find that the length of the string is divisible by 2, 4, and 8. Then the random function is supposed to choose one of those numbers and divide the length of the string by that number. The result will be the number of slices the string should have.

Upvotes: 0

Views: 301

Answers (2)

CryptoFool
CryptoFool

Reputation: 23089

Here's a function that will break a string into c equal size chunks, with the last chunk containing any overflow:

def split_str(strng, c):
    l = len(strng) // c
    r = [strng[n * l:(n + 1) * l] for n in range(c - 1)]
    r.append(strng[(c - 1) * l:])
    return r

s = 'Now is the time for all good men to come to the aid'

print(split_str(s, 4))

Result:

['Now is the t', 'ime for all ', 'good men to ', 'come to the aid']

You can simplify the function a bit if you know that the string will always divide evenly. Then it would be just:

def split_str(str, c):
    l = len(str) // c
    r = [str[n*l:(n+1)*l] for n in range(c)]

The rest is just math, which it sounds like you already have a handle on. Or do you need help finding the factors of a number? I know there are lots of questions about that already on SO.

Upvotes: 2

martineau
martineau

Reputation: 123463

Here's something a little different that will break the string into as many equally- sized groups as possible and put any excess into an extra one. That won't happen when the length if the string is an exact multiple of the group size.

from itertools import zip_longest

_filler = object()  # Value which couldn't be in data.

def grouper(n, iterable):
    for result in zip_longest(*[iter(iterable)]*n, fillvalue=_filler):
        yield tuple(v for v in result if v is not _filler)

n = 4
s = 'Now is the time for all good men to come to the aid'
group_len = len(s) // n
result = list(''.join(group) for group in grouper(group_len, s))
print(result)

Output

['Now is the t', 'ime for all ', 'good men to ', 'come to the ', 'aid']

Upvotes: 0

Related Questions