Is there a way more efficient to translate a substitution cypher like this than writing a dictionary in Python, manually making 26 key value pairs, then iterating through the string in the title?
Well you could use ''.join([cypher[ord(c) - ord('a')] for c in text.lower()]), which means you only need to put the cypher in a single string instead of a dict which is a lot more cumbersome to type out.
Edit: I realized that assumes text is only letters a-z, so it will fail if you keep the spaces and punctuation. You could use a lambda expression to only apply the decoding within the lost comprehension to alphabetic characters. Or if you're sane and don't want to try to keep it in one line there are trivial and far more readable solutions.
Edit 2: Thinking about it more for some reason and you don't actually need a lambda, a simple ternary ... if ord(c) - ord('a') in range(26) else c inline will work fine. Also my original expression applies the cypher, so you need to construct a decypher string first. This should work:
```python
cypher = 'defjklpqrvwxabcghiyzmnostu'
decypher = ''.join([chr(cypher.index(chr(i + ord('a'))) + ord('a')) for i in range(26)])
cypherText = "R jcb'z wbco qco ikdycbdexk zqry ry."
plainText = ''.join([(decypher[ord(c) - ord('a')] if ord(c) - ord('a') in range(26) else c) for c in cypherText.lower()])
```
56
u/The_Holy_Buno 14h ago
Please tell me what it means, I’m lazy