Stretch string with random spaces with the defined max length

Viewed 39

Long explanation :
I wrote a program to simulate a simple Caesar coding algorithm with python. After the first step of ciphering, we calculate the sum of alphabets in the new string and then "I want to stretch the original string on size of that calculated sum with random spaces!" It will be send to the destination and with that length and after the removing spaces, it will decipher the string for 2 times to get the original string.

Short explanation :
Just complete the stretch_str(text, length) function to generate the string as example follows :

text = "Hi I'm mark."
print(stretch_str(text, 23))
# The output can be :
# "H  i   I'  m m a  rk  ."
# or
# "H i  I ' m  m a r k   ."
# or any thing else like these...

And the code :

import string


def caesar(plaintext, shift):
    alphabet = string.ascii_lowercase
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    table = str.maketrans(alphabet, shifted_alphabet)
    return plaintext.translate(table)


def stretch_str(text, length):
    # I want to complete this function.
    # Stretch this text with random spaces.
    # The last character is dot, always.
    pass


# Sender section :
in_str = "some example sentence."
ciphered = caesar(in_str,-3)
total_sum = 0
for ch in ciphered[:-1].replace(" ", ""):
    total_sum += string.ascii_lowercase.index(ch)
ciphered_more = caesar(ciphered,total_sum%26)
msg = stretch_str(ciphered_more, total_sum)

# Receiver section :
number = len(msg)
ciphered_more = msg.replace(" ", "")
ciphered = caesar(ciphered_more, -(number%26))
original_str = caesar(ciphered, 3)
print(original_str)
# Printing the "someexamplesentence."
1 Answers
import random


def stretch_str(text, length):
    text = list(text)
    while len(text) < length:
        text.insert(random.randint(1, len(text) - 1), " ")
    return "".join(text)


text = "Hi I'm mark."
print(f"[{stretch_str(text, 23)}]")

Prints (for example):

[H    i I'm    m  a r k.]
Related