Skip to content
Closed
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
90 changes: 90 additions & 0 deletions strings/Secret_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Encoding & Decoding

Check failure on line 1 in strings/Secret_language.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (N999)

strings/Secret_language.py:1:1: N999 Invalid module name: 'Secret_language'

Check failure on line 1 in strings/Secret_language.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (N999)

strings/Secret_language.py:1:1: N999 Invalid module name: 'Secret_language'

import random
import string


def random_chars() -> str:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file strings/Secret_language.py, please provide doctest for the function random_chars

"""
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:
Comment thread
vivek2004-sec marked this conversation as resolved.
"""
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]

Check failure on line 80 in strings/Secret_language.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (SIM108)

strings/Secret_language.py:77:5: SIM108 Use ternary operator `code = code[-1] + code[:-1] if len(code) >= 3 else code[::-1]` instead of `if`-`else`-block help: Replace `if`-`else`-block with `code = code[-1] + code[:-1] if len(code) >= 3 else code[::-1]`

Check failure on line 80 in strings/Secret_language.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (SIM108)

strings/Secret_language.py:77:5: SIM108 Use ternary operator `code = code[-1] + code[:-1] if len(code) >= 3 else code[::-1]` instead of `if`-`else`-block help: Replace `if`-`else`-block with `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}")
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
4 changes: 4 additions & 0 deletions strings/frequency_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]


Expand Down
Loading