Skip to content

Optimized: Code optimized in 'brute_force_caesar_cipher.py' #11930

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
wants to merge 2 commits into from
Closed
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
20 changes: 8 additions & 12 deletions ciphers/brute_force_caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import string

Check failure on line 1 in ciphers/brute_force_caesar_cipher.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

ciphers/brute_force_caesar_cipher.py:1:8: F401 `string` imported but unused


def decrypt(message: str) -> None:
Expand Down Expand Up @@ -31,23 +31,19 @@
Decryption using Key #24: VOFGVWZ ROFXW
Decryption using Key #25: UNEFUVY QNEWV
"""
for key in range(len(string.ascii_uppercase)):
translated = ""
for key in range(alphabet_len):

Check failure on line 34 in ciphers/brute_force_caesar_cipher.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

ciphers/brute_force_caesar_cipher.py:34:22: F821 Undefined name `alphabet_len`
translated = []
for symbol in message:
if symbol in string.ascii_uppercase:
num = string.ascii_uppercase.find(symbol)
num = num - key
if num < 0:
num = num + len(string.ascii_uppercase)
translated = translated + string.ascii_uppercase[num]
if symbol in alphabet:

Check failure on line 37 in ciphers/brute_force_caesar_cipher.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

ciphers/brute_force_caesar_cipher.py:37:26: F821 Undefined name `alphabet`
num = alphabet.find(symbol) - key

Check failure on line 38 in ciphers/brute_force_caesar_cipher.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

ciphers/brute_force_caesar_cipher.py:38:23: F821 Undefined name `alphabet`
translated.append(alphabet[num % alphabet_len])

Check failure on line 39 in ciphers/brute_force_caesar_cipher.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

ciphers/brute_force_caesar_cipher.py:39:35: F821 Undefined name `alphabet`

Check failure on line 39 in ciphers/brute_force_caesar_cipher.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

ciphers/brute_force_caesar_cipher.py:39:50: F821 Undefined name `alphabet_len`
else:
translated = translated + symbol
print(f"Decryption using Key #{key}: {translated}")
translated.append(symbol)
print(f"Decryption using Key #{key}: {''.join(translated)}")


def main() -> None:
message = input("Encrypted message: ")
message = message.upper()
message = input("Encrypted message: ").upper()
decrypt(message)


Expand Down
Loading