Romeech

toggle_case.py

Dec 25th, 2017
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. import sys
  2.  
  3.  
  4. def to_lower(s):
  5.     return s.lower()
  6.  
  7.  
  8. def to_upper(s):
  9.     return s.upper()
  10.  
  11.  
  12. def process_chr(s, fn=to_upper):
  13.     if len(s) == 0:
  14.         return ""
  15.  
  16.     char = s[0]
  17.     if char.isalpha():
  18.         return fn(char) + process_chr(s[1:], to_lower if fn == to_upper else to_upper)
  19.     elif char in (".", "!", "?"):
  20.         return char + process_chr(s[1:], to_upper)
  21.     else:
  22.         return char + process_chr(s[1:], fn)
  23.  
  24.  
  25. if __name__ == '__main__':
  26.     print(process_chr(sys.argv[1]))
Add Comment
Please, Sign In to add comment