Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1. Defining a function for finding the spacebar characters in a word/phrase
- def findSpacesInString(str):
- # The next list will have the indexes of spacebar-chars in my phrase-string
- spacesList = [-1]
- for i in range(0, len(str)):
- if str[i] == ' ':
- spacesList.append(i)
- # If the last character of my string "str" is not the spacebar-char, then I will
- # add the (last index + 1) in my spacebar list
- if str[len(str)-1] != ' ':
- spacesList.append(len(str))
- return spacesList
- # 2. Defining a function that breaks the phrase in single words
- def findSingleWords(str):
- spacesList = findSpacesInString(str)
- words = []
- word = ""
- for i in range (0, len(spacesList)-1):
- # Example: word = "partnership" ----> word[2:6] = "rtne"
- word = str[(spacesList[i]+1) : spacesList[i+1]]
- words.append(word)
- return words
- # 3. Defining a function that creates the final string/phrase
- def reverseWhenNecessary(str):
- words = findSingleWords(str) # words = list with strings (every string is a word)
- finalString = ""
- for word in words:# 1. Defining a function for finding the spacebar characters in a word/phrase
- def findSpacesInString(str):
- # The next list will have the indexes of spacebar-chars in my phrase-string
- spacesList = [-1]
- for i in range(0, len(str)):
- if str[i] == ' ':
- spacesList.append(i)
- # If the last character of my string "str" is not the spacebar-char, then I will
- # add the (last index + 1) in my spacebar list
- if str[len(str)-1] != ' ':
- spacesList.append(len(str))
- return spacesList
- # 2. Defining a function that breaks the phrase in single words
- def findSingleWords(str):
- spacesList = findSpacesInString(str)
- words = []
- word = ""
- for i in range (0, len(spacesList)-1):
- # Example: word = "partnership" ----> word[2:6] = "rtne"
- word = str[(spacesList[i]+1) : spacesList[i+1]]
- words.append(word)
- return words
- # 3. Defining a function that creates the final string/phrase
- def reverseWhenNecessary(str):
- words = findSingleWords(str) # words = list with strings (every string is a word)
- finalString = ""
- for word in words:
- # If length >=5, I will put in my string my word BUT REVERSED
- # and then I will also add a spacebar/char, because it's another single word following
- if len(word) >= 5:
- # When I reverser a string, such as: str= "hello", I write:
- finalString += word[::-1] + ' '
- else:
- finalString += word + ' '
- # At the end of this process, I will have at the end one more spacebar/character(' ')
- finalString = finalString[0:len(finalString)-1]
- return finalString
- # MAIN FUNCTION OF THE PROGRAMME
- phrase = "Hello my friend"
- print(findSpacesInString(phrase))
- print(findSingleWords(phrase))
- print(reverseWhenNecessary(phrase))
- # If length >=5, I will put in my string my word BUT REVERSED
- # and then I will also add a spacebar/char, because it's another single word following
- if len(word) >= 5:
- # When I reverser a string, such as: str= "hello", I write:
- finalString += word[::-1] + ' '
- else:
- finalString += word + ' '
- # At the end of this process, I will have at the end one more spacebar/character(' ')
- finalString = finalString[0:len(finalString)-1]
- return finalString
- # MAIN FUNCTION OF THE PROGRAMME
- phrase = "Hello my friend"
- print(findSpacesInString(phrase))
- print(findSingleWords(phrase))
- print(reverseWhenNecessary(phrase))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement