-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
RNA Transcription and Atbash Cipher approach cleanup #4191
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,21 +1,24 @@ | ||
| { | ||
| "introduction": { | ||
| "authors": ["safwansamsudeen"] | ||
| "authors": ["safwansamsudeen"], | ||
| "contributors": ["yrahcaz7"] | ||
| }, | ||
| "approaches": [ | ||
| { | ||
| "uuid": "920e6d08-e8fa-4bef-b2f4-837006c476ae", | ||
| "slug": "mono-function", | ||
| "title": "Mono-function", | ||
| "blurb": "Use one function for both tasks", | ||
| "authors": ["safwansamsudeen"] | ||
| "authors": ["safwansamsudeen"], | ||
| "contributors": ["yrahcaz7"] | ||
| }, | ||
| { | ||
| "uuid": "9a7a17e0-4ad6-4d97-a8b9-c74d47f3e000", | ||
| "slug": "separate-functions", | ||
| "title": "Separate Functions", | ||
| "title": "Separate functions", | ||
| "blurb": "Use separate functions, and perhaps helper ones", | ||
| "authors": ["safwansamsudeen"] | ||
| "authors": ["safwansamsudeen"], | ||
| "contributors": ["yrahcaz7"] | ||
| } | ||
| ] | ||
| } |
44 changes: 27 additions & 17 deletions
44
exercises/practice/atbash-cipher/.approaches/introduction.md
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
58 changes: 33 additions & 25 deletions
58
exercises/practice/atbash-cipher/.approaches/mono-function/content.md
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 |
|---|---|---|
| @@ -1,46 +1,54 @@ | ||
| ## Approach: Mono-function | ||
| Notice that there the majority of the code is repetitive? | ||
| A fun way to solve this would be to keep it all inside the `encode` function, and merely chunk it if `decode` is False: | ||
| For variation, this approach shows a different way to translate the text. | ||
| # Approach: Mono-function | ||
|
|
||
| Notice that the majority of the code is repetitive? | ||
| A fun way to solve this would be to keep it all inside the `encode()` function, and merely chunk it if `decode` is `False`: | ||
| For variation, this approach also shows a different way to translate the text. | ||
|
|
||
| ```python | ||
| from string import ascii_lowercase as asc_low | ||
|
|
||
| ENCODING = {chr: asc_low[id] for id, chr in enumerate(asc_low[::-1])} | ||
|
|
||
| def encode(text: str, decode: bool = False): | ||
| res = "".join(ENCODING.get(chr, chr) for chr in text.lower() if chr.isalnum()) | ||
| return res if decode else " ".join(res[index:index+5] for index in range(0, len(res), 5)) | ||
| def encode(text, decode = False): | ||
| line = "".join(ENCODING.get(chr, chr) for chr in text.lower() if chr.isalnum()) | ||
| return line if decode else " ".join(line[index:index+5] for index in range(0, len(line), 5)) | ||
|
|
||
| def decode(text: str): | ||
| def decode(text): | ||
| return encode(text, True) | ||
| ``` | ||
| To explain the translation: we use a `dict` comprehension in which we reverse the ASCII lowercase digits, and enumerate through them - that is, `z` is 0, `y` is 1, and so on. | ||
| We access the character at that index and set it to the value of `c` - so `z` translates to `a`. | ||
|
|
||
| In the calculation of the result, we try to obtain the value of the character using `dict.get`, which accepts a default parameter. | ||
| In this case, the character itself is the default - that is, numbers won't be found in the translation key, and thus should remain as numbers. | ||
| Here, we use a dictionary comprehension in which we reverse the order of the ASCII lowercase digits and enumerate through them — that is, `z` is at index 0, `y` is at index 1, and so on. | ||
| For each code point, we set the value of `chr` in the resulting dictionary to the code point at the respective index — so `z` translates to `a`. | ||
|
|
||
| In the calculation of the result, we try to obtain the value of the code point using `dict.get()`, which accepts a default parameter. | ||
| In this case, the code point itself is the default — that is, numbers won't be found in the translation key, and thus should remain as numbers. | ||
|
|
||
| We use a [conditional expression (also known as a ternary operator)][conditional-expression] to check if we actually mean to decode the function, in which case we return the result as is. | ||
| If not, we "chunk" the result by joining every five code points with a space. | ||
|
|
||
| We use a [ternary operator][ternary-operator] to check if we actually mean to decode the function, in which case we return the result as is. | ||
| If not, we chunk the result by joining every five characters with a space. | ||
| Another possible way to solve this would be to use a function that returns another function (_a higher-order function or [closure][closure]_) that encodes or decodes based on the outer function's parameter: | ||
|
|
||
| Another possible way to solve this would be to use a function that returns a function that encodes or decodes based on the parameters: | ||
| ```python | ||
| from string import ascii_lowercase as alc | ||
| from string import ascii_lowercase as asc_low | ||
|
|
||
| lowercase = {chr: alc[id] for id, chr in enumerate(alc[::-1])} | ||
| ENCODING = {chr: asc_low[id] for id, chr in enumerate(asc_low[::-1])} | ||
|
|
||
| def code(decode=False): | ||
| def code(decode = False): | ||
| def func(text): | ||
| line = "".join(lowercase.get(chr, chr) for chr in text.lower() if chr.isalnum()) | ||
| line = "".join(ENCODING.get(chr, chr) for chr in text.lower() if chr.isalnum()) | ||
| return line if decode else " ".join(line[index:index+5] for index in range(0, len(line), 5)) | ||
| return func | ||
|
|
||
|
|
||
| encode = code() | ||
| decode = code(True) | ||
| ``` | ||
| The logic is the same - we've instead used one function that generates two _other_ functions based on the boolean value of its parameter. | ||
| `encode` is set to the function that's returned, and performs encoding. | ||
| `decode` is set a function that _decodes_. | ||
|
|
||
| [ternary-operator]: https://www.tutorialspoint.com/ternary-operator-in-python | ||
| [decorator]: https://realpython.com/primer-on-python-decorators/ | ||
| The logic is the same — the only change is that now we use use one function that generates two _other_ functions based on the boolean value of its parameter. | ||
|
|
||
| Here, we first call `code()` with no argument and set `encode` to the function that's returned, which performs encoding. | ||
| Then we call `code(True)` to get the decoding version of the function and set `decode` to that function. | ||
|
|
||
| After that, we can call `encode()` and `decode()` as normal, and both functions successfully perform their indended task. | ||
|
|
||
| [closure]: https://realpython.com/python-closure/ | ||
| [conditional-expression]: https://docs.python.org/3/reference/expressions.html#conditional-expressions | ||
8 changes: 4 additions & 4 deletions
8
exercises/practice/atbash-cipher/.approaches/mono-function/snippet.txt
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 |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| from string import ascii_lowercase as asc_low | ||
| ENCODING = {chr: asc_low[id] for id, chr in enumerate(asc_low[::-1])} | ||
|
|
||
| def encode(text: str, decode: bool = False): | ||
| res = "".join(ENCODING.get(chr, chr) for chr in text.lower() if chr.isalnum()) | ||
| return res if decode else " ".join(res[index:index+5] for index in range(0, len(res), 5)) | ||
| def decode(text: str): | ||
| def encode(text, decode = False): | ||
| line = "".join(ENCODING.get(chr, chr) for chr in text.lower() if chr.isalnum()) | ||
| return line if decode else " ".join(line[index:index+5] for index in range(0, len(line), 5)) | ||
| def decode(text): | ||
| return encode(text, True) |
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
4 changes: 2 additions & 2 deletions
4
exercises/practice/atbash-cipher/.approaches/separate-functions/snippet.txt
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 |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| from string import ascii_lowercase | ||
| ENCODING = str.maketrans(ascii_lowercase, ascii_lowercase[::-1]) | ||
|
|
||
| def encode(text: str): | ||
| def encode(text): | ||
| res = "".join(chr for chr in text.lower() if chr.isalnum()).translate(ENCODING) | ||
| return " ".join(res[index:index+5] for index in range(0, len(res), 5)) | ||
| def decode(text: str): | ||
| def decode(text): | ||
| return "".join(chr.lower() for chr in text if not chr.isspace()).translate(ENCODING) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.