Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. https://practice.geeksforgeeks.org/problems/reverse-words-in-a-given-string
  2. def main():
  3.     tests_count = int(input())
  4.     tests = [input() for _ in range(tests_count)]
  5.     for test in tests:
  6.         print(reverse_words_position(test))
  7.        
  8. def reverse_words_position(string):
  9.     reversed_words = []
  10.     if string == "":
  11.         return string
  12.     start_pos = 0
  13.     end_pos = len(string)
  14.     in_word = False
  15.     for index, i in enumerate(string):
  16.         if not in_word and i != ".":
  17.             start_pos = index
  18.             in_word = True
  19.         elif i == ".":
  20.             reversed_words.insert(0, string[start_pos:index])
  21.             in_word = False
  22.         elif index == len(string) - 1:
  23.             reversed_words.insert(0, string[start_pos:index+1])
  24.             in_word = False
  25.            
  26.     return ".".join(reversed_words)
  27.  
  28. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement