Skip to content

Commit

Permalink
Merge pull request #1194 from KraVLonE/patch-3
Browse files Browse the repository at this point in the history
Update caesar_cipher.py
  • Loading branch information
pnhofmann authored Sep 4, 2024
2 parents 165b22a + 41d1415 commit db74bb7
Showing 1 changed file with 67 additions and 60 deletions.
127 changes: 67 additions & 60 deletions jarviscli/plugins/caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
from colorama import Fore

from plugin import plugin


@plugin("caesar cipher")
def caesar_cipher_converter(jarvis, s):
"""Main function that handles the Caesar cipher plugin."""
option = get_option(jarvis)
if option == 1:
plain_to_cipher(jarvis)
elif option == 2:
cipher_to_plain(jarvis)
else:
return


def get_option(jarvis):
"""
Presents options to the user and returns their choice.
Args:
jarvis: The Jarvis assistant instance.
Returns:
option (int or None): 1 for plain-to-cipher, 2 for cipher-to-plain,
None for exit.
"""
jarvis.say("~> What can I do for you?", Fore.RED)
print("1: Convert plain text to cipher")
print("2: Convert cipher to plain text")
Expand All @@ -23,81 +31,80 @@ def get_option(jarvis):

while True:
try:
# Get user input and convert it to an integer
option = int(jarvis.input("Enter your choice: ", Fore.GREEN))
if option == 3:
return
elif option == 1 or option == 2:
return option
return None # Exit option
elif option in [1, 2]:
return option # Valid options
else:
jarvis.say(
"Invalid input! Enter a number from the choices provided.", Fore.YELLOW)
jarvis.say("Invalid input! Enter a number from the choices provided.", Fore.YELLOW)
except ValueError:
jarvis.say(
"Invalid input! Enter a number from the choices provided.", Fore.YELLOW)
# Handle cases where the input isn't a valid integer
jarvis.say("Invalid input! Enter a number from the choices provided.", Fore.YELLOW)
print()


def plain_to_cipher(jarvis):
user_input = get_user_input(jarvis)
converted = ""
def caesar_cipher(text, shift):
"""
Converts the text using a Caesar cipher with the specified shift.
for i in user_input:
if is_ascii(i):
if i.isalpha():
if i.isupper():
converted += chr((ord(i) - 68) % 26 + 65)
else:
converted += chr((ord(i) - 100) % 26 + 97)
else:
converted += i
Args:
text (str): The input string to be converted.
shift (int): The number of positions to shift each letter.
Returns:
converted (str): The resulting converted string.
"""
converted = ""
for char in text:
if char.isalpha():
# Determine starting ASCII code (A/a) based on case
start = ord('A') if char.isupper() else ord('a')
# Perform the shift within the bounds of the alphabet
converted += chr((ord(char) - start + shift) % 26 + start)
else:
x = ord(i)
if 192 <= x <= 255:
converted += chr((ord(i) - 195) % 63 + 192)
else:
converted += i
converted += char # Non-alphabetic characters are not shifted
return converted

jarvis.say(converted, Fore.YELLOW)

def plain_to_cipher(jarvis):
"""
Converts plain text to Caesar cipher text with a shift of 3.
def is_ascii(s):
return all(ord(c) < 128 for c in s)
Args:
jarvis: The Jarvis assistant instance.
"""
user_input = get_user_input(jarvis)
converted = caesar_cipher(user_input, 3)
jarvis.say(converted, Fore.YELLOW)


def cipher_to_plain(jarvis):
user_input = get_user_input(jarvis)
converted = ""

for i in user_input:
if is_ascii(i):
if i.isalpha():
if i.isupper():
converted += chr((ord(i) - 62) % 26 + 65)
else:
converted += chr((ord(i) - 94) % 26 + 97)
else:
converted += i
else:
x = ord(i)
if 192 <= x <= 255:
converted += chr((ord(i) - 189) % 63 + 192)
else:
converted += i
"""
Converts Caesar cipher text back to plain text with a shift of -3.
Args:
jarvis: The Jarvis assistant instance.
"""
user_input = get_user_input(jarvis)
converted = caesar_cipher(user_input, -3)
jarvis.say(converted, Fore.YELLOW)


def get_user_input(jarvis):
while True:
try:
user_input = jarvis.input("Enter string to convert: ")
if len(user_input) > 0:
return user_input
else:
jarvis.say(
"String length should be minimum 1.", Fore.YELLOW)
except ValueError:
jarvis.say("Sorry, I didn't understand that.", Fore.RED)
continue
"""
Prompts the user to enter a string for conversion.
return
Args:
jarvis: The Jarvis assistant instance.
Returns:
user_input (str): The string entered by the user.
"""
while True:
user_input = jarvis.input("Enter string to convert: ")
if len(user_input) > 0:
return user_input # Return the input if it's non-empty
else:
jarvis.say("String length should be minimum 1.", Fore.YELLOW)

0 comments on commit db74bb7

Please sign in to comment.