Advertisement
Guest User

FOPviva

a guest
Nov 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. write a method encrypt(String word) that encrypts a word by doing the following algorithm:
  2.  
  3.     - remove the middle letter of the input word and append it to the encrypted word.
  4.     - if the length of the input word is even, then take the letter on the left of the two median values.
  5.     - repeat until there are no letters left in `word`.
  6.  
  7.  
  8. example:
  9.  
  10. hello    ->    lelho
  11. world    ->    rolwd
  12. FOP      ->    OFP
  13.  
  14. explaination:
  15.  
  16. normal      "hello"     "helo"      "hlo"       "ho"        "o"         ""
  17. encrypted   ""          "l"         "le"        "lel"       "lelh"      "lelho"
  18.  
  19.  
  20. def encrypt(w):
  21.     s = ''
  22.     while w:
  23.         l = (len(w) - 1) // 2
  24.         s += w[l]
  25.         w = w[:l] + w[l + 1:]
  26.     return s
  27.  
  28. def decrypt(w):
  29.     s = [w[-1]]
  30.     w = w[:-1]
  31.     while w:
  32.         l = (len(s)) // 2
  33.         s.insert(l, w[-1])
  34.         w = w[:-1]
  35.     return ''.join(s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement