Code a function, ceasar, that takes two arguments: string and pos.
Your task is to shift all letters by pos.
if pos is 1,
E.g. a -> b, z->a
pos can be negative
if pos is -2,
E.g. a -> y
Keep all other characters as it is.
def ceasar(string,pos):
ans = ""
if pos<0:
pos=26+pos%26
for ch in string:
if ch.isupper():
ans += chr((ord(ch) + pos-65) % 26 + 65)
elif ch.islower():
ans += chr((ord(ch) + pos-97) % 26 + 97)
else: ans+=ch
return ans
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("Positive pos")
def test_case():
test.assert_equals(ceasar("Something",1),"Tpnfuijoh")
test.assert_equals(ceasar("iTs G0nn4 b3 l1ke th4t huh?",4),"mXw K0rr4 f3 p1oi xl4x lyl?")
@test.it("Negative pos")
def test_case():
test.assert_equals(ceasar("Batteries?",-2), "Zyrrcpgcq?")