Python Forum
Scramble and Interleave in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scramble and Interleave in Python
#1
Hello, I am struggling to answer the second part of this question that requires a scramble and an interleave. The description of the exercise is below and my attempt at the code follows. Appreciate any help.

In this exercise, you will implement a simple encryption algorithm, one that rearranges the letters in a message. While this algorithm will make the message difficult to read, it provides little actual security and should never be used for sensitive information.
a. Given two messages, A and B, which have the same length, we can create a new message by taking the first character from A, then the first character from B, then the second character from A, then the second character from B, and so on. We’ll call this the interleave of A and B.
For example, if A is the text “abcde” and B is the text “12345” the interleave of A and B is “a1b2c3d4e5”.
Write a function, interleave(), that takes two strings of the same length and returns the interleave of the two.
b. To make a message even harder to read, we can perform several interleaves in a row. Assume that the length of a message is a power of 2. We define the scramble of the message recursively as follows:
1. The scramble of a single character is just that character.
2. The scramble of a longer message is found by taking the scramble of the first half of the message and the scramble of the second half
of the message, and interleaving them.
For example, the scramble(“12”) should compute the scramble of “1”, which is “1”, interleaved with the scramble of “2”, which is “2”. The result is simply “12”.
The scramble of “1234” is the interleave of the scramble of “12”, which is “12”, and the scramble of “34”, which is similarly “34”. The result is “1324”.
The scramble of “12345678” can be similarly computed as “15372648".


My code:
def interleave (s1, s2):
    if (len(s1) + len(s2)) <= 2:
        return s1 + s2
    else:
        result = ""
        for i in range(0, len(s1)):
            result += s1[i] + s2[i]
        return result
#
#a = input("Enter first string ")
#b = input("Enter second string ")
#
#print(interleave(a, b))

def scramble(s3):
        half1 = str(s3[0:int((len(s3)/2-1))])
        half2 = str(s3[int((len(s3)/2)):int(len(s3)-1)])
        if len(half1) <= 2:
           return interleave (half1, half2)
        else:
           return interleave (scramble(half1), scramble(half2))


c = input("Enter string ")

print (scramble(c))
Reply


Messages In This Thread
Scramble and Interleave in Python - by dsaks - Jun-12-2018, 05:07 PM
RE: Scramble and Interleave in Python - by woooee - Jun-12-2018, 05:40 PM
RE: Scramble and Interleave in Python - by dsaks - Jun-13-2018, 02:03 AM
RE: Scramble and Interleave in Python - by dsaks - Jun-13-2018, 03:08 AM
RE: Scramble and Interleave in Python - by woooee - Jun-13-2018, 06:04 PM
RE: Scramble and Interleave in Python - by dsaks - Jun-13-2018, 10:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Scramble word game Zatoichi 9 8,468 Sep-20-2021, 03:47 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020