Guest User

Untitled

a guest
Mar 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. """
  2. Problem 4. We define words as sequences of non-space characters separated by a single space.
  3.  
  4. Write a function last_word(s) that returns the last word in s
  5.  
  6. Further, write a main() function that loads an integer n and then n strings which it prints sorted by the last word.
  7. """
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14. def last_word(s):
  15. if s=="" or s[-1]==" ":
  16. return ""
  17. n=len(s)-1
  18. while n>=0:
  19. if s[n]==" ":
  20. position=n
  21. break
  22. n-=1
  23. else:
  24. return s
  25. return s[n+1:]
  26.  
  27. print(last_word("Panionios is the greatest team"))
  28.  
  29.  
  30. def main():
  31. n=int(input("Enter a natural number: "))
  32. while n<=0:
  33. n=int(input("Invalid entry. Please enter a natural number: "))
  34. L=[]
  35. for i in range(n):
  36. L.append(input())
  37. return sorted(L,key=lambda t: last_word(t))
  38. s=main()
  39. print(s)
Add Comment
Please, Sign In to add comment