fork download
  1. from operator import methodcaller
  2. from itertools import takewhile, dropwhile
  3.  
  4. def complement(func):
  5. def wrapper(*args, **kwargs):
  6. return not func(*args, **kwargs)
  7. return wrapper
  8.  
  9. def lengthOfLastWord(s: str) -> int:
  10. space = methodcaller("isspace")
  11. word = takewhile(complement(space), dropwhile(space, s[::-1]))
  12. return len(list(word))
  13.  
  14. print(lengthOfLastWord(" fly me to the moon "))
Success #stdin #stdout 0.03s 9780KB
stdin
Standard input is empty
stdout
4