Advertisement
makispaiktis

Reverse the 5-letter words in a phrase

Oct 25th, 2019 (edited)
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. # 1. Defining a function for finding the spacebar characters in a word/phrase
  2. def findSpacesInString(str):
  3.     # The next list will have the indexes of spacebar-chars in my phrase-string
  4.     spacesList = [-1]
  5.     for i in range(0, len(str)):
  6.         if str[i] == ' ':
  7.             spacesList.append(i)
  8.  
  9.     # If the last character of my string "str" is not the spacebar-char, then I will
  10.     # add the (last index + 1) in my spacebar list
  11.     if str[len(str)-1] != ' ':
  12.         spacesList.append(len(str))
  13.  
  14.     return spacesList
  15.  
  16.  
  17. # 2. Defining a function that breaks the phrase in single words
  18. def findSingleWords(str):
  19.     spacesList = findSpacesInString(str)
  20.     words = []
  21.     word = ""
  22.     for i in range (0, len(spacesList)-1):
  23.         # Example: word = "partnership" ---->  word[2:6] = "rtne"
  24.         word = str[(spacesList[i]+1) : spacesList[i+1]]
  25.         words.append(word)
  26.  
  27.     return words
  28.  
  29.  
  30. # 3. Defining a function that creates the final string/phrase
  31. def reverseWhenNecessary(str):
  32.     words = findSingleWords(str)                # words = list with strings (every string is a word)
  33.     finalString = ""
  34.     for word in words:# 1. Defining a function for finding the spacebar characters in a word/phrase
  35. def findSpacesInString(str):
  36.     # The next list will have the indexes of spacebar-chars in my phrase-string
  37.     spacesList = [-1]
  38.     for i in range(0, len(str)):
  39.         if str[i] == ' ':
  40.             spacesList.append(i)
  41.  
  42.     # If the last character of my string "str" is not the spacebar-char, then I will
  43.     # add the (last index + 1) in my spacebar list
  44.     if str[len(str)-1] != ' ':
  45.         spacesList.append(len(str))
  46.  
  47.     return spacesList
  48.  
  49.  
  50. # 2. Defining a function that breaks the phrase in single words
  51. def findSingleWords(str):
  52.     spacesList = findSpacesInString(str)
  53.     words = []
  54.     word = ""
  55.     for i in range (0, len(spacesList)-1):
  56.         # Example: word = "partnership" ---->  word[2:6] = "rtne"
  57.         word = str[(spacesList[i]+1) : spacesList[i+1]]
  58.         words.append(word)
  59.  
  60.     return words
  61.  
  62.  
  63. # 3. Defining a function that creates the final string/phrase
  64. def reverseWhenNecessary(str):
  65.     words = findSingleWords(str)                # words = list with strings (every string is a word)
  66.     finalString = ""
  67.     for word in words:
  68.         # If length >=5, I will put in my string my word BUT REVERSED
  69.         # and then I will also add a spacebar/char, because it's another single word following
  70.         if len(word) >= 5:
  71.             # When I reverser a string, such as: str= "hello", I write:
  72.             finalString += word[::-1] + ' '
  73.         else:
  74.             finalString += word + ' '
  75.  
  76.         # At the end of this process, I will have at the end one more spacebar/character(' ')
  77.         finalString = finalString[0:len(finalString)-1]
  78.         return finalString
  79.  
  80.  
  81. # MAIN FUNCTION OF THE PROGRAMME
  82. phrase = "Hello my friend"
  83. print(findSpacesInString(phrase))
  84. print(findSingleWords(phrase))
  85. print(reverseWhenNecessary(phrase))
  86.         # If length >=5, I will put in my string my word BUT REVERSED
  87.         # and then I will also add a spacebar/char, because it's another single word following
  88.         if len(word) >= 5:
  89.             # When I reverser a string, such as: str= "hello", I write:
  90.             finalString += word[::-1] + ' '
  91.         else:
  92.             finalString += word + ' '
  93.  
  94.         # At the end of this process, I will have at the end one more spacebar/character(' ')
  95.         finalString = finalString[0:len(finalString)-1]
  96.         return finalString
  97.  
  98.  
  99. # MAIN FUNCTION OF THE PROGRAMME
  100. phrase = "Hello my friend"
  101. print(findSpacesInString(phrase))
  102. print(findSingleWords(phrase))
  103. print(reverseWhenNecessary(phrase))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement