Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions strings/camel_case_to_snake_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def camel_to_snake_case(input_str: str) -> str:
>>> camel_to_snake_case("someRandomString")
'some_random_string'
>>> camel_to_snake_case("SomeRandomStr#ng")
'some_random_str_ng'
>>> camel_to_snake_case("SomeRandomString")
'some_random_string'
>>> camel_to_snake_case("123someRandom123String123")
'123_some_random_123_string_123'
Expand Down
93 changes: 93 additions & 0 deletions strings/secret_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Encoding & Decoding

import random
import string


def random_chars() -> str:
"""
Generate a random string of 3 ASCII letters.

>>> import random
>>> len(random_chars()) == 3
True


>>> all(c in string.ascii_letters for c in random_chars())
True



"""
return "".join(random.choices(string.ascii_letters, k=3))


def random_digits() -> str:
"""
create a random string of 3 digits.

>>> len(random_digits()) == 3
True



>>> all(c in string.digits for c in random_digits())
True
"""
return "".join(random.choices(string.digits, k=3))


def encode(code: str) -> str:
"""
Encode a string by shifting the first character to the end and
wrapping it with random padding of 3 letters and 3 digits on each side.

Reference: https://en.wikipedia.org/wiki/Caesar_cipher

>>> len(encode('hello')) == len('hello') + 12
True
>>> len(encode('hi')) == len('hi') + 12
True


"""
if len(code) >= 3:
code = code[1:] + code[0]
code = (
random_chars() + random_digits() + code + random_digits() + random_chars()
)
else:
code = code[::-1]
code = (
random_chars() + random_digits() + code + random_digits() + random_chars()
)
return code


def decode(code: str) -> str:
"""
Decode an encoded string by stripping the random padding and
reversing the character shift.

>>> decode(encode('hello'))
'hello'
>>> decode(encode('hi'))
'hi'
>>> decode(encode('python'))
'python'



"""
code = code[6:-6]
code = code[-1] + code[:-1] if len(code) >= 3 else code[::-1]
return code


if __name__ == "__main__":
code = input("Enter the code: ")
encoded = encode(code)
decoded = decode(encoded)
print(f"Original → {code}")
print(f"Encoded → {encoded}")
print(f"Decoded → {decoded}")