You've devised a simple encryption method for alphabetic strings that shuffles the characters in such a way that the resulting string is hard to quickly read, but is easy to convert back into the original string.
When you encrypt a string S, you start with an initially-empty resulting string R and append characters to it as follows:
For example, to encrypt the string "abc", we first take "b", and then append the encrypted version of "a" (which is just "a") and the encrypted version of "c" (which is just "c") to get "bac".
If we encrypt "abcxcba" we'll get "xbacbca". That is, we take "x" and then append the encrypted version "abc" and then append the encrypted version of "cba".
S contains only lower-case alphabetic characters
1 <= |S| <= 10,000
Return string R, the encrypted version of S.
S = "abc"
R = "bac"
S = "abcd"
R = "bacd"
S = "abcxcba"
R = "xbacbca"
S = "facebook"
R = "eafcobok"
This is the solution I came up with, it's O(n):
import pytest
def encrypt(string: str, start: int, end: int) -> list[str]:
'''
Encrypts the substring of `string` between indexes `start` and `end`,
`substring = string[start:end + 1]`.
time O(n)
'''
# if starting index is greater than ending,
# substring is an empty string,
# len(substring) == 0
if start > end:
return []
# if indexes are equal,
# substring is just character at start index,
# len(substring) == 1
if start == end:
return [string[start]]
# encrypted string is list of strings to act as string builder
encrypted = []
# midpoint of substring
midpoint = ((end - start) // 2) + start
encrypted.append(string[midpoint])
# append encrypted left substring
encrypted.extend(encrypt(string, start, midpoint - 1))
# append encrypted right substring
encrypted.extend(encrypt(string, midpoint + 1, end))
return encrypted
def findEncryptedWord(s: str) -> str:
return ''.join(encrypt(s, 0, len(s) - 1))
@pytest.mark.parametrize(
('string', 'encrypted'), (
('abc', 'bac'),
('abcd', 'bacd'),
('abcxcba', 'xbacbca'),
('facebook', 'eafcobok')
)
)
def test(string, encrypted):
assert findEncryptedWord(string) == encryptedAll tests passing