Compare commits
No commits in common. "master" and "uebung1" have entirely different histories.
@ -1,39 +0,0 @@
|
|||||||
"""
|
|
||||||
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