Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- word = "to be or not to be that is the question"
- key = [3, 9, 1]
- def shift(a, k):
- # a is a letter, k is a number
- # ex. shift('b', 2) = 'd'
- # shift('y', 3) = 'b'
- # don't shift non-alphanumeric
- if (not a.isalpha()):
- return a
- # shift the letters ascii value (so a is 0, b is 1, ...)
- i = ord(a) - ord('a')
- i += k
- # if we went past 'z', wrap around
- i %= 26
- # return the letter back at the right ascii value
- return chr(i + ord('a'))
- def polyalphabetic(w, k):
- k_index = 0
- result = []
- for char in w:
- if (not char.isalpha()):
- # char isn't alphanumeric, so we don't shift it at all
- result.append(char)
- continue
- result.append(shift(char, k[k_index]))
- k_index += 1 # go to the next key
- k_index %= len(k) # if we're past the end, go back to 0
- return "".join(result)
- if __name__ == "__main__":
- print polyalphabetic(word, key)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement