Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- write a method encrypt(String word) that encrypts a word by doing the following algorithm:
- - remove the middle letter of the input word and append it to the encrypted word.
- - if the length of the input word is even, then take the letter on the left of the two median values.
- - repeat until there are no letters left in `word`.
- example:
- hello -> lelho
- world -> rolwd
- FOP -> OFP
- explaination:
- normal "hello" "helo" "hlo" "ho" "o" ""
- encrypted "" "l" "le" "lel" "lelh" "lelho"
- def encrypt(w):
- s = ''
- while w:
- l = (len(w) - 1) // 2
- s += w[l]
- w = w[:l] + w[l + 1:]
- return s
- def decrypt(w):
- s = [w[-1]]
- w = w[:-1]
- while w:
- l = (len(s)) // 2
- s.insert(l, w[-1])
- w = w[:-1]
- return ''.join(s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement