-
-
Notifications
You must be signed in to change notification settings - Fork 50.8k
Add secret language encoding and decoding algorithm #14837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+96
−2
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d62ebf7
corrected the typos
vivek2004-sec 4e9b21e
added the docstrings in the functions
vivek2004-sec ea72a2c
added new file of secret_language
vivek2004-sec cb237ed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cb408bf
resolved all the issues
vivek2004-sec 714b8b9
made some changes
vivek2004-sec 4115d68
modified the file
vivek2004-sec 9590b2d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
|
||
|
|
||
| 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: | ||
|
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
|
||
| 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}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 functionrandom_chars