new file: Lösung Kata Caesar Cipher Helper
This commit is contained in:
parent
2ffebe9db4
commit
444b5dabbd
39
src/caesar_cipher_helper
Normal file
39
src/caesar_cipher_helper
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
Lösung des Katas: Caesar Cipher Helper
|
||||
Link: https://www.codewars.com/kata/526d42b6526963598d0004db
|
||||
"""
|
||||
|
||||
|
||||
beginning_alphabet = ord("A")
|
||||
ending_alphabet = ord("Z")
|
||||
|
||||
class CaesarCipher(object):
|
||||
def __init__(self, offset):
|
||||
self.offset = offset
|
||||
|
||||
def encode(self, st):
|
||||
upper = st.upper()
|
||||
encoded_st = ""
|
||||
for char in upper:
|
||||
if beginning_alphabet <= ord(char) <= ending_alphabet:
|
||||
encoded_char = (ord(char) + self.offset)
|
||||
if encoded_char > ending_alphabet:
|
||||
encoded_char -= (ending_alphabet - (beginning_alphabet - 1))
|
||||
encoded_st += chr(encoded_char)
|
||||
else:
|
||||
encoded_st += char
|
||||
|
||||
return encoded_st
|
||||
|
||||
def decode(self, st):
|
||||
decoded_st = ""
|
||||
for char in st:
|
||||
if beginning_alphabet <= ord(char) <= ending_alphabet:
|
||||
decoded_char = (ord(char) - self.offset)
|
||||
if decoded_char < beginning_alphabet:
|
||||
decoded_char += (ending_alphabet - (beginning_alphabet - 1))
|
||||
decoded_st += chr(decoded_char)
|
||||
else:
|
||||
decoded_st += char
|
||||
|
||||
return decoded_st
|
||||
Loading…
x
Reference in New Issue
Block a user