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
#2
You catch the return from scramble and then when the entire string is scrambled, pass the lists to interleave(), i.e.

The scramble of “12345678” can be similarly computed as “15372648".
This would imply that there are 4 scramble pairs, [1, 2, 3, 4], [5, 6, 7. 8] = after first scramble, and [1, 2], [5, 6], [3, 4], [7, 8] = after second scramble. Since this is simply splitting a list or string, the only significant code is the interleave
def interleave(list_in):
    after_interleave=""
    ## assumes all sub-lists are the same length
    for ctr in range(len(list_in[0])):  ## each position in all sub-lists
        for sub_list in list_in:
            after_interleave += str(sub_list[ctr])

    return after_interleave


after_scrambled=[[1, 2], [5, 6], [3, 4], [7, 8]]
result=interleave(after_scrambled)
print(result)
 
Reply
#3
Thanks for the help! Although I'm not sure it is doing exactly what I had hoped for, for example -

Hello World!.... as an input should give Hro.ldW.el .l!o. as an output,
with the code you provided Hello World!.... gives HloWrd..el ol!.. as an output.
Reply
#4
I think I got it in case interested, may be messy, new to Python -

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


def scramble(s3):
    if len(s3) == 2:
        return s3[0:2]
    else:
        half1 = str(s3[0:int((len(s3)/2))])
        half2 = str(s3[int((len(s3)/2)):int(len(s3))])
        return interleave (scramble(half1), scramble(half2))
input_word = input("Enter word ")

print (scramble(input_word))
Reply
#5
Have you tested this on words that are more than 8 characters long? A list of lists would probably work better IMHO, so do you have to use recursion?
Reply
#6
Yes, part of the exercise is that I have to use recursion. I left the first part of the code out- the first part of the code adds "." to any word that doesn't have a length that is a power of 2 to make it a power of 2 (ie. a word that is 12 characters long would be changed to one that is 16 characters long). I left this part of the description and code out to simplify my question but some of the words I tested are much longer than 8 characters and it seems to work.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scramble word game Zatoichi 9 8,414 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