Skip to content

Instantly share code, notes, and snippets.

@jonhurlock
Last active August 29, 2015 14:13
Show Gist options
  • Save jonhurlock/9f2a31f25fc2c4c2598d to your computer and use it in GitHub Desktop.
Save jonhurlock/9f2a31f25fc2c4c2598d to your computer and use it in GitHub Desktop.
def reverse_sentence_order(forward_sentence):
backwards_sentence = ""
words_in_stack = forward_sentence.split(" ")
for x in range(0,len(words_in_stack)):
backwards_sentence = backwards_sentence+" "+ words_in_stack.pop()
return backwards_sentence[1:]
"""
Code Breakdown
--------------
line 2: setup a string
 line 3: create a stack of words, based on the sentence, but splitting the sentence by spaces
line 4: loop over items in the stack, - for x in 0 to the length/size of the stack O(n) runtime.
line 5: concatinates a space, plus a space, then pops the top word from the words stack
line 6: returns everything in the backwards sentence, except for the 1st character.
this is becaues we add a space to the start in line 5
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment