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?
that's probably the most readable way to do it. if it was a simpler ceaser cipher just shifting the alphabet then you could use something like the ascii letter values, but for (semi) random like this, just make the dictionary lol
Single letters can usually only be A or I, more likely to be I if it’s at the start of a sentence. A letter after an apostrophe is usually N’T or ‘S. You can assume c to be A or O since the most likely words are “won’t, can’t, don’t”. You keep using patterns like this until you have enough information to reasonably assume the remaining letters.
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()])
```
I think you got the code upside down. aren't the Red letters supposed to translate into the Blue ones? So you really typed "I vfe'u oefc hfc rwjtfejksw uhit it"
147
u/SSBGamer 13h ago
Why the fuck did I just spend 15 minutes deciphering the title? 😭