Thursday, February 29, 2024
HomeSoftware EngineeringLearn how to Resolve Caesar Cipher Helper in Python

Learn how to Resolve Caesar Cipher Helper in Python


The problem

Write a category that, when given a string, will return an uppercase string with every letter shifted ahead within the alphabet by nevertheless many spots the cipher was initialized to.

Instance:

c = CaesarCipher(5); # creates a CipherHelper with a shift of 5
c.decode('BFKKQJX') # returns 'WAFFLES'

If one thing within the string will not be within the alphabet (e.g. punctuation, areas), merely depart it as is.
The shift will at all times be within the vary of [1, 26].

The answer in Python code

Choice 1:

from string import maketrans

class CaesarCipher(object):
    def __init__(self, shift):
        self.alpha = "abcdefghijklmnopqrstuvwxyz".higher()
        self.newalpha = self.alpha[shift:] + self.alpha[:shift]

    def encode(self, str):
        t = maketrans(self.alpha, self.newalpha)
        return str.higher().translate(t)

    def decode(self, str):
        t = maketrans(self.newalpha, self.alpha)
        return str.higher().translate(t)

Choice 2:

class CaesarCipher(object):
    def __init__(self, shift):
        self.s=shift
        move;

    def encode(self, str):
        return ''.be a part of(chr((ord(i)+self.s-ord('A'))%26+ord('A')) if i.isalpha() else i for i in str.higher())
        
    def decode(self, str):
        return ''.be a part of(chr((ord(i)-self.s+ord('A'))%26+ord('A')) if i.isalpha() else i for i in str.higher())

Choice 3:

class CaesarCipher(object):
    
    def __init__(self, shift):
        abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        self.enc_dict = dict(zip(abc, abc[shift:] + abc[:shift]))
        self.dec_dict = dict(zip(abc[shift:] + abc[:shift], abc, ))
    
    def encode(self, str):
        return ''.be a part of( self.enc_dict.get(c, c) for c in str.higher() )
    
    def decode(self, str):
        return ''.be a part of( self.dec_dict.get(c, c) for c in str.higher() )

Take a look at instances to validate our resolution

check.describe("Testing the CaesarCipher class");
check.it("Shift of 5");
    
c =  CaesarCipher(5);

check.assert_equals(c.encode('WAFFLES'), 'BFKKQJX');
check.assert_equals(c.decode('BFKKQJX'), 'WAFFLES');
check.assert_equals(c.encode("IT'S A SHIFT CIPHER!!"), "NY'X F XMNKY HNUMJW!!");
check.assert_equals(c.decode("NY'X F XMNKY HNUMJW!!"), "IT'S A SHIFT CIPHER!!");

check.it("Shift of 13");

c =  CaesarCipher(13);
check.assert_equals(c.encode('CNAPNXRF'), 'PANCAKES');
check.assert_equals(c.decode('PANCAKES'), 'CNAPNXRF');
check.assert_equals(c.encode('JAVASCRIPT'), 'WNINFPEVCG');
check.assert_equals(c.decode('WNINFPEVCG'), 'JAVASCRIPT');

from random import pattern
import string
for i in vary(0, 5):
    rs = ''.be a part of(pattern(string.punctuation + string.ascii_uppercase, 10));
    check.assert_equals(c.decode(c.encode(rs)), rs);

check.it("Shift of 26");
c = CaesarCipher(26);
    
for i in vary(0, 7):
    rs = ''.be a part of(pattern(string.punctuation + string.ascii_uppercase, 15));
    check.assert_equals(c.decode(rs), rs);
    check.assert_equals(c.encode(rs), rs);
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments