Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Cyborg-Model-Z/216ef375b94e6a100e621253202db6cf to your computer and use it in GitHub Desktop.

Select an option

Save Cyborg-Model-Z/216ef375b94e6a100e621253202db6cf to your computer and use it in GitHub Desktop.
Reverse a sentence
text = "python is fun"
substring = " "
def spaces(text):
return (str[::text.count(" ", 0,100)])
spaces(text)
print (spaces("to sit in solemn silence"))
@Cyborg-Model-Z
Copy link
Author

def reverseWords(text):
backwardsentence = ""
for i in text.split(" ")[-1::-1]:
backwardsentence += str(i) + " "
return (backwardntence)[:-1]
reverseWords("to sit in solemn silence")

@Camsbury

@Camsbury
Copy link

Camsbury commented Jul 6, 2019

def reverse_words(text):
    """ Reverses a string of words by:
    1. Splitting the string into the individual words
    2. Reversing the list of words
    3. Joining the words into a string again
    """
    return " ".join(reversed(text.split(" ")))

@Camsbury
Copy link

Camsbury commented Jul 6, 2019

So you could have:

def join_spaced(items):
    pass

def my_reversed(items):
    pass

def split_words(text):
    pass

def reverse_words(text):
    return join_spaced(my_reversed(split_words(text)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment