Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Problem 4. We define words as sequences of non-space characters separated by a single space.
- Write a function last_word(s) that returns the last word in s
- Further, write a main() function that loads an integer n and then n strings which it prints sorted by the last word.
- """
- def last_word(s):
- if s=="" or s[-1]==" ":
- return ""
- n=len(s)-1
- while n>=0:
- if s[n]==" ":
- position=n
- break
- n-=1
- else:
- return s
- return s[n+1:]
- print(last_word("Panionios is the greatest team"))
- def main():
- n=int(input("Enter a natural number: "))
- while n<=0:
- n=int(input("Invalid entry. Please enter a natural number: "))
- L=[]
- for i in range(n):
- L.append(input())
- return sorted(L,key=lambda t: last_word(t))
- s=main()
- print(s)
Add Comment
Please, Sign In to add comment