diff --git a/strings/Secret_language.py b/strings/Secret_language.py new file mode 100644 index 000000000000..7d090439a3d8 --- /dev/null +++ b/strings/Secret_language.py @@ -0,0 +1,90 @@ +# Encoding & Decoding + +import random +import string + + +def random_chars() -> str: + """ + Generate a random string of 3 ASCII letters. + + >>> import random + >>> random.seed(42) + >>> random_chars() + 'ZoX' + """ + return "".join(random.choices(string.ascii_letters, k=3)) + + +def random_digits() -> str: + """ + Generate a random string of 3 digits. + + >>> import random + >>> random.seed(42) + >>> random_digits() + '638' + """ + 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 + + >>> import random + >>> random.seed(42) + >>> encode('hello') + 'ZoX638elloh415mJu' + + >>> import random + >>> random.seed(42) + >>> encode('hi') + 'ZoX638ih415mJu' + """ + 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. + + >>> import random + >>> random.seed(42) + >>> decode(encode('hello')) + 'hello' + + >>> import random + >>> random.seed(42) + >>> decode(encode('hi')) + 'hi' + """ + code = code[6:-6] + if len(code) >= 3: + code = code[-1] + code[:-1] + else: + code = 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}") diff --git a/strings/camel_case_to_snake_case.py b/strings/camel_case_to_snake_case.py index 582907be2edb..a01b8e4c06bb 100644 --- a/strings/camel_case_to_snake_case.py +++ b/strings/camel_case_to_snake_case.py @@ -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' diff --git a/strings/frequency_finder.py b/strings/frequency_finder.py index 98720dc36d6e..e9da49799cc8 100644 --- a/strings/frequency_finder.py +++ b/strings/frequency_finder.py @@ -36,6 +36,9 @@ def get_letter_count(message: str) -> dict[str, int]: + """get_letter_count() is a function that takes message as parameter which is + supposed to be the string. and it returns a dictionary where string is a key + and integer is a value.""" letter_count = dict.fromkeys(string.ascii_uppercase, 0) for letter in message.upper(): if letter in LETTERS: @@ -45,6 +48,7 @@ def get_letter_count(message: str) -> dict[str, int]: def get_item_at_index_zero(x: tuple) -> str: + """It takes x as parameter which is tuple and returns a string.""" return x[0]