Created a ROT13 cipher

Here’s my version of my ROT13 cipher,give me your thoughts, suggestions, optimization

#creating a rot 13 cipher

#first create a string containing a-z

string_ = 'abcdefghijklmnopqrstuvwxyz'

#convert the string to an array

#create a function that takes an alphabet and returns another alphabet that is 13th letter from the given alphabet

def return_alpha():
    str_list = []
    for i in string_:
    return str_list


def check(a,rot):
    forward_index = 1
    str_list = return_alpha()
    extended_list = return_alpha() + return_alpha()
    end_elem = ''
    if a in return_alpha():
        #print(str_list[ str_list.index(a)+1 : rot+str_list.index(a)+1 ][-1])
        end_elem = str_list[ str_list.index(a)+1 : rot+str_list.index(a)+1 ][-1]
        if a in ['n','o','p','q','r','s','t','u','v','w','x','y','z']:
            #print(extended_list[str_list.index(a)+1 : rot+str_list.index(a)+1][-1])
            end_elem = extended_list[str_list.index(a)+1 : rot+str_list.index(a)+1][-1]
    return end_elem


the issue of certain letters of the alphabet going over ‘z’ after rotating by 13 can be solved by converting the chars to ASCII, add 13, and then mod (%) it by 26.

# we start with 'o'
toConvert = 'o'

# ascii of 'o'

# minus the ascii val of 'a' returns you the position of 'o' within the alphabets
convPos = convAscii-ord('a')

print(convPos) # prints 14, where 'a' = 0

# add 13 to rotate, but at this point it is too large
rotate = convPos+13 # rotate right now is 27, where 'z' = 25

# simply mod the value by 26
rotate%=26 # and now we have 1, or 'b'

# to bring the 1 back to a char we add the ord('a') we subtracted earlier, then use chr()

good job.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.